Cod sursa(job #2462830)

Utilizator gabib97Gabriel Boroghina gabib97 Data 27 septembrie 2019 21:09:45
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <bits/stdc++.h>
#define N 10005

using namespace std;

int n, m, e, l[N], r[N];
vector<int> G[N], pairs;
bool o[N];

int match(int s) {
	if (o[s]) return 0;
	o[s] = true;

	for (int x : G[s])
		if (!l[x]) {
			r[s] = x;
			l[x] = s;
			return 1;
		}
	for (int x : G[s])
		if (match(l[x])) {
			r[s] = x;
			l[x] = s;
			return 1;
		}
	return 0;
}

int main() {
	ifstream cin ("cuplaj.in");
	ofstream cout ("cuplaj.out");
	cin >> n >> m >> e;

	int x, y;
	for (int i = 0; i < e; i++) {
		cin >> x >> y;
		G[x].push_back(y);
	}

	int matchings;
	do {
		matchings = 0;
		memset(o, 0, sizeof(o));

		for (int i = 1; i <= n; i++)
			if (!r[i])
				matchings += match(i);
	} while (matchings);

	for (int i = 1; i <= n; i++)
		if (r[i]) pairs.push_back(i);
	cout << pairs.size();
	for (int p : pairs)
		cout << '\n' << p << " " << r[p];
	return 0;
}