Cod sursa(job #3268041)

Utilizator Manolea_Teodor_StefanManolea Teodor Stefan Manolea_Teodor_Stefan Data 13 ianuarie 2025 16:07:41
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <bits/stdc++.h>

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

array<list<int>,100001> graph;
vector<int> ans;

array<int,100001> in,out;

stack<int> stk;
void solve(int node) {
    stk.push(node);
    while (!stk.empty()) {
        int x = stk.top();
        if (graph[x].empty()) {
            ans.push_back(x);
            stk.pop();
        } else {
            int y = graph[x].front();
            graph[y].erase(find(graph[y].begin(),graph[y].end(),x));
            graph[x].pop_front();
            stk.push(y);
        }
    }
}


int n,m;
int main() {
    fin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int x,y;
        fin >> x >> y;
        in[x]++; out[x]++;
        in[y]++; out[y]++;
        graph[x].push_back(y);
        graph[y].push_back(x);

    }
    bool ok = true;
    for (int i = 1; i <= n; i++) {
        if (in[i] % 2 or out[i] % 2) {
            fout << -1;
            return 0;
        }
    }

    solve(1);

    for (auto it = ans.rbegin(); it != ans.rend(); it++) {
        fout << *it << ' ';
    }
    return 0;
}