Cod sursa(job #376794)

Utilizator klamathixMihai Calancea klamathix Data 22 decembrie 2009 16:33:15
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include<cstdio>
#include<vector>
using namespace std;
#define maxn 10005
int i , j , k , n , m , e , a, b , edges;
vector <int> G[maxn];
bool U[maxn];
int L[maxn],R[maxn];

int cupleaza ( int x ) 
{
	int i;
	if ( U[x] ) return 0;
	
	U[x] = true;
	
	for( i = 0 ; i < G[x].size() ; ++i)
		if ( R[G[x][i]] == 0 || cupleaza ( R[G[x][i]] ))
		{
			L[x] = G[x][i];
			R[G[x][i]] = x;
			return 1;
		}
		
return 0;
}

void clear()
{
	int i;
	for( i = 1 ; i <= n ; ++i )
		U[i] = 0;
}


int cuplaj()
{
	bool ok = 1;
	int result = 0;
	
	while ( ok ) 
	{
		clear();
		ok = 0;
		for( i = 1 ; i <= n ; ++i )
			if ( L[i] == 0 ) 
				if ( cupleaza ( i ) )
					result++ , ok = 1;;
		/*
		printf("%d\n",result);
		for( i = 1 ; i <= n ; ++i )
			if ( L[i] )
				printf("%d %d\n",i,L[i]);
		printf("\n");
		*/
	}
	
return result;
}

int main()
{
	freopen("cuplaj.in","r",stdin);
	freopen("cuplaj.out","w",stdout);
	
	scanf("%d %d %d",&n,&m,&edges);
	
	for( ; edges -- ;)
	{
		scanf("%d %d",&a,&b);
		G[a].push_back(b);
	}
	
	printf("%d\n",cuplaj());
	
	for( i = 1 ; i <= n ; ++i )
		if ( L[i] )
			printf("%d %d\n",i,L[i]);
		
return 0;
}