Cod sursa(job #2738647)

Utilizator vlad082002Ciocoiu Vlad vlad082002 Data 6 aprilie 2021 10:23:37
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <bits/stdc++.h>
using namespace std;

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

int n, m;
vector<pair<int , int> > g[100005];
vector<int> ans;
bool vn[100005], ve[500005];

void dfs(int x) {
    vn[x] = true;
    for(auto next: g[x])
        if(!vn[next.first])
            dfs(next.first);
}

void euler(int x) {
    while(g[x].size()) {
        auto next = g[x].back();
        g[x].pop_back();
        if(ve[next.second]) continue;
        ve[next.second] = true;
        euler(next.first);
    }
    ans.push_back(x);
}

int main() {
    fin >> n >> m;
    while(m--) {
        int u, v;
        fin >> u >> v;
        g[u].push_back({v, m});
        g[v].push_back({u, m});
    }
    dfs(1);
    for(int i = 1; i <= n; i++)
        if(g[i].size()%2 || !vn[i]) {
            fout << -1 << '\n';
            return 0;
        }
    euler(1);
    ans.pop_back();
    for(auto x: ans) fout << x << ' ';
}