Cod sursa(job #2216760)

Utilizator flibiaVisanu Cristian flibia Data 27 iunie 2018 20:19:30
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include <bits/stdc++.h>

using namespace std;

ifstream in("cuplaj.in");
ofstream out("cuplaj.out");

int n, m, e, x, y, cnt, L[10010], R[10010];
vector <int> v[10010];
bool viz[10010];

bool OMG_I_HAVE_A_PARTNER(int from){
	if(viz[from])
		return 0;
	viz[from] = 1;
	for(auto to : v[from]){
		if(R[to] == 0){
			L[from] = to;
			R[to] = from;
			return 1;
		}
		if(OMG_I_HAVE_A_PARTNER(R[to]) == 1){
			L[from] = to;
			R[to] = from;
			return 1;
		}
	}
	return 0;
}

int main(){
	in >> n >> m >> e;
	for(int i = 1; i <= e; i++){
		in >> x >> y;
		v[x].push_back(y);
	}
	bool flag = 1;
	while(flag){
		memset(viz, 0, sizeof viz);
		flag = 0;
		for(int i = 1; i <= n; i++)
			if(L[i] == 0 && OMG_I_HAVE_A_PARTNER(i)){
				flag = 1;
				cnt++;
			}
	}
	out << cnt;
	for(int i = 1; i <= n; i++)
		if(L[i] != 0)
			out << '\n' << i << ' ' << L[i];
	return 0;
}