Cod sursa(job #1267141)

Utilizator xtreme77Patrick Sava xtreme77 Data 19 noiembrie 2014 16:01:14
Problema Strazi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.03 kb
/*
 * 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;
}