Pagini recente » Cod sursa (job #1364031) | Cod sursa (job #2823227) | Cod sursa (job #1021964) | Cod sursa (job #1047458) | Cod sursa (job #460183)
Cod sursa(job #460183)
#include <vector>
#include <cstdlib>
#include <fstream>
#include <algorithm>
#define Nmax 10111
/*
*
*/
using namespace std;
bool was[Nmax];
int L[Nmax], R[Nmax];
vector< int > G[Nmax];
inline bool PairUp( int x )
{
if( was[x] )
return false;
was[x]=true;
vector< int >::const_iterator it=G[x].begin(), iend=G[x].end();
for( ; it < iend; ++it )
if( !R[*it] || PairUp( R[*it] ) )
{
L[x]=*it;
R[*it]=x;
return true;
}
return false;
}
int main( void )
{
bool ok;
int N, M, E, x, y, nr=0;
ifstream in( "cuplaj.in" );
for( in>>N>>M>>E; E; --E )
{
in>>x>>y;
G[x].push_back(y);
}
do
{
ok=false;
for( x=1; x <= N; ++x )
if( !L[x] && PairUp(x) )
ok=true, ++nr;
fill( was, was+N+1, false );
}while( ok );
ofstream out( "cuplaj.out" );
out<<nr<<'\n';
for( x=1; x <= N; ++x )
if( L[x] )
out<<x<<' '<<L[x]<<'\n';
return EXIT_SUCCESS;
}