Cod sursa(job #862200)

Utilizator veleanduAlex Velea veleandu Data 22 ianuarie 2013 13:38:39
Problema Cuplaj maxim in graf bipartit Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.86 kb
#include <cstdio>
#include <vector>
#include <fstream>
using namespace std;


	ifstream in("cuplaj.in");
	ofstream out("cuplaj.out");

	#define MaxChar 1000000
	#define verf ( (++CharB==MaxChar) ? ( in.read(Buffer,MaxChar),CharB=0 ) : (1) )

	long CharB=MaxChar-1;
	char Buffer [ MaxChar ];

	void cit ( int &a )
	{
	    bool ok=0;
	    for ( ; !( (Buffer[ CharB ]>='0' && Buffer[ CharB ]<='9') || ( Buffer[ CharB ] == '-' ) ); verf )
		;
	    if ( Buffer[ CharB ] == '-' ){
		CharB++;
		ok=1;
	    }
	    for ( a=0; (Buffer[ CharB ]>='0' && Buffer[ CharB ]<='9'); a*=10,a+=( Buffer[ CharB ]-'0'), verf )
		;
	    if ( ok ){
		a=-a;
	    }
	}


	#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 );
 	verf;
 	cit ( n );
	cit ( m );
	cit ( e );
	for ( i=1; i<=e; ++i ){
//		scanf ("%d %d", &a, &b );
		cit ( a );
		cit ( 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 );
	out<< rez <<"\n";
	for ( i=1; i<=n; ++i ){
		if ( Other[ i ] ){
//			printf("%d %d\n", i, Other[ i ] - n );
			out<< i <<" "<< Other[ i ] - n <<"\n";
		}
	}

	return 0;
}