Cod sursa(job #862196)

Utilizator veleanduAlex Velea veleandu Data 22 ianuarie 2013 13:35:03
Problema Cuplaj maxim in graf bipartit Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <cstdio>
#include <vector>
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 ] );
	}

	// 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 );
	}
	for ( i=1; i<=n; ++i ){
		if ( !Cuplaj[ i ] ){
			Cuplaj[ i ] = 1;
			if ( df ( i ) ){
				rez++;
				for ( int j=1; j<=n+m; ++j ){
					Viz[ j ] = 0;
				}
			}   
			else{
				Cuplaj[ i ] = 0;
			}
		}
	}
	printf("%d\n", rez );
	for ( i=1; i<=n; ++i ){
		if ( Other[ i ] ){
			printf("%d %d\n", i, Other[ i ] - n );
		}
	}

	return 0;
}