Cod sursa(job #749513)

Utilizator BitOneSAlexandru BitOne Data 17 mai 2012 15:29:22
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include <bitset>
#include <vector>
#include <fstream>
#include <cstdlib>

using namespace std;

const int N_MAX=10011;

bitset<N_MAX> was;
int L[N_MAX], R[N_MAX];
vector<int> G[N_MAX];
vector<int>::const_iterator it, iend;

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