Cod sursa(job #380992)

Utilizator cristikIvan Cristian cristik Data 8 ianuarie 2010 15:25:03
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.03 kb
#include <stdio.h>
#include <vector>
#include <set>
#define max 10010
const int inf=0x3f3f3f3f;
using namespace std;
typedef vector<int> ve;
ve a[max];
int left[max],right[max];
int nr,n,m,edges,i,j;
char u[max];
int cuplaj(int nod)
{
    if(u[nod]) return 0;
    u[nod]=1;
    for(ve::iterator i=a[nod].begin(); i!=a[nod].end(); ++i)
     if(!right[*i] || cuplaj(right[*i]))
     {
         left[nod]=*i;
         right[*i]=nod;
         return 1;
     }
    return 0;

}
int main()
{
    freopen("cuplaj.in","r",stdin);
    freopen("cuplaj.out","w",stdout);
    scanf("%d%d%d",&n,&m,&edges);
    for(; edges; edges--)
    {
        scanf("%d%d",&i,&j);
        a[i].push_back(j);
    }
    for(int change=1; change; )
    {
        change=0;
        memset(u,0,sizeof(u));
        for(i=1; i<=n; i++)
         if(!left[i]) change|=cuplaj(i);
    }
    for(i=1; i<=n; i++) nr+=(left[i]>0);
    printf("%d\n",nr);
    for(i=1; i<=n; i++)
     if(left[i]) printf("%d %d\n",i,left[i]);
    return 0;
}