Cod sursa(job #2844970)

Utilizator sireanu_vladSireanu Vlad sireanu_vlad Data 6 februarie 2022 17:18:43
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.05 kb
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;

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

vector<int> G[10001];
int N, M, E, L[10001], R[10001];
bool Use[10001];

void Read()
{
	in >> N >> M >> E;
	for(int i = 1, x, y; i <= E; ++i)
	{
		in >> x >> y;
		G[x].push_back(y);
	}
}

bool Pairup(int Nod)
{
	if(Use[Nod]) return 0;
	Use[Nod] = 1;
	for(auto Vecin : G[Nod])
		if(!R[Vecin])
		{
			L[Nod] = Vecin;
			R[Vecin] = Nod;
			return 1;
		}
	for(auto Vecin : G[Nod])
		if(Pairup(R[Vecin]))
		{
			L[Nod] = Vecin;
			R[Vecin] = Nod;
			return 1;
		}
	return 0;
}

void Solve()
{
	bool Ok;
	do
	{
		Ok = 0;
		memset(Use, 0, sizeof(Use));
		for(int i = 1; i <= N; ++i)
			if(!L[i])
				Ok |= Pairup(i);
	}
	while(Ok);
}

void Print()
{
	int Cuplaj = 0;
	for(int i = 1; i <= N; ++i)
		if(L[i]) Cuplaj++;
	out << Cuplaj << '\n';
	for(int i = 1; i <= N; ++i)
		if(L[i])
			out << i << ' ' << L[i] << '\n';
}

int main()
{
	Read();
	Solve();
	Print();
	return 0;
}