Cod sursa(job #1339539)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 10 februarie 2015 23:01:07
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.5 kb
#include <fstream>
#include <algorithm>
#include <vector>

#define DIM 10005
#define vint vector<int>::iterator
#define infile "cuplaj.in"
#define outfile "cuplaj.out"

using namespace std;

ifstream f(infile);
ofstream g(outfile);

int nrNodesLeft, nrNodesRight, nrEdges;

bool viz[DIM];

int matchLeft[DIM], matchRight[DIM];

vector<int> Graph[DIM];

bool graphMatch(int node) {

	if (viz[node])
		return 0;

	viz[node] = true;

	for (vint it = Graph[node].begin(); it != Graph[node].end(); ++it) {

		if (!matchRight[*it]) {

			matchRight[*it] = node;
			matchLeft[node] = *it;
			return 1;

		}

	}

	for (vint it = Graph[node].begin(); it != Graph[node].end(); ++it) {

		if (graphMatch(matchRight[*it])) {

			matchRight[*it] = node;
			matchLeft[node] = *it;
			return 1;

		}

	}

	return 0;

}

int main() {

	f >> nrNodesLeft >> nrNodesRight >> nrEdges;

	for (int i = 1; i <= nrEdges; ++i) {

		int source, destination;

		f >> source >> destination;

		Graph[source].push_back(destination);

	}

	bool ok;

	int nrMatches = 0;

	do {

		ok = false;

		memset(viz, false, sizeof(viz));

		for (int i = 1; i <= nrNodesLeft; ++i) {
			if (!matchLeft[i] && graphMatch(i)) {

				++nrMatches;
				ok = true;

			}
		}

	} while (ok);

	g << nrMatches << '\n';

	for (int i = 1; i <= nrNodesLeft; ++i) {

		if (!matchLeft[i])
			continue;

		g << i << ' ' << matchLeft[i] << '\n';

	}

	return 0;
}

//Trust me, I'm the Doctor!