Cod sursa(job #2569383)

Utilizator sichetpaulSichet Paul sichetpaul Data 4 martie 2020 11:58:48
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.95 kb
#include <bits/stdc++.h>
#define Nmax 10005
using namespace std;

ifstream f("cuplaj.in");
ofstream g("cuplaj.out");

int N, M, E, ans;
int le[Nmax], ri[Nmax];
bitset<Nmax> vis;
vector<int> G[Nmax];

bool match(int x) {
   if (vis[x]) return 0;
   vis[x] = 1;

   for (auto y: G[x])
   if (!le[y]) {
       ri[x] = y;
       le[y] = x;
       return 1;
   }

   for (auto y: G[x])
     if (match(le[y])) {
         ri[x] = y;
         le[y] = x;
         return 1;
     }

   return 0;
}
int main()
{
    f >> N >> M >> E;
    for (int i = 1; i <= E; ++i) {
        int x, y;
        f >> x >> y;
        G[x].push_back(y);
    }

    bool ok = 1;
    while (ok) {
        ok = 0;
        for (int i = 1; i <= N; ++i)
            if (!ri[i] && match(i)) ++ans, ok = 1;
        vis.reset();
    }

    g << ans << '\n';
    for (int i = 1; i <= N; ++i)
        if (ri[i]) g << i << " " << ri[i] << '\n';

    return 0;
}