Cod sursa(job #554196)

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

using namespace std;
int N, M;
bool was[N_MAX];
int L[N_MAX], R[N_MAX];
vector< int > G[N_MAX], v;
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();
	for( it=v.begin(); it < iend; ++it )
		if( !L[*it] )
		{
			if( Check(*it) )
				++maxMatch;
			else {
					fill( was+1, was+N+1, false);
					if( Check(*it) )
						++maxMatch;
				 }
		}
	ofstream out( "cuplaj.out" );
	out<<maxMatch<<'\n';
	for( it=v.begin(); it < iend && maxMatch; ++it )
		if( L[*it] )
		{
			out<<*it<<' '<<L[*it]<<'\n';
			--maxMatch;
		}
	return EXIT_SUCCESS;
}