Cod sursa(job #2679629)

Utilizator KPP17Popescu Paul KPP17 Data 1 decembrie 2020 08:53:45
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.95 kb
#include <fstream>
#define fisier "cuplaj"
std::ifstream in(fisier ".in");
std::ofstream out(fisier ".out");
const int N = 15001;
int R[N], L[N], s;
#include <vector>
std::vector<int> V[N];
#include <bitset>
std::bitset<N> E;
bool dfs(int);
bool cup(int l, bool p)
{
    for (int r: V[l])
        if (p? dfs(L[r]): not L[r])
        {
            L[r] = l, R[l] = r;
            if (not p) s++;
            return true;
        }
    return false;
}
bool dfs(int l)
{
    if (E[l]) return false;
    E[l] = true;
    return cup(l, false) or cup(l, true);
}
int main()
{
    int n, m, c; in >> n >> m >> c;
    while (c--)
    {
        int a, b; in >> a >> b;
        V[a].push_back(b);
    }
    do {
        E = c = false;
        for (int l = 1; l <= n; l++)
            c |= not R[l] and dfs(l);
    } while (c);
    out << s << '\n';
    for (int l = 1; l <= n; l++)
        if (R[l]) out << l << ' ' << R[l] << '\n';
}