Pagini recente » Cod sursa (job #1589180) | Cod sursa (job #2962438) | Cod sursa (job #1526813) | Cod sursa (job #1010085) | Cod sursa (job #2855463)
#include <iostream>
#include <fstream>
#include <vector>
#define pb push_back
#define nl '\n'
using namespace std;
ifstream f ( "cuplaj.in" );
ofstream g ( "cuplaj.out" );
const int NMAX = 10001;
vector<int>G[NMAX];
int N, M, E, L[NMAX], R[NMAX], maxMatch;
bool viz[NMAX];///nodurile folosite intr-o iteratie
void citire()
{
int x, y;
f >> N >> M >> E;
while ( E-- )
{
f >> x >> y;
G[x].pb ( y );
}
}
bool match ( int x )
{
if ( viz[x] )
return 0;
viz[x] = 1;
for ( auto &y : G[x] )
if ( R[y] == 0 )
{
L[x] = y;
R[y] = x;
return 1;
}
for ( auto &y : G[x] )
if ( match ( R[y] ) )
{
L[x] = y;
R[y] = x;
return 1;
}
return 0;
}
void HK()
{
bool wasChanged = 1;
int i;
while ( wasChanged )
{
wasChanged = 0;
for ( i = 1; i <= N; i++ )
viz[i] = 0;
for ( i = 1; i <= N; i++ )
if ( !L[i] && match ( i ) )
{
wasChanged = 1;
maxMatch++;
}
}
}
int main()
{
citire();
HK();
g << maxMatch << nl;
for ( int i = 1; i <= N; i++ )
{
if ( L[i] != 0 )
g << i << ' ' << L[i] << nl;
}
f.close();
g.close();
return 0;
}