Cod sursa(job #3260008)

Utilizator andreic06Andrei Calota andreic06 Data 28 noiembrie 2024 20:19:55
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.19 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in ("cuplaj.in");
ofstream out ("cuplaj.out");

const int N_MAX = 1e4;

int N, M, R; vector<int> g[1 + 2 * N_MAX];
void readInput (void) {
     in >> N >> M >> R;
     for (int i = 1; i <= R; i ++) {
        int u, v; in >> u >> v;
        g[u].push_back (N + v);
        g[N + v].push_back (u);
     }
}

bool visited[1 + 2 * N_MAX];
int p[1 + 2 * N_MAX];

bool repair (int root) {
   visited[root] = true;
   for (int node : g[root]) {
      if (!p[node] || (!visited[p[node]] && repair (p[node]))) {
        p[root] = node, p[node] = root;
        return true;
      }
   }
   return false;
}

int main()
{
   readInput ();

   int match = 0;

   bool fine = true;
   while (fine) {
       for (int node = 1; node <= N + M; node ++) visited[node] = false;
       int cnt = 0;
       for (int node = 1; node <= N; node ++)
          if (!visited[node] && !p[node] && repair (node))
            cnt ++;
       fine &= (cnt > 0);
       match += cnt;
   }
   out << match << endl;
   for (int node = 1; node <= N; node ++)
      if (p[node])
        out << node << " " << p[node] - N << endl;
    return 0;
}