Cod sursa(job #1394623)

Utilizator taigi100Cazacu Robert taigi100 Data 20 martie 2015 15:06:30
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.15 kb
/*
	Keep It Simple!
*/

#include <fstream>
#include <vector>
#include <cstring>

using namespace std;

const int kMaxN = 10005;

ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");

int N, M, E;
vector<int> G[kMaxN];
bool used[kMaxN];
int l[kMaxN], r[kMaxN];

void ReadInput()
{
	fin >> N >> M >> E;

	for (int i = 1; i <= E; ++i)
	{
		int x, y;
		fin >> x >> y;
		G[x].push_back(y);
	}
}

bool PairUp(int node)
{
	if (used[node]) return false;
	used[node] = true;

	for (auto i : G[node])  // direct pairup
		if (!r[i])
		{
			l[node] = i;
			r[i] = node;
			return true;
		}

	for (auto i : G[node])
		if (PairUp(r[i]))
		{
			l[node] = i;
			r[i] = node;
			return true;
		}
	return false;
}

void Solve()
{
	ReadInput();

	bool ok = true;
	while (ok)
	{
		memset(used, 0, sizeof used);
		ok = false;
		for (int i = 1; i <= N; ++i)
			if (!l[i])
				ok |= PairUp(i);
	}

	int ans = 0;
	for (int i = 1; i <= N; ++i)
		ans += (l[i] > 0);
	fout << ans << '\n';
	for (int i = 1; i <= N; ++i)
		if (l[i])
			fout << i << ' ' << l[i] << '\n';


	return;
}

int main()
{
	Solve();
	return 0;
}