Cod sursa(job #2456511)

Utilizator tudi98Cozma Tudor tudi98 Data 14 septembrie 2019 15:18:32
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <bits/stdc++.h>
using namespace std;

vector<int> G[10005];
int L[10005], R[10005];
int N, M, E;
bool seen[10005];

bool cuplat(int x) {
  if (seen[x]) return 0;
  seen[x] = 1;
  for (auto p : G[x]) {
    if (!R[p] || cuplat(R[p])) {
      R[p] = x;
      L[x] = p;
      return 1;
    }
  }
  return 0;
}

int main() {
  ios_base::sync_with_stdio(false);
  ifstream cin("cuplaj.in");
  ofstream cout("cuplaj.out");

  cin >> N >> M >> E;
  for (int i = 1; i <= E; ++i) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
  }

  bool ok = true;
  int cup = 0;
  while (ok) {
    fill_n(seen + 1, 10000, 0);
    ok = 0;
    for (int i = 1; i <= N; ++i) {
      if (!L[i] && cuplat(i)) {
        ok = 1;
        ++cup;
      }
    }
  }

  cout << cup << "\n";
  for (int i = 1; i <= N; ++i)
    if (L[i]) cout << i << " " << L[i] << "\n";
}