Cod sursa(job #445071)

Utilizator funkydvdIancu David Traian funkydvd Data 22 aprilie 2010 18:01:43
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <cstdio>
#include <vector>
using namespace std;
#define MAX 10002
#define pb push_back
typedef vector<int> vi;
vi A1[MAX];
bool U[MAX];
int L[MAX], R[MAX],N, M, E;
int PairUp( int x ) 
{
	if ( U[x] ) return 0;
	U[x] = true;
	for ( vi::iterator i=A1[x].begin(); i!=A1[x].end(); ++i ) 
		if ( R[ *i ] == 0 || PairUp( R[*i] ) ) 
		{
			L[x] = *i; R[*i] = x;
			return 1;
		}

}
int cuplaj() {
	int c = 0, ok = 1;
	while ( ok )
	 {
		memset(U, 0, sizeof(bool)*(N+1));
		ok = 0;
		for ( int i = 1; i<=N; ++i ) 
			if ( L[i] == 0 ) 
				if ( PairUp(i) )  c ++, ok = 1;
	}
	return c;
}

int main() 
{
	freopen( "cuplaj.in", "r", stdin );
	freopen( "cuplaj.out", "w", stdout );
	scanf( "%d %d %d", &N, &M, &E );
	while ( E-- ) 
	{
		int x, y;
		scanf( "%d %d", &x, &y );
		A1[x].pb( y );
	}

	printf( "%d\n", cuplaj() );
	for ( int i=1; i<=N; ++i ) 
		if ( L[i] )
			printf( "%d %d\n", i, L[i] );
	return 0;
}