Pagini recente » Cod sursa (job #992363) | Cod sursa (job #364202) | Cod sursa (job #3040902) | Cod sursa (job #1955355) | Cod sursa (job #582269)
Cod sursa(job #582269)
#include <bitset>
#include <vector>
#include <fstream>
#include <cstdlib>
#define N_MAX 10011
using namespace std;
int L[N_MAX], R[N_MAX];
bitset< N_MAX > was;
vector< int > G[N_MAX];
inline bool PairUp( int x )
{
if( was.test(x) )
return false;
was.set(x);
vector< int >::const_iterator it=G[x].begin(), iend=G[x].end();
for( ; it < iend; ++it )
{
if( !R[*it] || PairUp(R[*it]) )
{
R[*it]=x;
L[x]=*it;
return true;
}
}
return false;
}
int main( void )
{
bool ok;
int N, M, E, x, y;
ifstream in( "cuplaj.in" );
for( in>>N>>M>>E; E; --E )
{
in>>x>>y;
G[x].push_back(y);
}
y=0;
do
{
ok=false;
for( x=1; x <= N; ++x )
if( !L[x] && PairUp(x) )
ok=true, ++y;
was&=0;
}while( ok );
ofstream out( "cuplaj.out" );
out<<y<<'\n';
for( x=1; x <= N && y; ++x )
if( L[x] )
out<<x<<' '<<L[x]<<'\n', --y;
}