Cod sursa(job #1635520)

Utilizator DobosDobos Paul Dobos Data 6 martie 2016 18:29:39
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 100005;
vector < int > SOL,S;
list < int > G[NMAX];
int V[NMAX];
list < int > ::iterator it;
int main(){
    int n,m,x,y,nod,go;
    fin >> n >> m;
    for(int i = 1; i <=m; i++){
        fin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
        V[x] ++; V[y] ++;
    }
    for(int i = 1; i <= n; i++){
        if(V[i] % 2 != 0){
            fout << -1;
            return 0;
        }
    }
    S.push_back(1);
    while(!S.empty()){
        nod = S.back(); S.pop_back();
        while(!G[nod].empty()){
            go = G[nod].front();
            for( it = G[go].begin(); it != G[go].end(); it++){
                if(*it == nod){
                    G[go].erase(it);
                    break;
                }
            }
            S.push_back(nod);
            G[nod].pop_front();
            nod = go;
        }
        SOL.push_back(nod);
    }
    for(int i = 0; i < SOL.size(); i++)
        fout << SOL[i] << " ";
    return 0;
}