Cod sursa(job #914497)

Utilizator dragangabrielDragan Andrei Gabriel dragangabriel Data 14 martie 2013 10:46:14
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include<cstdio>
#include<vector>
using namespace std;
int rez,st[10005],dr[10005],x,y,i,n,m,e;
bool ok,viz[10005];
vector<int>v[10005];

bool dfs(int x)
{
	if (viz[x]) return false;
	viz[x]=true;
	for (int j=0;j<v[x].size();j++)
		if (!dr[v[x][j]])
		{
			st[x]=v[x][j];
			dr[v[x][j]]=x;
			rez++;
			return true;
		}
	for (int j=0;j<v[x].size();j++)
		if (!dr[v[x][j]])
		{
			st[x]=v[x][j];
			dr[v[x][j]]=x;
			return true;
		}
	return false;
}

int main()
{
	freopen("cuplaj.in","r",stdin);
	freopen("cupalj.out","w",stdout);
	scanf("%d %d %d",&n,&m,&e);
	for (i=1;i<=e;i++) scanf("%d %d",&x,&y),v[x].push_back(y);
	ok=true;
	while (ok)
	{
		ok=false;
		memset(viz,false,sizeof(viz));
		for (i=1;i<=n;i++)if (!st[i]) if (dfs(i)) ok=true;
	}
	printf("%d\n",rez);
	for (i=1;i<=n;i++)
		if (st[i]) printf("%d %d",i,dr[i]);
	return 0;
}