Pagini recente » Istoria paginii runda/preoji_4 | Cod sursa (job #2906605) | Cod sursa (job #1163812) | Cod sursa (job #2542505) | Cod sursa (job #2269363)
#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;
}