Cod sursa(job #751414)

Utilizator mihaibogdan10Mihai Bogdan mihaibogdan10 Data 26 mai 2012 05:21:49
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include<fstream>
#include<vector>
#include<algorithm>
using namespace std;
ifstream fin("cuplaj.in"); ofstream fout("cuplaj.out");

vector <int> G[10001];
int n, m, e, L[10001], R[10001], viz[10001];

int pairUp(int nod){
	if (viz[nod]) return 0;
	viz[nod] = 1;
	
	for (int i = 0, ad; i < (int) G[nod].size() && (ad = G[nod][i]); i++)
		if (!L[ad] || pairUp(L[ad])){
			R[nod] = ad, L[ad] = nod;
			return 1;
		}
	return 0;
}

int main(){
	int i, cuplez = 1, nr = 0, x, y;
	
	fin >> n >> m >> e;
	for (i = 0; i < e; i++){
		fin >> x >> y;
		G[x].push_back(y);
	}
	
	while(cuplez){
		cuplez = 0;
		memset (viz, 0, sizeof(viz));
		for (i = 1; i <= n; i++)
			if (!R[i] && pairUp(i))
				cuplez = 1, nr ++;
	}
	
	fout << nr << "\n";
	for (i = 1; i <= m; i++)
		if (L[i]) fout << L[i] << " " << i << "\n";
}