Cod sursa(job #674148)

Utilizator pykhNeagoe Alexandru pykh Data 5 februarie 2012 18:06:48
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.15 kb
#include<cstdio>
#include<vector>
#include<bitset>
using namespace std;

const char in[]="cuplaj.in";
const char out[]="cuplaj.out";

const int N = 10005;

vector<int> G[N];
bitset<N>u;

int l[N], r[N], n, m, e, sol;


bool cuplaj(int x)
	{
		if(u[x])return false;
		u[x] = true;
		
		for(vector<int>::iterator it = G[x].begin() ; it != G[x].end() ; ++it)
		if(!r[*it])
		{
			r[*it] = x  ;
			l[ x ] = *it;
			return true;
		}
		
		for(vector<int>::iterator it = G[x].begin() ; it != G[x].end() ; ++it)
		if(cuplaj(r[*it]))
		{
			r[*it] = x  ;
			l[ x ] = *it;
			return true;
		}
			
		return false;
}
			


int main()
{
	freopen(in,"r",stdin);
	freopen(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);
		G[x].push_back(y);
	}
	
	for(bool ch = true ; ch ; )
	{
		u.reset();
		ch = false;
		for(int i = 1 ; i <= n ; ++i)
			if(!l[i])ch |= cuplaj(i);
	}
	
	for(int i = 1 ; i <= n ; ++i)
	if(l[i])++sol;
	printf("%d\n", sol);
	
	for(int i = 1 ; i <= n ; ++i)
		if(l[i])
			printf("%d %d\n", i, l[i]);
		
	return 0;
}