Cod sursa(job #1414170)

Utilizator darrenRares Buhai darren Data 2 aprilie 2015 13:39:03
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <cstring>
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

int N, M, E;
vector<int> V[10002];
int wh[2][10002];
bool S[10002];
int result;

bool match(int x)
{
	if (S[x]) return false;
	S[x] = true;
	
	for (auto nod : V[x])
		if (wh[1][nod] == 0 || match(wh[1][nod]))
		{
			wh[0][x] = nod;
			wh[1][nod] = x;
			return true;
		}
	return false;
}

int main()
{
	ifstream fin("cuplaj.in");
	ofstream fout("cuplaj.out");
	
	fin >> N >> M >> E;
	for (int i = 1, nod1, nod2; i <= E; ++i)
	{
		fin >> nod1 >> nod2;
		V[nod1].push_back(nod2);
	}
	
	bool change = true;
	while (change)
	{
		change = false;
		memset(S, 0, sizeof(S));
		
		for (int i = 1; i <= N; ++i)
			if (!wh[0][i] && match(i))
			{
				++result;
				change = true;
			}
	}
	
	fout << result << '\n';
	for (int i = 1; i <= N; ++i)
		if (wh[0][i] != 0)
			fout << i << ' ' << wh[0][i] << '\n';
	
	fin.close();
	fout.close();
}