Cod sursa(job #544775)

Utilizator brainwashed20Alexandru Gherghe brainwashed20 Data 2 martie 2011 09:06:11
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include<cstdio>
#include<vector>
#include<bitset>

#define Nmax 10050

using namespace std;

vector<int> G[Nmax];
bitset<Nmax> viz;
int N, M, L[Nmax], R[Nmax], sol;

inline bool cupleaza(int nod) {	
	if(viz[nod]) 
		return 0;
	viz[nod]=1;
	for(vector<int>:: iterator it=G[nod].begin(), it2=G[nod].end(); it!=it2; ++it)
		if(!R[*it] || cupleaza(R[*it])) {
			R[*it]=nod;
			L[nod]=*it;
			return 1;
		}
	return 0;
}

int main() {
	freopen("cuplaj.in","r",stdin);
	freopen("cuplaj.out","w",stdout);
	
	int edges, i, x, y;
	
	scanf("%d %d %d\n",&N,&M,&edges);
	while(edges--) {
		scanf("%d %d\n",&x,&y);
		G[x].push_back(y);
	}
	
	bool ok=1;
	while(ok) {
		ok=0;
		viz.reset();
		for(i=1; i<=N; i++)
			if(!L[i]) 
				if(cupleaza(i))
					++sol, ok = 1;
	}
	
	printf("%d\n",sol);
	for(i=1; i<=N; i++)
		if(L[i])
			printf("%d %d\n",i,L[i]);
	return 0;
}