Cod sursa(job #546716)

Utilizator andrei.dAndrei Diaconeasa andrei.d Data 5 martie 2011 13:53:04
Problema Cuplaj maxim in graf bipartit Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include <cstdio>
#include <vector>

using namespace std;

#define file_in "cuplaj.in"
#define file_out "cuplaj.out"

#define nmax 101000

int n,m,e,st[nmax];
int dr[nmax];
int viz[nmax];
int ans;
int a,b;
int i;
vector<int> G[nmax];

inline int cupleaza(int nod){
	
	if (viz[nod])
		return 0;
	
	viz[nod]=1;
	
	vector<int> :: iterator it;
	
	for (it=G[nod].begin();it!=G[nod].end();++it)
		if (!st[*it]){
			
			st[*it]=nod;
			dr[nod]=*it;
			return 1;
		}
		
	for (it=G[nod].begin();it!=G[nod].end();++it)
		if (cupleaza(st[*it])){
			
			st[*it]=nod;
			dr[nod]=*it;
			return 1;
		}
	return 0;

}
			

int main(){
	
	freopen(file_in,"r",stdin);
    freopen(file_out,"w",stdout);
	
	scanf("%d %d %d", &n, &m,&e);
	
	while(e--){
		
		scanf("%d %d", &a, &b);
		
		G[a].push_back(b);
		//G[b].push_back(a);
		
	}
	
	
	int ok=0;
	
	while(!ok){
		
		ok=1;
		memset(viz,0,sizeof(viz));
		
		for (i=1;i<=n;++i)
			if (!dr[i])
				if (cupleaza(i))
					ok=0;
	}
	
	for (i=1;i<=n;++i)
		 if (dr[i])
	      ans++;
		 
	printf("%d\n", ans);

	for (i=1;i<=n;++i)
		 if (dr[i])
			 printf("%d %d\n", dr[i], i);
	
	return 0;

}