Cod sursa(job #576702)

Utilizator BitOneSAlexandru BitOne Data 9 aprilie 2011 14:25:37
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include <bitset>
#include <vector>
#include <fstream>
#include <cstdlib>
#define N_MAX 10011

using namespace std;
int L[N_MAX], R[N_MAX], SM[N_MAX];
bitset< N_MAX > wasVist;
vector< int > G[N_MAX];
inline bool PairUp( int x )
{
	if( wasVist.test(x) )
		return false;
	wasVist.flip(x);
	vector< int >::const_iterator it=G[x].begin(), iend=G[x].end();
	for( ; it < iend; ++it )
		if( !R[*it] || PairUp( R[*it] ) )
		{
			L[x]=*it;
			R[*it]=x;
			return true;
		}
	return false;
}
int main( void )
{
	bool ok;
	int N, M, E, x, y, _count=0;
	ifstream in( "cuplaj.in" );
	for( in>>N>>M>>E; E; --E )
	{
		in>>x>>y;
		G[x].push_back(y);
	}
	do
	{
		ok=false;
		for( x=1; x <= N; ++x )
			if( !L[x] && PairUp(x) )
				++_count, ok=true;
		wasVist&=0;
	}while( ok );
	ofstream out( "cuplaj.out" );
	out<<_count<<'\n';
	for( x=1; x <= N && _count; ++x )
		if( L[x] )
			out<<x<<' '<<L[x]<<'\n', --_count;
	return EXIT_SUCCESS;
}