Cod sursa(job #404449)

Utilizator Alexa_ioana_14Antoche Ioana Alexandra Alexa_ioana_14 Data 26 februarie 2010 10:28:45
Problema Cuplaj maxim in graf bipartit Scor 24
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include<cstdio>
#include<bitset>
#include<vector>
using namespace std;
#define NMAX 10005
#define sh short int
#define pb push_back
sh N,M,x,y,dr[NMAX],st[NMAX];
int nr,E;
bitset<NMAX>viz;
vector<sh>a[NMAX];
bool df (sh n)
{
	if (viz[n])
		return 0;
	viz[n]=1;
	size_t g=a[n].size();
	for (size_t i=0; i<g; ++i)
	{
		if (!dr[a[n][i]] || df(a[n][i]))
		{
			st[n]=a[n][i];
			dr[a[n][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].pb(y);
	}
	cuplaj();
	printf("%d\n",nr);
	for (int i=1;i<=N; ++i)
	{
		if (st[i]>0)
		printf("%hd %hd\n",i,st[i]);
	}
	return 0;
}