Cod sursa(job #1437499)

Utilizator StefanRARapeanu-Andreescu Stefan StefanRA Data 17 mai 2015 20:45:20
Problema Cuplaj maxim in graf bipartit Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include <fstream>
#include <vector>
#define MAXN 10001
int n, m, e, x, y, left[MAXN], right[MAXN], k;
std::vector<int> graph[MAXN];
bool visited[MAXN], ok;
bool pairup(int vertex)
{
	if (visited[vertex])
		return false;
	visited[vertex]=true;
	for (int i=0;i<graph[vertex].size();++i)
	{
		int neighbour=graph[vertex][i];
		if (!right[neighbour] || pairup(neighbour))
		{
			left[vertex]=neighbour;
			right[neighbour]=vertex;
			return true;
		}
	}
	return false;
}
int main()
{
	int i;
	freopen("cuplaj.in", "r", stdin);
	freopen("cuplaj.out", "w", stdout);
	scanf("%d %d %d", &n, &m, &e);
	while (e--)
	{
		scanf("%d %d", &x, &y);
		graph[x].push_back(y);
	}
	ok=true;
	while (ok)
	{
		ok=false;
		for (i=1;i<=n;++i)
			visited[i]=false;
		for (i=1;i<=n;++i)
		{
			if (!left[i])
				if (pairup(i))
				{
					ok=true;
					k++;
				}
		}
	}
	printf("%d\n", k);
	for (i=1;i<=n;++i)
		if (left[i])
			printf("%d %d\n", i, left[i]);
	return 0;
}