Cod sursa(job #2737791)

Utilizator PetyAlexandru Peticaru Pety Data 5 aprilie 2021 10:03:45
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <bits/stdc++.h>
#define ll long long
#define ld long double

using namespace std;

const ll MOD = 1e9 + 7;
const ll INF = 1e9;

ifstream fin ("cuplaj.in");
ofstream fout ("cuplaj.out");

bool viz[10002];
int st[10002], dr[10002], n,m, e;
vector<int>G[10002];

bool match (int nod) {
  if (viz[nod])
    return 0;
  viz[nod] = 1;
  for (auto it : G[nod])
    if (!dr[it] || match(dr[it])) {
      st[nod] = it;
      dr[it] = nod;
      return 1;
    }
  return 0;
}

int main()
{
  fin >> n >> m >> e;
  while (e--) {
    int x, y;
    fin >> x >> y;
    G[x].push_back(y);
  }
  bool ok = 1;
  while (ok) {
    ok = 0;
    memset(viz, 0, sizeof(viz));
    for (int i = 1; i <= n; i++)
      if (!st[i] && match(i))
        ok = 1;
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++)
    if (st[i])
      cnt++;
  fout << cnt << "\n";
  for (int i = 1; i <= n; i++)
    if (st[i])
      fout << i << " " << st[i] << "\n";
  return 0;
}