Cod sursa(job #554178)

Utilizator BitOneSAlexandru BitOne Data 14 martie 2011 17:43:43
Problema Cuplaj maxim in graf bipartit Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include <vector>
#include <fstream>
#include <cstdlib>
#define N_MAX 10011
#define oo 1<<20

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