Cod sursa(job #1557709)

Utilizator eu3neuomManghiuc Teodor-Florin eu3neuom Data 28 decembrie 2015 00:14:39
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMax = 1e5 + 5;

int Grad[NMax];

vector < int > A;
list < int > G[NMax];
stack < int > S;

int main(){
    int n, m, node, x, y, go;
    fin >> n >> m;
    while(m--){
        fin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
        Grad[x]++; Grad[y]++;
    }
    for(int i = 1; i <= n; i++){
        if(Grad[i] % 2){
            fout << -1;
            return 0;
        }
    }
    S.push(1);
    while(!S.empty()){
        node = S.top(); S.pop();
        while(!G[node].empty()){
            go = G[node].front();
            for(list < int > ::iterator it = G[go].begin(); it != G[go].end(); it++){
                if(*it == node){
                    G[go].erase(it);
                    break;
                }
            }
            G[node].pop_front();
            S.push(node);
            node = go;
        }
        A.push_back(node);
    }
    for(auto it: A) fout << it << " ";
    return 0;
}