Cod sursa(job #588842)

Utilizator rendorzegAndrei Pavel rendorzeg Data 9 mai 2011 19:29:19
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include<stdio.h>
#define N 10005
int s[N],d[N],n,v[N],k;
struct muchie
{
	int info;
	muchie *next;
}*c[N],*q;
int df(int x)
{
	muchie *q;
	if (v[x]==1)
		return 0;
	v[x]=1;
	q=c[x];
	while (q)
	{
		if (d[q->info]==0)
		{
			s[x]=q->info;
			d[q->info]=x;
			return 1;
		}
		q=q->next;
	}
	q=c[x];
	while (q)
	{
		if (df(d[q->info])==1)
		{
		s[x]=q->info;
		d[q->info]=x;
		return 1;
		}
		q=q->next;
	}
	return 0;
}			
void cupleaza ()
{
	int ok=0;
	int i;
	while (ok==0)
	{
		ok=1;
		for (i=1;i<=n;i++)
			v[i]=0;
		for (i=1;i<=n;i++)
			if (s[i]==0)
				if (df (i)==1)
				{
					ok=0;
					k++;
				}
	}
}
int main()
{
	int m,e;
	freopen ("cuplaj.in","r",stdin);
	freopen ("cuplaj.out","w",stdout);
	scanf("%i%i%i",&n,&m,&e);
	int i;
	for (i=0;i<e;i++)
	{
		int x,y;
		scanf("%i%i",&x,&y);
		q=new muchie;
		q->info=y;
		q->next=c[x];
		c[x]=q;
	}
	cupleaza();
	printf("%i\n",k);
	for (i=1;i<=N;i++)
		if (s[i]!=0)
			printf("%i %i\n",i,s[i]);
	return 0;
}