Cod sursa(job #3226461)

Utilizator Toni07Stoica Victor Toni07 Data 21 aprilie 2024 15:13:46
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f ("ciclueuler.in");
ofstream g ("ciclueuler.out");

const int nmax = 1e5 + 1, mmax = 5 * nmax;
bool viz[mmax];
vector <pair <int, int>> L[nmax];
vector <int> ans;

void dfs (int nod){
    while (!L[nod].empty()) {
        int son = L[nod].back().first, indx =L[nod].back().second;
        L[nod].pop_back();
        if (viz[indx]) continue;
        viz[indx] = true;
        dfs(son);
    }
    ans.push_back(nod);
}
signed main ()
{
    int n, m;
    f >> n >> m;
    for (int i = 1; i <= m; i++) {
        int x, y;
        f >> x >> y;
        L[x].push_back({y, i});
        L[y].push_back({x, i});
    }
    for (int i = 1; i <= n; i++){
        if (L[i].size() % 2 == 1){
            g << -1;
            return 0;
        }
    }
    dfs(1);
    ans.pop_back();
    for (auto i : ans)
        g << i << " ";
    return 0;
}