Cod sursa(job #3234011)

Utilizator betybety bety bety Data 5 iunie 2024 20:46:30
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.18 kb
#include <bits/stdc++.h>
using namespace std;
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
const int lim = 1e4 + 5;
vector<int> righty[lim];
vector<int> lefty[lim];
int taken[lim];
int last_run[lim];
int wifey[lim];
int hubby[lim];
int n, m, e;
int x, y;
bool df(int nod, int id) {
  if (last_run[nod] == id) {
    return false;
  }
  last_run[nod] = id;
  for (int x : righty[nod]) {
    if (hubby[x] == 0) {
      hubby[x] = nod;
      wifey[nod] = x;
      return true;
    }
  }
  for (int x : righty[nod]) {
    if (df(hubby[x], id)) {
      hubby[x] = nod;
      wifey[nod] = x;
      return true;
    }
  }
  return false;
}
int main() {
  in >> n >> m >> e;
  while (e--) {
    in >> x >> y;
    righty[x].push_back(y);
    lefty[y].push_back(x);
  }
  int id;
  bool modif = true;
  for (id = 1; modif; id++) {
    modif = false;
    for (int i = 1; i <= n; ++i) {
      if (last_run[i] != id and wifey[i] == 0) {
        modif |= df(i, id);
      }
    }
  }
  int count = 0;
  for (int i = 1; i <= n; ++i) {
    count += (wifey[i] != 0);
  }
  out << count << '\n';
  for (int i = 1; i <= n; ++i) {
    if (wifey[i]) {
      out << i << ' ' << wifey[i] << '\n';
    }
  }
  return 0;
}