Cod sursa(job #615609)

Utilizator BitOneSAlexandru BitOne Data 10 octombrie 2011 12:31:26
Problema Cuplaj maxim in graf bipartit Scor 46
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <bitset>
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#define N_MAX 10011

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