Cod sursa(job #2386868)

Utilizator Dobricean_IoanDobricean Ionut Dobricean_Ioan Data 23 martie 2019 19:51:39
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <cstring>
#include <vector>
#include <fstream>

using namespace std;

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

const int Dim = 10001;
int n,m,e,L[Dim],R[Dim],maxmat,Viz[Dim];

using VI = vector < int >;
using VVI = vector < VI >;
bool Cupleaza(int nod);

VVI G;

int main() {

	fin >> n >> m >> e;
	G = VVI( n + 1);
	int x,y;
	for (; e > 0; --e)  {
		fin >> x >> y;
		G[x].push_back(y);
	}
	bool found = true;
	while ( found) {
		found = false;
		memset(Viz,0,sizeof(Viz));
		for ( int i = 1; i <= n; ++i)
			if ( !L[i] and Cupleaza(i) )
				found = true,++maxmat;
	}
	fout << maxmat << "\n";
	for ( int i = 1; i <= n; ++i)
		if (L[i] )
		fout << i  << " " << L[i] << "\n";

}

bool Cupleaza(int nod) {
	if ( Viz[nod]) return false;
	Viz[nod] = 1;
	for ( const int & y : G[nod])
		if  (!R[y] or Cupleaza(R[y])) {
			L[nod] = y;
			R[y] = nod;
			return true;
		}
	return false;
}