Cod sursa(job #445138)

Utilizator cosmin79Carabet Cosmin Andrei cosmin79 Data 22 aprilie 2010 21:11:48
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <stdio.h>
#include <vector>
#define NMAX 10005
#define pb push_back
#include <string.h>
using namespace std;
int n,m,e,st[NMAX],dr[NMAX],rez;
vector <int> A[NMAX];
char marc[NMAX];
void read()
{
	scanf("%d%d%d",&n,&m,&e);
	int i,x,y;
	for (i=1; i<=e; i++)
	{
		scanf("%d%d",&x,&y);
		A[x].pb(y);
	}
}
int pairup(int x)
{
	marc[x]=1;
	int i,y;
	for (i=0; i<A[x].size(); i++)
	{
		y=A[x][i];
		if (!dr[y])
		{
			dr[y]=x; 
			st[x]=y;
			return 1;
		}
	}
	for (i=0; i<A[x].size(); i++)
	{
		y=A[x][i];
		if (!marc[dr[y]] && pairup(dr[y]))
		{
			dr[y]=x;
			st[x]=y;
			return 1;
		}
	}
	return 0;
}
void cuplaj()
{
	int i,fin=1;
	while (fin)
	{
		fin=0;
		memset(marc,0,sizeof(marc));
		for (i=1; i<=n; i++)
			if (!st[i])
			{
				if (pairup(i))
				{
					rez++;
					fin=1;
				}
			}
	}
}
void show()
{
	printf("%d\n",rez);
	int i;
	for (i=1; i<=n; i++)
		if (st[i])
			printf("%d %d\n",i,st[i]);
}
int main()
{
	freopen("cuplaj.in","r",stdin);
	freopen("cuplaj.out","w",stdout);
	read();
	cuplaj();
	show();
	return 0;
}