Cod sursa(job #2669262)

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