Cod sursa(job #2188117)

Utilizator tifui.alexandruTifui Ioan Alexandru tifui.alexandru Data 26 martie 2018 22:24:38
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <bits/stdc++.h>
#define Nmax 10001
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
int vL[Nmax],vR[Nmax];
bool viz[Nmax];
list <int> G[Nmax];
bool match(int x)
{
    if(viz[x]) return false;
    viz[x]=true;
    for(const auto &it:G[x])
        if(!vL[it])
        {
            vL[it]=x;
            vR[x]=it;
            return true;
        }
    for(const auto &it:G[x])
        if(match(vL[it]))
        {
            vL[it]=x;
            vR[x]=it;
            return true;
        }
    return false;
}
int main()
{
    int n,m,e,i,j;
    f>>n>>m>>e;
    while(e--)
    {
        f>>i>>j;
        G[i].push_back(j);
    }
    bool ok=true;
    int ans=0;
    while(ok)
    {
        ok=false;
        memset(viz,false,sizeof(viz));
        for(i=1;i<=n;i++)
            if(!vR[i] and match(i))
            {
                ok=true;
                ++ans;
            }
    }
    g<<ans<<'\n';
    for(i=1;i<=n;i++)
        if(vR[i]) g<<i<<' '<<vR[i]<<'\n';

    return 0;
}