Pagini recente » Cod sursa (job #301153) | Cod sursa (job #139247) | Cod sursa (job #1990724) | Cod sursa (job #742401) | Cod sursa (job #1267141)
/*
* Code by Spiromanul
*/
#include <fstream>
#include <vector>
#include <bitset>
#define pb push_back
const char IN [ ] = "strazi.in" ;
const char OUT [ ] = "strazi.out" ;
const int MAX = 260 ;
using namespace std;
ifstream fin ( IN ) ;
ofstream fout ( OUT ) ;
vector < int > gr [ MAX ] ;
bitset < MAX > viz ;
int unu [ MAX ] , doi [ MAX ] , rightt [ MAX ] , leftt [ MAX ] , n ;
inline bool cuplaj ( int nod )
{
if ( viz [ nod ] )
return 0 ;
viz [ nod ] = 1 ;
for ( auto x : gr [ nod ] )
if ( rightt [ x ] == 0 )
{
rightt [ x ] = nod ;
leftt [ nod ] = x ;
return 1 ;
}
for ( auto x : gr [ nod ] )
if ( cuplaj ( rightt [ x ] ) )
{
rightt [ x ] = nod ;
leftt [ nod ] = x ;
return 1 ;
}
return 0 ;
}
inline void do_the_little_job ( )
{
int ok ;
do {
ok = 0 ;
viz.reset ( ) ;
for ( int i = 1 ; i <= n ; ++ i )
if ( leftt [ i ] == 0 )
ok |= cuplaj ( i ) ;
}while ( ok ) ;
}
inline void rezolva ( )
{
int lant = 0 ;
for ( int i = 1 ; i <= n ; ++ i )
if ( rightt [ i ] == 0 )
{
int nod = i ;
while ( leftt [ nod ] )
nod = leftt [ nod ] ;
++ lant ;
unu [ lant ] = nod ;
doi [ lant ] = i ;
}
fout << -- lant << '\n' ;
for ( int i = 1 ; i <= lant ; ++ i ){
fout << doi [ i ] << ' ' << unu [ i + 1 ] << '\n' ;
rightt [ doi [ i ] ] = unu [ i + 1 ] ;
}
int start = unu [ 1 ] ;
while ( start )
{
fout << start << ' ' ;
start = rightt [ start ] ;
}
fout << '\n' ;
}
int main ( )
{
int m ;
fin >> n >> m ;
for ( ; m ; -- m )
{
int x , y ;
fin >> x >> y ;
gr [ y ].pb ( x ) ;
}
do_the_little_job ( ) ;
rezolva ( ) ;
return 0;
}