Cod sursa(job #1379980)

Utilizator alex_unixPetenchea Alexandru alex_unix Data 6 martie 2015 20:49:21
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb

#include <fstream>
#include <vector>
#include <bitset>

const int MAX_N(10001);

int n, m, Matching;
std::vector<int> Graph [MAX_N * 2];
int Left [MAX_N], Right [MAX_N * 2];
std::bitset<MAX_N * 2> Mark;

inline void Read (void)
{
	std::ifstream input("cuplaj.in");
	int e;
	input >> n >> m >> e;
	int x, y;
	while (e)
	{
		input >> x >> y;
		y += n;
		Graph[x].push_back(y);
		Graph[y].push_back(x);
		--e;
	}
	input.close();
}

inline void Print (void)
{
	std::ofstream output("cuplaj.out");
	output << Matching << '\n';
	for (int i(1) ; i <= n ; ++i)
		if (Left[i])
			output << i << ' ' << Left[i] - n << '\n';
	output.close();
}

bool DepthFirstSearch (int node)
{
	if (Mark[node])
		return false;
	Mark[node] = true;
	for (unsigned int i(0) ; i < Graph[node].size() ; ++i)
		if (!Right[Graph[node][i]] || DepthFirstSearch(Right[Graph[node][i]]))
		{
			Left[node] = Graph[node][i];
			Right[Graph[node][i]] = node;
			return true;
		}
	return false;
}

void HopcroftKarp (void)
{
	bool ok;
	do
	{
		ok = false;
		Mark.reset();
		for (int i(1) ; i <= n ; ++i)
			if (!Left[i] && DepthFirstSearch(i))
			{
				ok = true;
				++Matching;
			}
	}
	while (ok);
}

int main (void)
{
	Read();
	HopcroftKarp();
	Print();
	return 0;
}