Cod sursa(job #1318203)

Utilizator vladrochianVlad Rochian vladrochian Data 15 ianuarie 2015 19:00:14
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <fstream>
#include <cstring>
#include <vector>
using namespace std;

const int kMaxN = 10005;

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

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

inline void Connect(int a, int b) {
	l[a] = b;
	r[b] = a;
}

bool Link(int node) {
	if (use[node])
		return false;
	use[node] = true;
	for (int i : G[node])
		if (!r[i]) {
			Connect(node, i);
			return true;
		}
	for (int i : G[node])
		if (Link(r[i])) {
			Connect(node, i);
			return true;
		}
	return false;
}

void Read() {
	fin >> N >> M >> E;
	while (E--) {
		int x, y;
		fin >> x >> y;
		G[x].push_back(y);
	}
}

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

void Print() {
	fout << sol << "\n";
	for (int i = 1; i <= N; ++i)
		if (l[i])
			fout << i << " " << l[i] << "\n";
}

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