Cod sursa(job #404437)

Utilizator Alexa_ioana_14Antoche Ioana Alexandra Alexa_ioana_14 Data 26 februarie 2010 10:02:59
Problema Cuplaj maxim in graf bipartit Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.9 kb
#include<cstdio>
#include<bitset>
using namespace std;
#define NMAX 10005
#define sh short int
sh N,M,x,y,dr[NMAX],st[NMAX];
int nr,E;
bitset<NMAX>a[NMAX];
bitset<NMAX>viz;
bool df (sh n)
{
	if (viz[n])
		return 0;
	viz[n]=1;
	for (sh i=1; i<=M; ++i)
	{
		if (!a[n][i])
			continue;
		if (!dr[i] || df(dr[i]) )
		{
			st[n]=i;
			dr[i]=n;
			return 1;
		}
	}
	return 0;
}
void cuplaj()
{
	sh i;
	for (i=1; i<=N; ++i)
	{
		if (st[i])
			continue;
		if (df(i))
			++nr;
		else
		{
			viz.reset();
			if (df(i))
				++nr;
		}
	}
}
int main()
{
	freopen("cuplaj.in","r",stdin);
	freopen("cuplaj.out","w",stdout);
	scanf("%hd%hd%d",&N,&M,&E);
	int i;
	for (i=1; i<=E; ++i)
	{
		scanf("%hd%hd",&x,&y);
		a[x][y]=1;
	}
	cuplaj();
	printf("%d\n",nr);
	for (int i=1;i<=N||i<=M; ++i)
	{
		if (st[i]&&dr[st[i]])
		printf("%hd %hd\n",dr[st[i]],st[i]);
	}
	return 0;
}