Cod sursa(job #553813)

Utilizator dornescuvladVlad Eugen Dornescu dornescuvlad Data 14 martie 2011 12:44:18
Problema Cuplaj maxim in graf bipartit Scor 52
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

const char iname[] = "cuplaj.in";
const char oname[] = "cuplaj.out";
const int  nmax    = 10005;

ifstream fin(iname);
ofstream fout(oname);

int n, m, e, i, x, y, ct, am_cuplat;
vector<int> gr[nmax];
int viz[nmax], L[nmax], R[nmax];

int pair_up(int nod)
{	
	if(viz[nod])
		return 0;
	viz[nod] = 1;
	for(vector<int>::iterator it = gr[nod].begin(); it != gr[nod].end(); ++ it)
		if(pair_up(R[*it]) || R[*it] == 0)
		{
			L[i] = *it;
			R[*it] = nod;
			return 1;
		}
	return 0;
}

int main()
{
	fin >> n >> m >> e;
	for(i = 1; i <= e; i ++)
	{
		fin >> x >> y;
		gr[x].push_back(y);
	}
	
	am_cuplat = 1;
	while(am_cuplat)
	{	
		memset(viz, 0, sizeof(viz));
		am_cuplat = 0;
		for(i = 1; i <= n; i ++)
			if(!L[i])
				if(pair_up(i))
				{
					am_cuplat = 1;
					++ct;
				}
	}
	
	fout << ct << "\n";
	for(i = 1; i <= n; i ++)
		if(L[i])
			fout << i << " " << L[i] << "\n";
	return 0;
}