Cod sursa(job #485327)

Utilizator raduzerRadu Zernoveanu raduzer Data 18 septembrie 2010 08:03:45
Problema Cuplaj maxim in graf bipartit Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <cstdio>
#include <vector>
using namespace std;

#define pb push_back
#define forit(it, v) for(__typeof(v.begin()) it = v.begin(); it != v.end(); ++it)

const int MAX_N = 10001;

int n, m, muc, sol;
vector <int> v[MAX_N];
int cuplat[MAX_N], tata[MAX_N];

int cupleaza(int nod)
{
	forit(it, v[nod])
		if (!tata[*it])
		{
			tata[*it] = nod;
			return cuplat[nod] = 1;
		}

	forit(it, v[nod])
		if (tata[*it] != nod && cupleaza(tata[*it]))
		{
			tata[*it] = nod;
			return cuplat[nod] = 1;
		}

	return 0;
}

int main()
{
	int i;
	freopen("cuplaj.in", "r", stdin);
	freopen("cuplaj.out", "w", stdout);

	scanf("%d %d %d", &n, &m, &muc);

	for (i = 1; i <= muc; ++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 (!cuplat[i])
			{
				int q = cupleaza(i);

				sol += q;
				ok |= q;
			}
	}

	printf("%d\n", sol);

	for (i = 1; i <= m; ++i)
		if (tata[i])
			printf("%d %d\n", tata[i], i);
}