Cod sursa(job #3168778)

Utilizator alexdumitruAlexandru Dumitru alexdumitru Data 13 noiembrie 2023 12:01:57
Problema Cuplaj maxim in graf bipartit Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.18 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 1e1 + 5;

int n, m, e;
int st[NMAX];
int dr[NMAX];
bool used[NMAX];
vector<int> g[NMAX];

bool pairUp(int node) {
  if (used[node])
    return false;
  used[node] = true;
  for (auto it : g[node])
    if (!dr[it]) {
      st[node] = it;
      dr[it] = node;
      return true;
    }
  for (auto it : g[node])
    if (pairUp(dr[it])) {
      st[node] = it;
      dr[it] = node;
      return true;
    }
  return false;
}

void solve() {
  fin >> n >> m >> e;
  for (int i = 1; i <= e; i++) {
    int u, v;
    fin >> u >> v;
    g[u].push_back(v);
  }
  bool ok = true;
  while (ok) {
    ok = false;
    for (int i = 1; i <= n; i++)
      used[i] = false;
    for (int i = 1; i <= n; i++)
      if (!st[i])
        ok |= pairUp(i);
  }
  int ans = 0;
  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';
}

int main() {
  ios_base :: sync_with_stdio(false);
  cin.tie(nullptr);
  solve();
  return 0;
}