Cod sursa(job #2405289)

Utilizator Dobricean_IoanDobricean Ionut Dobricean_Ioan Data 14 aprilie 2019 11:52:13
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <cstring>
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

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

using namespace std;

const int Dim = (1e5+ 5) * 2;
vector < int > G[Dim];
int n,m,e,L[Dim],R[Dim],maxmat,Viz[Dim];

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


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