Cod sursa(job #3293787)

Utilizator andreic06Andrei Calota andreic06 Data 12 aprilie 2025 16:30:15
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#include <bits/stdc++.h>

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

const int N_MAX = 1e4;
const int M_MAX = 1e4;
const int E_MAX = 1e5;

int N, M, E; vector<int> g[1 + N_MAX + M_MAX];
void readInput (void) {
   fin >> N >> M >> E;
   for (int i = 1; i <= E; i ++) {
      int u, v; fin >> u >> v;
      g[u].push_back (N + v);
      g[N + v].push_back (u);
   }
}

int p[1 + N_MAX + M_MAX];
bool visited[1 + N_MAX + M_MAX];

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


int main()
{
   readInput ();

   bool fine = true;
   int match = 0;

   while (fine) {
       for (int node = 1; node <= N; 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;
   }
   fout << match << "\n";
   for (int node = 1; node <= N; node ++) {
      if (p[node])
        fout << node << " " << p[node] - N << "\n";
   }

    return 0;
}