Cod sursa(job #896215)

Utilizator The_DisturbedBungiu Alexandru The_Disturbed Data 27 februarie 2013 14:24:43
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include<cstdio>
int n,m,q,i,j,c[10013],t[10013],x,y,nr;
bool ok,fol[10000];
struct point
{
	int x;
	point *y;
}*g[10013],*p;
inline bool gas(int i)
{
	if(fol[i])return 0;
	fol[i]=1;
	point *p;
	for(p=g[i];p!=NULL;p=p->y)
		if(t[p->x]==0)
		{
			c[i]=p->x;
			t[p->x]=i;
			return 1;
		}
	for(p=g[i];p!=NULL;p=p->y)
	{
		if(gas(t[p->x]))
		{
			c[i]=p->x;
			t[p->x]=i;
			return 1;
		}
	}
	return 0;
}
int main()
{
	freopen("cuplaj.in","r",stdin);
	freopen("cuplaj.out","w",stdout);
	scanf("%d%d%d",&n,&m,&q);
	for(i=0;i<q;++i)
	{
		scanf("%d%d",&x,&y);
		p=new point;
		p->x=y;
		p->y=g[x];
		g[x]=p;
	}
	ok=1;
	while(ok)
	{
		ok=0;
		for(i=0;i<=n;++i)fol[i]=0;
		for(i=1;i<=n;++i)if(c[i]==0) ok|=gas(i);
	}
	nr=0;
	for(i=1;i<=n;++i) if(c[i]!=0)++nr;
	printf("%d\n",nr);
	for(i=1;i<=n;++i) if(c[i]!=0) printf("%d %d\n",i,c[i]);
	return 0;
}