Cod sursa(job #2560754)

Utilizator PatrascuAdrian1Patrascu Adrian Octavian PatrascuAdrian1 Data 28 februarie 2020 11:20:26
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.98 kb
#include <bits/stdc++.h>

using namespace std;

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

const int Nmax = 1e5 + 5;
bool vis[Nmax];
stack <int> S;
list <int> G[Nmax];
vector<int> ans;

void dfs(int node) {
    S.push(node);
    while(!S.empty()) {
        int x = S.top();
        if(G[x].empty()) {
            ans.push_back(x);
            S.pop();
        } else {
            int y = G[x].front();
            G[x].pop_front();
            S.push(y);
            G[y].erase(find(G[y].begin(),G[y].end(),x));
        }
    }
}
int main() {
    ios_base::sync_with_stdio(0);
    in.tie(nullptr),out.tie(0);
    int N,M;
    in >> N >> M;
    int x,y;
    while(M--) {
        in >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    for(int i = 1; i <= N; ++i)
        if(G[i].size() & 1)
    {
        out << -1;
        return 0;
    }
    dfs(1);
    for(auto i : ans)
        out << i << " ";
    return 0;
}