Cod sursa(job #1610400)

Utilizator xtreme77Patrick Sava xtreme77 Data 23 februarie 2016 15:08:33
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.93 kb
/**
 * Code by Patrick Sava
 * "Spiru Haret" National College of Bucharest
 **/

# include "fstream"
# include "cstring"
# include "vector"
# include "queue"
# include "bitset"
# include "algorithm"
# include "map"
# include "set"
# include "unordered_map"
# include "deque"
# include "string"
# include "iomanip"
# include "cmath"
# include "stack"
# include "cassert"

const char IN [ ] =  "ciclueuler.in" ;
const char OUT [ ] = "ciclueuler.out" ;

using namespace std ;

# define pb push_back
# define mp make_pair
# define FORN( a , b , c ) for ( register int a = b ; a <= c ; ++ a )
# define FORNBACK( a , b , c ) for ( register int a = b ; a >= c ; -- a )

ifstream cin ( IN ) ;
ofstream cout ( OUT ) ;

const int MAX = 1e5 + 14 ;
const int EDGE = 5e5 + 14 ;

bitset < EDGE > viz ;

vector < pair < int , int > > gr [ MAX ] ;

int p [ MAX ] ;

stack < int > st ;

vector < int > sol ;

int grad [ MAX ] ;

int main()
{
    int n , m ;
    cin >> n >> m ;
    FORN ( i , 1 , m )
    {
        int x , y ;
        cin >> x >> y ;
        gr [ x ].pb ( mp ( y , i ) ) ;
        gr [ y ].pb ( mp ( x , i ) ) ;
        ++ grad [ x ] ;
        ++ grad [ y ] ;
    }
    FORN ( i , 1 , n )
        if ( grad [ i ] & 1 )
        {
            cout << -1 ;
            exit ( 0 ) ;
        }
    st.push ( 1 ) ;
    while ( !st.empty ( ) )
    {
        int nod = st.top() ;
        while ( p [ nod ] < ( int ) gr [ nod ].size ( ) and viz [ gr [ nod ] [ p [ nod ] ].second ] == 1 )
            ++ p [ nod ] ;
        if ( p [ nod ] == ( int ) gr [ nod ].size ( ) )
        {
            sol.pb ( nod ) ;
            st.pop ( ) ;
            continue ;
        }
        viz [ gr [ nod ] [ p [ nod ] ].second ] = 1 ;
        st.push ( gr [ nod ] [ p [ nod ] ].first ) ;
        ++ p [ nod ] ;
    }
    sol.pop_back() ;
    for ( auto x : sol )
        cout << x << ' ' ;
    return 0;
}