Cod sursa(job #498431)

Utilizator vlad_DVlad Dumitriu vlad_D Data 5 noiembrie 2010 03:42:54
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <cstdio>
#include <vector>

using namespace std;

int n, m;
vector<vector<int> > G;
int L[10100], R[10100], viz[10100];
int pair_up(int nod) {
	if (viz[nod]) return 0;
	viz[nod] = 1;
	for (int i = 0; i < G[nod].size(); ++i) {
		int nod2 = G[nod][i];
		if (R[nod2] == 0 || pair_up(R[nod2])) {
			L[nod] = nod2; R[nod2] = nod;
			return 1;
		}
	}
	return 0;
}
int cuplaj() {
	int flux = 0;
	int lst = -1;
	while (lst != flux) {
		lst = flux;
		int ok = 0;
		// try to increase
		for (int i = 1; i <= n; ++i) viz[i] = 0;
		for (int i = 1; i <= n; ++i) if (L[i] == 0) {
			flux += pair_up(i);
		}
	}
	return flux;
}
int main() {
	freopen("cuplaj.in", "r", stdin);
	freopen("cuplaj.out", "w", stdout);

	int e;
	scanf("%d %d %d", &n, &m, &e);

	G.resize(n+1);
	for (int i = 0; i < e; ++i) {
		int a, b;
		scanf("%d %d", &a, &b);
		G[a].push_back(b);
	}
	printf("%d\n", cuplaj());
	for (int i = 1; i <= n; ++i) if (L[i])
		printf("%d %d\n", i, L[i]);
	return 0;
}