Pagini recente » Cod sursa (job #1541091) | Istoria paginii utilizator/cruceruvlad | Cod sursa (job #1851285) | Cod sursa (job #2145936) | Cod sursa (job #554181)
Cod sursa(job #554181)
#include <vector>
#include <fstream>
#include <cstdlib>
#define N_MAX 10011
#define oo 1<<20
using namespace std;
int N, M;
int L[N_MAX], R[N_MAX];
vector< int > G[N_MAX], v;
vector< bool > was;
inline bool Check( 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] || Check( R[*it] ) )
{
L[x]=*it;
R[*it]=x;
return true;
}
return false;
}
int main( void )
{
int E, x, y, maxMatch=0;
vector< int >::const_iterator it, iend;
ifstream in( "cuplaj.in" );
for( in>>N>>M>>E; E; --E )
{
in>>x>>y;
G[x].push_back(y);
v.push_back(x);
}
iend=v.end();
was.resize( N+1 );
for( it=v.begin(); it < iend; ++it )
if( !L[*it] )
{
was.assign( N+1, false );
if( Check(*it) )
++maxMatch;
}
ofstream out( "cuplaj.out" );
out<<maxMatch<<'\n';
for( it=v.begin(); it < iend && maxMatch; ++it )
if( L[*it] )
{
out<<*it<<' '<<L[*it]<<'\n';
--maxMatch;
}
return EXIT_SUCCESS;
}