Cod sursa(job #2464891)

Utilizator aurelionutAurel Popa aurelionut Data 29 septembrie 2019 01:24:23
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <fstream>
#include <vector>
#include <bitset>

using namespace std;

const int NMAX = 10005;
int n, m, e;
vector <int> graph[NMAX];
bitset <NMAX> seen;
int stg[NMAX], drp[NMAX];

bool Cuplaj(int node)
{
	//sunt in cuplajul curent
	if (seen[node] == 1)
		return false;
	seen[node] = 1;

	//incerc cu un nod liber
	for (auto &x : graph[node])
		if (drp[x] == 0)
		{
			stg[node] = x;
			drp[x] = node;
			return true;
		}

	//niciun nod liber, incerc sa decuplez
	for (auto &x : graph[node])
		if (Cuplaj(drp[x]))
		{
			stg[node] = x;
			drp[x] = node;
			return true;
		}
	return false;
}

int main()
{
	ifstream fin("cuplaj.in");
	ofstream fout("cuplaj.out");
	fin >> n >> m >> e;
	while (e-- > 0)
	{
		int x, y;
		fin >> x >> y;
		graph[x].push_back(y);
	}
	bool done = false;
	int cnt = 0;
	while (!done)
	{
		seen.reset();
		done = true;
		for (int i = 1;i <= n;++i)
		{
			if (stg[i] == 0 && Cuplaj(i))
			{
				++cnt;
				done = false;
			}
		}
	}
	fout << cnt << "\n";
	for (int i = 1;i <= n;++i)
		if (stg[i] != 0)
			fout << i << " " << stg[i] << "\n";
	fin.close();
	fout.close();
	return 0;
}