Cod sursa(job #2817751)

Utilizator PetyAlexandru Peticaru Pety Data 14 decembrie 2021 10:00:44
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 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");


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

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

int main()
{
  //ios_base::sync_with_stdio(false);
  //cin.tie(0); cout.tie(0);
  fin >> n >> m >> e;
  for (int i =1; i <= e; i++) {
    int x, y;
    fin >> x >> y;
    G[x].push_back(y);
  }
  do {
    bool ok = 0;
    memset(viz, 0, sizeof(viz));
    for (int i = 1; i <= n; i++) {
      if (!st[i] && cuplaj(i))
        ok = 1;

    }
    if (!ok)
      break;
  }while (1);
  for (int i = 1; i <= n; i++)
    if (st[i])
      ans++;
  fout << ans << "\n";
  for (int i = 1; i <= n; i++)
    if (st[i])
      fout << i << " " << st[i] << "\n";
    
  return 0;
}