Cod sursa(job #561829)

Utilizator antoanelaAntoanela Siminiuc antoanela Data 21 martie 2011 20:11:39
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <cstdio>
#include <vector>
using namespace std;
#define nmax 10010

vector <int> g[nmax];
int n, m, e, sol, d, c[nmax], u[nmax], p[nmax];

int pairup(int nod)
{
	if (u[nod]==d) return 0;
	int i;
	u[nod]=d;
	for (i=0; i<g[nod].size(); i++)
		if (p[g[nod][i]]==-1 || pairup(p[g[nod][i]]))
		{
			c[nod]=g[nod][i];
			p[g[nod][i]]=nod;
			return 1;
		}
	return 0;
}

int main()
{
	freopen("cuplaj.in","r",stdin);
	freopen("cuplaj.out","w",stdout);
	scanf("%d %d %d",&n, &m, &e);
	int last, x, y, i;
	while (e--)
	{
		scanf("%d %d", &x, &y);
		g[x].push_back(y);
	}
	for (i=1; i<=m; i++) p[i]=-1;
	last=-1;
	while (last!=sol)
	{
		last=sol;
		d++;
		for (i=1; i<=n; i++)  
			if (!c[i]) sol+=pairup(i);
	}
	printf("%d\n",sol);
	for (i=1; i<=n; i++) 
		if (c[i]) printf("%d %d\n",i,c[i]);
}