Cod sursa(job #3186701)

Utilizator Yanis3PiquePopescu Pavel-Yanis Yanis3Pique Data 24 decembrie 2023 18:11:38
Problema Ciclu Eulerian Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("ciclueuler.in"); // ciclueulerian.in
ofstream g("ciclueuler.out");

const int NMAX = 100005;
int N, M;
vector<pair<int, int>> G[NMAX];
bool viz[NMAX];

void read() {
    f >> N >> M;
    int x, y;
    for(int i = 1; i <= M; i++) {
        f >> x >> y;
        G[x].push_back({y, i});
        G[y].push_back({x, i});
    }
}

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

    vector<int> result;
    stack<int> s;
    s.push(1);

    while(!s.empty()) {
        int top = s.top();

        if(G[top].size()) {
            auto back = G[top].back();
            G[top].pop_back();
            if(!viz[back.second]) {
                s.push(back.first);
            }
            viz[back.second] = true;
        } else {
            s.pop();
            result.push_back(top);
        }
    }
    for(const auto& node : result) {
        g << node << " ";
    }
}

int main() {
    read();
    solve();

    return 0;
}