Cod sursa(job #554530)

Utilizator feelshiftFeelshift feelshift Data 14 martie 2011 21:58:08
Problema Cuplaj maxim in graf bipartit Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
// http://infoarena.ro/problema/cuplaj
#include <fstream>
#include <bitset>
#include <vector>
using namespace std;

const int maxSize = 10001;

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

int matchLeft[maxSize];
int matchRight[maxSize];
bitset<maxSize> visit;
vector<int> graph[maxSize];

bool match(int node);

int main() {
	int leftSize,rightSize,edges;
	in >> leftSize >> rightSize >> edges;

	int from,to;
	for(int i=1;i<=edges;i++) {
		in >> from >> to;

		graph[from].push_back(to);
	}

	int maxFlow = 0;
	for(int i=1;i<=leftSize;i++) {
		visit.reset();
		if(match(i))
			maxFlow++;
	}

	out << maxFlow << "\n";
	for(int i=1;i<=leftSize;i++)
		if(matchLeft[i])
			out << i << " " << matchLeft[i] << "\n";

	return (0);
}

bool match(int node) {
	vector<int>::iterator it,end;
	end = graph[node].end();

	for(it=graph[node].begin();it!=end;++it)
		if(!visit[*it]) {
			visit[*it] = true;

			// daca nodul it nu este cuplat sau am reusit
			// sa-l cuplam (se incearca cuplarea si
			// in cazul in care nodul este cuplat)
			if(!matchRight[*it] || match(matchRight[*it])) {
				matchLeft[node] = *it;
				matchRight[*it] = node;
				return true;
			}
		}

	return false;
}