Cod sursa(job #2744444)

Utilizator gavra_bogdanBogdan Gavra gavra_bogdan Data 24 aprilie 2021 18:29:39
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <fstream>
#include <vector>
#include <cstring>

const int N = 1e5 + 5;

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

std::vector<int>l[N];
int left[N], right[N];
bool viz[N];

bool dfs(int node) {
	viz[node] = true;
	for(int to:l[node]) if(!left[to] or(!viz[left[to]] and dfs(left[to]))) {
		left[to] = node, right[node] = to;
		return 1;
	}
	return 0;
}

int main() {
	int n, m, k, ans = 0;
	fin>>n>>m>>k;
	for(int i=1;i<=k;i++) {
		int from, to;
		fin>>from>>to;
		l[from].push_back(to);
	}
	bool changed = true;
	while(changed) {
		changed = false;
		memset(viz, 0, sizeof(viz));
		for(int i=1;i<=n;i++) if(!right[i] and !viz[i] and dfs(i)) changed = true, ans++;
	}
	fout<<ans<<"\n";
	for(int i=1;i<=n;i++) if(right[i]) fout<<i<<" "<<right[i]<<"\n";
}