Cod sursa(job #1784002)

Utilizator cella.florescuCella Florescu cella.florescu Data 19 octombrie 2016 17:51:57
Problema Cuplaj maxim in graf bipartit Scor 24
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 10000;

vector <int> g[MAXN + 1];
int conleft[MAXN + 1], conright[MAXN + 1], seen[MAXN + 1];

int match(int node) {
  if (seen[node])
    return 0;
  seen[node] = 1;
  for (auto it : g[node])
    if (conright[it] == 0) {
      conleft[node] = it;
      conright[it] = node;
      return 1;
    }
  for (auto it : g[node])
    if (match(it)) {
      conleft[node] = it;
      conright[it] = node;
      return 1;
    }
  return 0;
}

int main()
{
    int n, m, e, i, x, y, augm;
    ifstream fin("cuplaj.in");
    fin >> n >> m >> e;
    for (i = 0; i < e; ++i) {
      fin >> x >> y;
      g[x].push_back(y);
    }
    fin.close();
    do {
      augm = 0;
      memset(seen, 0, sizeof(seen));
      for (i = 1; i <= n; ++i)
        if (conleft[i] == 0)
          augm += match(i);
    } while (augm);
    for (i = 1, augm = 0; i <= n; ++i)
      augm += (conleft[i] > 0);
    ofstream fout("cuplaj.out");
    fout << augm << '\n';
    for (i = 1; i <= n; ++i)
      if (conleft[i] > 0)
        fout << i << " " << conleft[i] << '\n';
    fout.close();
    return 0;
}