Cod sursa(job #234174)

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

vi A1[MAX], A2[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 );
		A2[y].pb( x );
	}
}

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

int PairUP( int x ) {
	if ( U[x] ) return 0;
	U[x] = 1;
	for ( vi::iterator i=A1[x].begin(); i!=A1[x].end(); ++i ) 
		if ( R[ *i ] == 0 ) {
			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;
	for ( int i = 1; i<=N; ++i ) 
		if ( PairUP(i) )
			c++;
		else {
			memset(U, 0, sizeof(U));
			c += PairUP(i);
		}
	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;
}