Cod sursa(job #1884220)

Utilizator LucianTLucian Trepteanu LucianT Data 18 februarie 2017 15:44:10
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.27 kb
#include <bits/stdc++.h>
#define maxN 10005
using namespace std;
bool seen[maxN],ok;
vector<int> v[maxN];
int n,m,i,j,x,y,maxmatch;
int _left[maxN],_right[maxN];
bool pairup(int nod)
{
    if(seen[nod])
        return false;
    seen[nod]=true;
    for(auto it:v[nod])
        if(!_right[it]) /* node not matched */
        {
            _left[nod]=it;
            _right[it]=nod;
            maxmatch++;
            return true;
        }
    for(auto it:v[nod])
        if(pairup(_right[it])) /* re-match node */
        {
            _left[nod]=it;
            _right[it]=nod;
            return true;
        }
    return false;
}
int main()
{
    freopen("cuplaj.in","r",stdin);
    freopen("cuplaj.out","w",stdout);
    scanf("%d %d",&n,&m);
    scanf("%d",&m);
    for(i=1;i<=m;i++){
        scanf("%d %d",&x,&y),
        v[x].push_back(y);
    }
    ok=true;
    while(ok) /* maximum number of augmenting paths */
    {               /* simultaneously */
        ok=false;
        memset(seen,false,sizeof(seen));
        for(i=1;i<=n;i++)
            if(!_left[i] && pairup(i))
                ok=true;
    }
    printf("%d\n",maxmatch);
    for(i=1;i<=n;i++)
        if(_left[i])
            printf("%d %d\n",i,_left[i]);
    return 0;
}