Cod sursa(job #573803)

Utilizator avram_florinavram florin constantin avram_florin Data 6 aprilie 2011 16:30:12
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include<fstream>
#include<vector>

#define InFile "cuplaj.in"
#define OutFile "cuplaj.out"

using namespace std;

ifstream f (InFile);
ofstream g (OutFile);

const int MaxN = 10001;

int L,R,E,unit[MaxN],l[MaxN],r[MaxN];
vector<int> G[MaxN];

int pairup(int nod)
{
	if( unit[nod] )
		return 0;
	unit[nod] = 1;
	vector<int>::iterator it;
	for( it = G[nod].begin() ; it != G[nod].end() ; ++it )
		if( !r[*it] )
			{
				l[nod] = *it;
				r[*it] = nod;
				return 1;
			}
	for( it = G[nod].begin() ; it != G[nod].end() ; ++it )
		if( pairup(r[*it]) )
			{
				l[nod] = *it;
				r[*it] = nod;
				return 1;
			}
	return 0;
}

int main()
{
	f >> L >> R >> E;
	int i,x,y;
	for( i = 0 ; i < E ; i++ )
		{
			f >> x >> y;
			G[x].push_back(y);
		}
	int stare = 1;
	while( stare )
		{
			stare = 0;
			memset(unit,0,sizeof(unit));
			for( i = 1 ; i <= L ; i++ )
				if( !l[i] )
					stare = stare | pairup(i);
		}
	int cuplaj= 0;
	for( i = 1 ; i <= L ; i++ )
		if( l[i] )
			cuplaj++;
	g << cuplaj << '\n';
	for( i = 1 ; i <= L ; i++ )
		if( l[i] )
			g << i << ' ' << l[i] <<'\n';
	return 0;
}