Cod sursa(job #1151791)

Utilizator tudgal1001Profir Tudor tudgal1001 Data 24 martie 2014 12:58:49
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include<cstdio>
#include<cstring>
#include<vector>
#define NMax 10005
using namespace std;

int viz[NMax],st[NMax],dr[NMax];
vector<int> vc[NMax];

int cuplu (int nod)
{
    if (viz[nod]) return 0;
    viz[nod]=1;
    for (vector<int>::iterator it=vc[nod].begin(); it!=vc[nod].end(); ++it)
        if (!dr[*it] || cuplu(dr[*it]))
        {
            st[nod]=*it;
            dr[*it]=nod;
            return 1;
        }
    return 0;
}

int main ()
{
    int i,x,y,ok=1,cont=0,N,M,E;
    freopen("cuplaj.in","r",stdin);
    freopen("cuplaj.out","w",stdout);
    scanf("%d%d%d",&N,&M,&E);
    for (i=1; i<=E; i++)
    {
        scanf("%d%d",&x,&y);
        vc[x].push_back(y);
    }
    while (ok)
    {
        ok=0;
        memset(viz,0,sizeof(viz));
        for (i=1; i<=N; i++)
            if (!st[i])
                ok|=cuplu(i);
    }
    for (i=1; i<=N; i++)
        if (st[i]) cont++;
    printf("%d\n",cont);
    for (i=1; i<=N; i++)
        if (st[i])
            printf("%d %d\n",i,st[i]);
    return 0;
}