Pagini recente » Cod sursa (job #1763733) | Istoria paginii utilizator/mario_deaconescu | Cod sursa (job #2893814) | Cod sursa (job #2853560) | Cod sursa (job #448933)
Cod sursa(job #448933)
/*
* File: main.cpp
* Author: virtualdemon
*
* Created on May 5, 2010, 6:23 AM
*/
#include <queue>
#include <cstdlib>
#include <fstream>
#define Nmax 10011
#define oo 0x3f3f3f3f
/*
*
*/
using namespace std;
int N;
int d[Nmax], L[Nmax], R[Nmax];
vector< int > G[Nmax];
vector< int >::const_iterator it, iend;
inline bool bfs( void )
{
int x;
queue< int > Q;
for( x=1; x <= N; ++x )
if( !L[x] )
{
d[x]=0;
Q.push(x);
}
else d[x]=oo;
d[0]=oo;
while( !Q.empty() )
{
x=Q.front(); Q.pop();
if( !x )
continue;
for( it=G[x].begin(), iend=G[x].end(); it < iend; ++it )
if( oo == d[R[*it]] )
{
d[R[*it]]=d[x]+1;
Q.push(R[*it]);
}
}
return d[0] != oo;
}
inline bool dfs( int x )
{
if( !x )
return true;
vector< int >::const_iterator it=G[x].begin(), iend=G[x].end();
for( ; it < iend; ++it )
if( d[R[*it]] == d[x]+1 && dfs(R[*it]) )
{
L[x]=*it;
R[*it]=x;
return true;
}
d[x]=oo;
return false;
}
int main(int argc, char** argv)
{
int M, E, x, y, nrc=0;
ifstream in( "cuplaj.in" );
for( in>>N>>M>>E; E; --E )
{
in>>x>>y;
G[x].push_back(y);
}
while( bfs() )
{
for( x=1; x <= N; ++x )
if( !L[x] && dfs(x) )
++nrc;
}
ofstream out( "cuplaj.out" );
out<<nrc<<'\n';
for( x=1; nrc && x <= N; ++x )
if( L[x] )
out<<x<<' '<<L[x]<<'\n', --nrc;
return (EXIT_SUCCESS);
}