Cod sursa(job #699571)

Utilizator arch_enemyAngela Gossow arch_enemy Data 29 februarie 2012 20:10:59
Problema Cuplaj maxim in graf bipartit Scor 24
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

#define NMAX 10001
#define pb push_back

int N1, N2, M, x, y, i, Lanturi, CuplajMax;
int St[NMAX], Dr[NMAX];
vector< int > G[NMAX];
bool Used[NMAX];

inline int Cupleaza( int Nod )
{
	if( Used[Nod] ) return 0;
	Used[Nod] = true;
	
	for( vector< int >::iterator it = G[Nod].begin(); it != G[Nod].end(); ++it )
		if( !Dr[*it] )
		{
			St[Nod] = *it;
			Dr[*it] = Nod;
			return 1;
		}
	for( vector< int >::iterator it = G[Nod].begin(); it != G[Nod].end(); ++it )
		if( Cupleaza( *it ) )
		{
			St[Nod] = *it;
			Dr[*it] = Nod;
			return 1;
		}
	return 0;
}

int main()
{
	freopen("cuplaj.in", "r", stdin);
	freopen("cuplaj.out", "w", stdout);
	
	scanf("%d%d%d", &N1, &N2, &M );
	for( ; M--; )
	{
		scanf("%d%d", &x, &y );
		G[x].pb( y );
	}
	
	memset( St, 0, sizeof(St) );
	memset( Dr, 0, sizeof(Dr) );
	
	for( Lanturi = 1; Lanturi; )
	{
		Lanturi = 0;
		memset( Used, false, sizeof(Used) );
		for( i = 1; i <= N1; ++i )
			if( !St[i] )
				Lanturi += Cupleaza( i );
	}
	
	for( CuplajMax = 0, i = 1; i <= N1; ++i )
		CuplajMax += (int)( St[i] > 0 );
	printf("%d\n", CuplajMax );
	for( i = 1; i <= N1; ++i )
		if( St[i] )
			printf("%d %d\n", i, St[i] );
	
	return 0;
}