Cod sursa(job #553192)

Utilizator cr1st18Cristi cr1st18 Data 13 martie 2011 19:10:55
Problema Cuplaj maxim in graf bipartit Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include<iostream>
#include<fstream>
using namespace std;

#define nmax 10000

int a[nmax][nmax];
int l[nmax];
int r[nmax];
int viz[nmax];
int i,j,n,m,card,e,k;

int cupleaza(int i)
{
	int j;
	if(viz[i] == 1)
		return 0;
	
	viz[i] = 1;
	
	for(j=1;j<=n;j++)
		if(a[i][j] == 1)
			if(r[j] == 0)
			{
				l[i] = j;
				r[j] = i;
				return 1;
			}
	for(j=1;j<=m;j++)
		if(a[i][j] == 1)
			if(cupleaza(r[j]) == 1)
		{
			l[i] = j;
			r[j] = i;
			return 1;
		}
	
	return 0;
}


int main()
{
	
	ifstream fin("cuplaj.in");
	ofstream fout("cuplaj.out");
	
	fin>>n>>m>>e;
	
	for(k=1;k<=e;k++)
	{
		fin>>i>>j;
		a[i][j] = 1;
	}
	
	for(i=1;i<=n;i++)
	{
		if(l[i] == 0)
		{
		for(j=1;j<=n;j++)
			viz[j] = 0;
		card = card + cupleaza(i);
		}
		else
			card++;
	}
	
	fout<<card<<"\n";
	
	for(j=1;j<=m;j++)
		if(r[j] == 1)
			fout<<r[j]<<" "<<j<<"\n";
			
return 0;
}