Cod sursa(job #2356740)

Utilizator triscacezarTrisca Vicol Cezar triscacezar Data 26 februarie 2019 21:18:35
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <bits/stdc++.h>

using namespace std;

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

int n,m,e,cnt,S[10010],D[10010];
vector<int> v[10010];
bitset<10010> viz;

bool match(int poz)
{
    if(viz[poz])return 0;
    viz[poz]=1;
    for(auto it:v[poz])
        if(!D[it])
        {
            D[it]=poz;
            S[poz]=it;
            return 1;
        }
    for(auto it:v[poz])
        if(match(D[it]))
        {
            D[it]=poz;
            S[poz]=it;
            return 1;
        }
    return 0;
}

int main()
{
    f>>n>>m>>e;
    for(;e;e--)
    {
        int x,y;
        f>>x>>y;
        v[x].push_back(y);
    }
    bool ok=1;
    for(;ok;)
    {
        ok=0;viz.reset();
        for(int i=1;i<=n;i++)
            if(!S[i])
                if(match(i))
                    ok=1,cnt++;
    }
    g<<cnt<<'\n';
    for(int i=1;i<=n;i++)
        if(S[i])
            g<<i<<' '<<S[i]<<'\n';
    return 0;
}