Cod sursa(job #2225126)

Utilizator AlexDabuDabu Alexandru AlexDabu Data 25 iulie 2018 23:17:36
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.25 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

#define NMAX 10005

using namespace std;

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

int l[NMAX], r[NMAX], n, m, e, matching = 0;
bool wasVisited[NMAX] = { false };
vector <int> bounds[NMAX];

void Read(void)
{
	int x, y;
	fin >> n >> m >> e;
	for (int i = 0; i < e; i++)
	{
		fin >> x >> y;
		bounds[x].push_back(y);
	}
}

int PairUp(int from)
{
	int to = 0;

	if (wasVisited[from])
	{
		return 0;
	}
	wasVisited[from] = 1;
	for (int i = 0; i < bounds[from].size(); i++)
	{
		to = bounds[from].at(i);
		if (!r[to])
		{
			l[from] = to;
			r[to] = from;
			return 1;
		}
		if (PairUp(r[to]))
		{
			l[from] = to;
			r[to] = from;
			return 1;
		}
	}
	return 0;
}

void GetMaxFlow(void)
{
	bool flag = 1;
	while (flag) 
	{
		memset(wasVisited, 0, sizeof wasVisited);
		flag = 0;
		for (int i = 1; i <= n; i++)
		{
			if (l[i] == 0 && PairUp(i))
			{
				flag = 1;
				matching++;
			}
		}
	}
}

void Print(void)
{
	fout << matching;
	for (unsigned int i = 1; i <= n; i++)
	{
		if (l[i] > 0)
			fout << '\n' << i << ' ' << l[i];
	}
}

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