Cod sursa(job #2949956)

Utilizator andreic06Andrei Calota andreic06 Data 2 decembrie 2022 13:35:18
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.37 kb
#include <bits/stdc++.h>

using namespace std;
const int NMAX = 1e4;
const int MMAX = 1e4;
const int NONE = -1;

int N, M, E; vector<int> g[1 + NMAX];

bool visited[1 + NMAX];
int to[1 + NMAX], from[1 + MMAX];

bool getPath (int root) {
   if (visited[root])
     return false;

   visited[root] = true;
   for (int node : g[root]) {
      if (from[node] == NONE || getPath (from[node])) {
        from[node] = root;
        to[root] = node;
        return true;
      }
   }
   return false;
}

int main()
{
   ifstream in ("cuplaj.in");
   in >> N >> M >> E;
   for (int i = 1; i <= E; i ++) {
      int u, v; in >> u >> v;
      g[u].push_back (v);
   }

   for (int node = 1; node <= max (N, M); node ++)
      to[node] = from[node] = NONE;

   bool found_match;
   do {
     found_match = false;
     for (int node = 1; node <= N; node ++)
        if (to[node] == NONE && getPath (node))
          found_match = true;
     for (int node = 1; node <= N; node ++)
        visited[node] = false;
   }while (found_match);

   int answer = 0;
   for (int node = 1; node <= M; node ++) {
      if (from[node] != NONE)
        answer ++;
   }

   ofstream out ("cuplaj.out");
   out << answer << "\n";
   for (int node = 1; node <= M; node ++)
      if (from[node] != NONE)
        out << from[node] << " " << node << "\n";
    return 0;
}