Cod sursa(job #366215)

Utilizator adelinasAdelina Spataru adelinas Data 21 noiembrie 2009 11:26:21
Problema Cuplaj maxim in graf bipartit Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include <stdio.h>
#include <vector>
using namespace std;
#define FOR(i, V) for(vector<int>::iterator i = (V).begin(); i != (V).end(); i++ )
#define pb push_back
#define NMAX 10010
vector<int> v[NMAX];
int l[NMAX],r[NMAX],n,m,e,u[NMAX];
int pairup(int x){
	
	if(u[x]) return 0;
	u[x]=1;
	
	FOR(i, v[x])
		if(!r[*i]){
			l[x] = *i;
			r[*i] = x;
			return 1;
		}
	FOR(i ,v[x])
		if(pairup(r[*i])){
			l[x] = *i;
			r[*i] = x;
			return 1;
		}
	return 0;
}
int main(){
	int i;
	freopen("cuplaj.in","r",stdin);
	freopen("cuplaj.out","w",stdout);
	scanf("%d%d%d",&n,&m,&e);
	for(int i = 1; i <= e; ++i){
		int x, y;
		scanf("%d%d", &x, &y);
		v[x].pb(y);
	}
	int ok = 1;
	while( ok ){
		ok = 0;
		for(i = 1; i <= n; ++i)
			if (!l[i]){
				memset(u, 0, sizeof(u));
				ok |= pairup(i);
			}
	}
	int cuplaj = 0;
	for(i = 1; i <= n; ++i)
		cuplaj += (l[i] > 0);
	printf("%d\n", cuplaj);
	for(i = 1; i <= n; ++i)
		if(l[i]) printf("%d %d\n", i, l[i]);
	return 0;
}