Cod sursa(job #2737792)

Utilizator vlad082002Ciocoiu Vlad vlad082002 Data 5 aprilie 2021 10:04:36
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 fin("ciclueuler.in");
ofstream fout("ciclueuler.out");

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

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

void euler(int x) {
    while(g[x].size()) {
        auto next = g[x].back();
        g[x].pop_back();

        if(vis[next.second]) continue;
        vis[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(!v[i] || g[i].size()%2) {
            fout << -1 << '\n';
            return 0;
        }

    euler(1);
    ans.pop_back();
    for(auto x: ans) fout << x << ' ';
    fout << '\n';
}