Cod sursa(job #456599)

Utilizator alexandru92alexandru alexandru92 Data 16 mai 2010 10:23:27
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.33 kb
/* 
 * File:   main.cpp
 * Author: virtualdemon
 *
 * Created on May 16, 2010, 9:06 AM
 */
#include <stack>
#include <vector>
#include <cstdlib>
#include <fstream>
#include <iterator>
#include <algorithm>
#define Nmax 100011

/*
 * 
 */
using namespace std;
int d[Nmax], j;
bool uz[Nmax];
stack< int > S;
vector< int > euler;
vector< int > G[Nmax];
vector< int >::const_iterator it, iend;
inline void dfs( int x )
{
    static int w;
    while( !G[x].empty() )
    {
        w=*( G[x].end()-1 );
        if( !uz[w] )
            ++j, uz[w]=true;
        G[x].pop_back();
        G[w].erase( find( G[w].begin(), G[w].end(), x ) );
        S.push(x);
        x=w;
    }
}
int main( void )
{
    int N, M, x, y, par, imp;
    ifstream in( "ciclueuler.in" );
    ofstream out( "ciclueuler.out" );
    in>>N>>M;
    par=N; imp=0;
    for( ; M; --M )
    {
        in>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);
        ++d[x]; ++d[y];
    }
    for( x=1; x <= N; ++x )
        if( d[x]%2 )
        {
            out<<"-1\n";
            return EXIT_SUCCESS;
        }
    x=1;
    do
    {
        dfs( x );
        x=S.top(); S.pop();
        euler.push_back(x);
    }while( !S.empty() );
    if( N != j )
        out<<"-1";
    else copy( euler.rbegin(), euler.rend(), ostream_iterator<int>( out, " " ) );
    out<<'\n';
    return EXIT_SUCCESS;
}