Cod sursa(job #303541)

Utilizator peanutzAndrei Homorodean peanutz Data 9 aprilie 2009 23:01:58
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include <stdio.h>
#include <vector>
#include <string.h>

using namespace std;

#define NMAX 10010

int L[NMAX], R[NMAX];
vector<int> g[NMAX];
int n, m, e;
short uz[NMAX];

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

short pair_up(int x)
{
	++uz[x];
	for(vector<int> :: iterator it = g[x].begin(); it != g[x].end(); ++it)
		if(!R[*it])
		{
			R[*it] = x;
			L[x] = *it;
			return 1;
		}
	for(vector<int> :: iterator it = g[x].begin(); it != g[x].end(); ++it)
		if(!uz[ R[*it] ] && pair_up(R[*it])) 
		{
			R[*it] = x;
			L[x] = *it;
			return 1;
		}
	return 0;
}

int main()
{
	freopen("cuplaj.in", "r", stdin);
	freopen("cuplaj.out", "w", stdout);
	
	read();
	
	for(short ok = 1; ok;)
	{
		memset(uz, 0, sizeof(uz));
		ok = 0;
		for(int i = 1; i <= n; ++i)
			if(!L[i])
				ok |= pair_up(i);
	}
	int res = 0;
	for(int i = 1; i <= n; ++i)
		if(L[i])
			++res;
	printf("%d\n", res);
	for(int i = 1; i <= n; ++i)
			if(L[i])
				printf("%d %d\n", i, L[i]);
	
	return 0;
}