Cod sursa(job #2269363)

Utilizator Dobricean_IoanDobricean Ionut Dobricean_Ioan Data 25 octombrie 2018 21:51:55
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 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 >;

VVI G;
bool Cupleaza(int x);

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_path = true;
	while ( found_path ) {
		found_path = false;
		memset(Viz,0,sizeof(Viz));
		for ( int i = 1; i <= n; ++i)
			if ( !L[i] and Cupleaza(i))
				++maxmat, found_path = true;
	}
	fout << maxmat << "\n";
	 for ( int i = 1; i <= n; ++i)
        if (L[i])
            fout << i << " " << L[i] << "\n";
}

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