Cod sursa(job #2728457)

Utilizator vlad082002Ciocoiu Vlad vlad082002 Data 23 martie 2021 12:01:19
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");

vector<pair<int ,int> > g[100005];
int n, m;
bool v1[100005], v2[500005];
vector<int> cycle;

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

void euler(int x) {
    while(g[x].size()) {
        auto next = g[x].back();
        g[x].pop_back();
        if(v2[next.second]) continue;
        v2[next.second] = true;
        euler(next.first);
    }
    cycle.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(!v1[i] || g[i].size()%2) {
            fout << -1;
            return 0;
    }
    euler(1);
    cycle.pop_back();
    for(auto x: cycle) fout << x << ' ';
}