Cod sursa(job #2662246)

Utilizator IRadu1529Radu Ionescu IRadu1529 Data 23 octombrie 2020 18:49:16
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <fstream>
#include <vector>
#include <iostream>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
#define N 10001
int n, m, e, cnt, viz[N], L[N], R[N];
vector<vector<int>> g(N, vector<int>());
bool cuplaj(int nod) // daca nod se poate cupla cu cineva
{
	if (viz[nod] == true)
		return false;
	viz[nod] = true;
	for (auto y : g[nod])
		if (!R[y] || cuplaj(R[y])) { // y nu a fost cuplat pana acum, y se afla in partea dreapta
			L[nod] = y;
			R[y] = nod;
			return true;
		}
	return false;
}
int main() {
	fin >> n >> m >> e;
	for (int i = 1; i <= e; ++i) {
		int x, y;
		fin >> x >> y;
		y += n;
		g[x].push_back(y);
	}
	bool ok = true;
	while (ok == true) { // cat timp se mai poate cupla un nod
		ok = false;
		memset(viz, 0, sizeof(viz));
		for (int i = 1; i <= n; ++i)
			if (!L[i] and cuplaj(i)) {
				cnt++;
				ok = true;
			}
	}
	fout << cnt << "\n";
	for (int i = 1; i <= n; ++i)
		if (L[i])
			fout << i << " " << L[i] << "\n";
}