Cod sursa(job #554499)

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

const int maxSize = 10001;

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

int nrFirstSet,nrSecondSet,edges;
bool visit[maxSize];
int matchLeft[maxSize];
int matchRight[maxSize];
vector<int> graph[maxSize];

bool match(int node);

int main() {
	in >> nrFirstSet >> nrSecondSet >> edges;

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

		graph[from].push_back(to);
	}

	memset(matchLeft,0,sizeof(matchLeft));
	memset(matchRight,0,sizeof(matchRight));

	int maxFlow = 0;
	for(int i=1;i<=nrFirstSet;i++) {
		memset(visit,false,sizeof(visit));
		if(match(i))
			maxFlow++;
	}

	out << maxFlow << "\n";
	for(int i=1;i<=nrFirstSet;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;

			if(!matchRight[*it] || match(matchRight[*it])) {
				matchLeft[node] = *it;
				matchRight[*it] = node;
				return true;
			}
		}

	return false;
}