Cod sursa(job #412366)

Utilizator alexandru92alexandru alexandru92 Data 5 martie 2010 15:35:21
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
#include <queue>
#include <vector>
#include <fstream>
#define Nmax 10001

/*
 *
 */
using namespace std;
int sink;
int C[Nmax][Nmax];
vector< vector< int > > G;
vector< int >::const_iterator it, iend;
int find_path( void )
{
	int x;
	queue< int > Q;
	vector< int > father( sink+1, -1 );
	Q.push( 0 );
	while( -1 == father[sink] && !Q.empty() )
	{
		x=Q.front(); Q.pop();
		for( it=G[x].begin(), iend=G[x].end(); it < iend; ++it )
			if( -1 == father[*it] && C[x][*it] > 0 )
			{
				father[*it]=x;
				Q.push( *it );
			}
	}
	if( -1 == father[sink] )
		return 0;
	father[0]=-1;
	for( x=sink; -1 != father[x]; x=father[x] )
	{
		C[father[x]][x]-=1;
		C[x][father[x]]+=1;
	}
	return 1;
}
int MaxBipartitMach( void )
{
	int s;
	for( s=0; find_path(); ++s );
	return s;
}
int main( void )
{
	int N, M, E, x, y;
	ifstream in( "cuplaj.in" );
	in>>N>>M>>E;
	sink=max( N, M )+1;
	G.resize( sink+1 );
	for( ; E; --E )
	{
		in>>x>>y;
		C[0][x]=C[y][sink]=C[x][y]=C[y][x]=1;
		G[0].push_back( x );
		G[x].push_back( 0 );
		G[x].push_back( x );
		G[y].push_back( y );
		G[y].push_back( sink );
		G[sink].push_back( y );
	}
	ofstream out( "cuplaj.out" );
	out<<MaxBipartitMach();
	return 0;
}