Cod sursa(job #2720492)

Utilizator mihai03Mihai Grigore mihai03 Data 10 martie 2021 21:38:27
Problema Ciclu Eulerian Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <bits/stdc++.h>
#define Nmax 100002
using namespace std;

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

int N, M;
vector<pair<int, int> > G[Nmax];
vector<int> ans;
bool vis[Nmax * 5];

void DFS(int node) {
    while (!G[node].empty()) {
        int nxt = G[node].back().first, edge = G[node].back().second;
        G[node].pop_back();
        DFS(nxt);
    }
    ans.push_back(node);
}
int main()
{
    fin >> N >> M;
    for (int i = 1; i <= M; ++i) {
        int x, y;
        fin >> x >> y;
        G[x].push_back({y, i});
        G[y].push_back({x, i});
    }

    for (int i = 1; i <= N; ++i)
    if (G[i].size() % 2) {
        fout << -1;
        return 0;
    }

    DFS(1);
    ans.pop_back();
    for (auto it: ans)
        fout << it << " ";

    return 0;
}