Cod sursa(job #554493)

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

const int maxSize = 10;
const int oo = 0x3f3f3f3f;
const int source = 0;
const int sink = 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);
		//graph[to].push_back(from);
	}

	/*for(int i=1;i<=nrFirstSet;i++) {
		graph[source].push_back(i);
		graph[i].push_back(source);
	}

	for(int i=1;i<=nrSecondSet;i++) {
		graph[i].push_back(sink);
		graph[sink].push_back(i);
	}*/

	memset(matchLeft,-1,sizeof(matchLeft));
	memset(matchRight,-1,sizeof(matchRight));

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

	out << maxFlow << "\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] < 0 || match(matchRight[*it])) {
				matchLeft[node] = *it;
				matchRight[*it] = node;
				return true;
			}
		}

	return false;
}