Cod sursa(job #530149)

Utilizator PlayLikeNeverB4George Marcus PlayLikeNeverB4 Data 7 februarie 2011 00:34:51
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
#include <fstream>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");

#define maxn 10005

struct nod {
	int inf;
	nod *next;
} *A[maxn];
int i,N,M,E,NR,ok;
bool v[maxn];
int L[maxn],R[maxn];

void citire()
{
	int x,y; nod *q;
	fin >> N >> M >> E;
	for(i=1;i<=E;i++)
	{
		fin >> x >> y;
		q=new nod;
		q->inf=y;
		q->next=A[x];
		A[x]=q;
	}
}

int pairup(int x)
{
	nod *q;
	if(v[x]) return 0;
	v[x]=true;
	for(q=A[x];q;q=q->next)
		if( R[q->inf]==0 || pairup(R[q->inf]) )
		{
			L[x]=q->inf; R[q->inf]=x; return 1;
		}
	return 0;
}

int main()
{
	citire();
	ok=1;
	while(ok)
	{
		ok=0;
		for(i=0;i<=N;i++) v[i]=0;
		for(i=1;i<=N;i++)
			if(L[i]==0)
				if( pairup(i) )
				{
					NR++;
					ok=1;
				}
	}
	fout << NR << '\n';
	for(i=1;i<=N;i++)
		if(L[i]>0)
			fout << i << " " << L[i] << '\n';
}