Cod sursa(job #2218279)

Utilizator cella.florescuCella Florescu cella.florescu Data 4 iulie 2018 05:18:19
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e4;

vector < int > g[MAXN + 1];
int conl[MAXN + 1], conr[MAXN + 1], seen[MAXN + 1];

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

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