Cod sursa(job #862227)

Utilizator veleanduAlex Velea veleandu Data 22 ianuarie 2013 14:12:11
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include <cstdio>
#include <vector>
#include <fstream>
using namespace std;

	#define max_n 10005
	#define pb push_back

int n,m,e,i,a,b,rez;

bool Viz[ 2*max_n ], Cuplaj[ 2*max_n ];
int Other[ 2*max_n ];
vector<int> T[ max_n ];

bool df ( int nod ){
    Viz[ nod ] = 1;
	if ( !Cuplaj[ nod ] ){
		Cuplaj[ nod ] = 1;
		return 1;
	}

	if ( nod > n ){
		return df ( Other[ nod ] );
	}

	for ( int i=0; i< int ( T[ nod ].size() ); ++i ){
		if ( !Cuplaj[ T[ nod ][ i ] ] ){
 			if ( df ( T[ nod ][ i ] ) ){
				Other[ nod ] = T[ nod ][ i ];
				Other[ T[ nod ][ i ] ] = nod;
				return 1;
			} 
		}
	}

	// nodul e din partea stanga.
	for ( int i=0; i<int ( T[ nod ].size() ); ++i ){
    	if ( !Viz[ T[ nod ][ i ] ] ){
			if ( df ( T[ nod ][ i ] ) ){
				Other[ nod ] = T[ nod ][ i ];
				Other[ T[ nod ][ i ] ] = nod;
				return 1;
			}
		}
	}
	return 0;
}

int main(){

    freopen ("cuplaj.in","r",stdin);
	freopen ("cuplaj.out","w",stdout);
   	
 	scanf ("%d %d %d", &n, &m, &e );
	for ( i=1; i<=e; ++i ){
		scanf ("%d %d", &a, &b );
		b+=n;
		T[ a ].pb ( b );
	}
	int ok=1;
	while ( ok ){
		ok=0;
		for ( i=1; i<=n; ++i ){
			if ( !Cuplaj[ i ] ){
			Cuplaj[ i ] = 1;
				if ( df ( i ) ){
					ok=1;
					rez++;

				}   
				else{
					Cuplaj[ i ] = 0;
				}
			}
		} 
		for ( int j=1; j<=n+m; ++j ){
			Viz[ j ] = 0;
		}
	}
	printf("%d\n", rez );
	for ( i=1; i<=n; ++i ){
		if ( Other[ i ] ){
			printf("%d %d\n", i, Other[ i ] - n );
		}
	}

	return 0;
}