Cod sursa(job #614330)

Utilizator ELHoriaHoria Cretescu ELHoria Data 5 octombrie 2011 23:54:48
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> G[10002];
int n , m , e , r[10002] , l[10002] , CUPLAJ;
bool u[10002];

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

int main()
{
	ifstream fin("cuplaj.in");
	ofstream fout("cuplaj.out");
	fin>>n>>m>>e;
	for(int x,y;e;e--)
	{
		fin>>x>>y;
		G[x].push_back(y);
	}
	for(int change = 1;change;)
	{
		change = 0;
		fill(u+1,u+n+1,false);
		for(int i=1;i<=n;++i)
			if(!l[i])
				change|=cuplaj(i);
	}

	for(int i=1;i<=n;++i) CUPLAJ +=(l[i]>0);
	fout<<CUPLAJ<<'\n';
	for(int i=1;i<=n;++i)
		if(l[i]>0) 
	fout<<i<<' '<<l[i]<<'\n';
	return 0;
}