Cod sursa(job #234180)

Utilizator sima_cotizoSima Cotizo sima_cotizo Data 20 decembrie 2008 10:44:18
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <cstdio>
#include <vector>
using namespace std;
#define MAX 10002
#define pb push_back
typedef vector<int> vi;

vi A1[MAX];
int N, M, E;

void load() {
	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 );
	}
}

bool U[MAX];
int L[MAX], R[MAX];

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;
		}
/*	for ( vi::iterator i=A1[x].begin(); i!=A1[x].end(); ++i ) 
		if ( PairUP( R[*i] ) ) {
			L[x] = *i; R[*i] = x;
			return 1;
		}
*/	return 0;
}

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() {
	load();

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