Cod sursa(job #418021)

Utilizator stinkyStinky si Grasa stinky Data 15 martie 2010 12:10:13
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include<cstdio>
#include<cstring>
#include<vector>

using namespace std;

const int N = 1<<14;

int c,n,m,st[N],dr[N];
vector<int> stanga[N],dreapta[N];
bool marcat[N];

void citire()
{
	int e,x,y;
	scanf("%d%d%d",&n,&m,&e);
	while(e--)
	{
		scanf("%d%d",&x,&y);
		dreapta[x].push_back(y);
		stanga[y].push_back(x);
	}
}

bool cupleaza(int x)
{
	int y;
	if(marcat[x])
		return false;
	marcat[x] = true;
	for(size_t i=0 ; i<dreapta[x].size() ; ++i)
	{
		y = dreapta[x][i];
		if(!st[y] || cupleaza(st[y]))
		{
			dr[x] = y;
			st[y] = x;
			return true;
		}
	}
	return false;
}

void cuplaj()
{
	int i;
	bool gata;
	do{
		gata = true;
		memset(marcat,0,sizeof(marcat));
		for(i=1 ; i<=n ; ++i)
			if(!dr[i])
			{
				cupleaza(i);
				if(dr[i])
				{
					++c;
					gata = false;
				}
			}
	}while(!gata);
}

void scrie()
{
	printf("%d\n",c);
	for(int i=1 ; i<=n ; ++i)
		if(dr[i])
			printf("%d %d\n",i,dr[i]);
}

int main()
{
	freopen("cuplaj.in","r",stdin);
	freopen("cuplaj.out","w",stdout);
	citire();
	cuplaj();
	scrie();
	return 0;
}