Cod sursa(job #3239240)

Utilizator AlexPlesescuAlexPlesescu AlexPlesescu Data 3 august 2024 12:50:44
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <bits/stdc++.h>

using namespace std; 
#define pb push_back
#define sz(x) (int)(x.size())
const int N = 1e5 + 5, V = 25, INF = 1e9;

int n, m, deg[N];
vector<pair<int, int>> g[N];

int main() {
	cin.tie(0)->sync_with_stdio(0);

#ifndef ONLINE_JUDGE
    freopen("ciclueuler.in", "r", stdin);
    freopen("ciclueuler.out", "w", stdout);
#endif
    
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int u, v;
        cin >> u >> v;
        g[u].pb({v, i});
        g[v].pb({u, i});
    }

    for (int i = 1; i <= n; i++) {
        if (sz(g[i]) % 2 == 1) {
            cout << -1;
            return 0;
        }
    }

    vector<int> ans, stk, seen(m + 1);

    stk.push_back(1);

    while (!stk.empty()) {
        int node = stk.back();
        if (!g[node].empty()) {
            auto cand = g[node].back();
            g[node].pop_back();
            if (seen[cand.second])
                continue;
            seen[cand.second] = 1;
            stk.push_back(cand.first);
        } else {
            ans.push_back(stk.back());
            stk.pop_back();
        }
    }

    ans.pop_back();
    for (auto it : ans)
        cout << it << ' ';

}