Pagini recente » Cod sursa (job #1632455) | Cod sursa (job #1675813) | Cod sursa (job #2148341) | Cod sursa (job #2217727) | Cod sursa (job #576702)
Cod sursa(job #576702)
#include <bitset>
#include <vector>
#include <fstream>
#include <cstdlib>
#define N_MAX 10011
using namespace std;
int L[N_MAX], R[N_MAX], SM[N_MAX];
bitset< N_MAX > wasVist;
vector< int > G[N_MAX];
inline bool PairUp( int x )
{
if( wasVist.test(x) )
return false;
wasVist.flip(x);
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, _count=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) )
++_count, ok=true;
wasVist&=0;
}while( ok );
ofstream out( "cuplaj.out" );
out<<_count<<'\n';
for( x=1; x <= N && _count; ++x )
if( L[x] )
out<<x<<' '<<L[x]<<'\n', --_count;
return EXIT_SUCCESS;
}