Cod sursa(job #2665060)

Utilizator gavra_bogdanBogdan Gavra gavra_bogdan Data 29 octombrie 2020 22:58:42
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <fstream>
#include <vector>
#include <cstring>

const int nmax = 1e4 + 5;

std::vector<int>l[nmax];
int viz[nmax], st[nmax], dr[nmax], curr, prevcurr;

int cuplaj(int node) {
	if (viz[node] == true) return 0;
	viz[node] = true;
	for(int x:l[node])
		if (!dr[x]) {
			dr[x] = node, st[node] = x;
			return 1;
		}
	for(int x:l[node])
		if (cuplaj(dr[x])) {
			dr[x] = node, st[node] = x;
			return 1;
		}
	return 0;
}

int main() {
	std::ifstream fin("cuplaj.in");
	std::ofstream fout("cuplaj.out");
	int n, m, u, v, e;
	fin >> n >> m >> e;
	for (int i = 0; i < e; i++) {
		fin >> u >> v;
		l[u].push_back(v);
	}
	do {
		prevcurr = curr;
		memset(viz, 0, sizeof(viz));
		for (int i = 1; i <= n; i++) if(!st[i]) curr += cuplaj(i);
	} while (curr!=prevcurr);
	fout << curr << "\n";
	for (int i = 1; i <= n; i++)
		if (st[i]) fout << i << " " << st[i] << "\n";
}