Cod sursa(job #696984)

Utilizator informatician28Andrei Dinu informatician28 Data 28 februarie 2012 21:21:22
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include<fstream> 
#include<stack> 
#include<vector> 
#define DIM 100001
#define pb push_back
using namespace std; 

ifstream in("ciclueuler.in");
ofstream out("ciclueuler.out");

int N, M;
stack< int > St;
vector< int > G[DIM], sol;

int main()
{
    int i, x, y;
    
    in >> N >> M;
    for(i = 1; i <= M; i++)
    {
        in >> x >> y;
        G[x].pb(y); 
        G[y].pb(x);
    }
    for(i = 1; i <= N; i++)
        if( G[i].size() & 1 )
            out << "-1";
    
    for( St.push(1); !St.empty(); )
    {
        int x = St.top();
        
        if( G[x].size() == 0 )
        {
            sol.pb(x); 
            St.pop();
            continue;
        }
        
        int y = G[x].back(); 
        G[x].pop_back();
        St.push(y); 
        
        for(vector< int > ::iterator it = G[y].begin(); it != G[y].end(); ++it)
            if( *it == x )
            {
                G[y].erase(it);
                break;
            }
    }
    for(size_t i = 0; i < sol.size()-1; i++)
        out << sol[i] << " ";
    return 0;
}