Cod sursa(job #2831312)

Utilizator iancupoppPopp Iancu Alexandru iancupopp Data 11 ianuarie 2022 09:45:20
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.03 kb
#include <fstream>
#include <vector>
#include <cstring>

using namespace std;

const int N = 1e4 + 5;

vector<int> gr[N];
int st[N], dr[N];
bool viz[N];

bool pairUp(int nod) {
  if (viz[nod]) return false;
  viz[nod] = true;
  for (auto nxt: gr[nod]) {
    if (st[nxt] == 0 || pairUp(st[nxt])) {
      dr[nod] = nxt;
      st[nxt] = nod;
      return true;
    }
  }
  return false;
}

int main() {
  ifstream cin("cuplaj.in");
  ofstream cout("cuplaj.out");
  int n, dummy, m;
  cin >> n >> dummy >> m;
  while (m--) {
    int x, y;
    cin >> x >> y;
    gr[x].push_back(y);
  }
  cin.close();
  bool ok = true;
  while (ok) {
    ok = false;
    memset(viz, 0, (n + 1) * sizeof(bool));
    for (int i = 1; i <= n; ++i)
      if (dr[i] == 0 && pairUp(i))
        ok = true;
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i)
    ans += (dr[i] != 0);
  cout << ans << "\n";
  for (int i = 1; i <= n; ++i)
    if (dr[i] != 0)
      cout << i << " " << dr[i] << "\n";
  cout.close();
  return 0;
}