Cod sursa(job #2178975)

Utilizator cyber_ghSoltan Gheorghe cyber_gh Data 19 martie 2018 21:11:01
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include <bits/stdc++.h>

using namespace std;

const int NMAX = 100010, MMAX = 500010;

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

vector<int> V[NMAX];
int F[MMAX],T[MMAX];
int N,M;
bool used[MMAX];

int main(){

    fin >> N >> M;
    for (int i = 0; i < M;i++){
        int x,y;
        fin >> x >> y;
        V[x].push_back(i);
        V[y].push_back(i);
        F[i] = x;
        T[i] = y;
    }

    for(int i = 1; i <= N;i++) if (V[i].size()&1) return fout <<-1,0;

    vector<int> ans;

    vector<int> stk;

    stk.push_back(1);
    while(!stk.empty()){
        int node = stk.back();
        //cout << node << " ";
        if (!V[node].empty()){
            int e = V[node].back();
            V[node].pop_back();
            if (!used[e]){
                used[e] = 1;
                int to = ::F[e] ^ ::T[e] ^ node;
                stk.push_back(to);
            }
        }
        else{
                stk.pop_back();
                ans.push_back(node);
            }

    }
    for (auto it:ans) fout << it << " ";
    return 0;
}