Cod sursa(job #3285698)

Utilizator _andrei4567Stan Andrei _andrei4567 Data 13 martie 2025 13:24:02
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>

using namespace std;

ifstream cin ("ciclueuler.in");
ofstream cout ("ciclueuler.out");

const int M = 5e5;

const int N = 1e5;

bool viz[M + 1];

int f[N + 1];

vector <int> sol;

vector <pair <int, int> > g[N + 1];

stack <int> s;

int n, m, x, y;

int main()
{
    cin >> n >> m;
    for (int i = 1; i <= m; ++i)
    {
        cin >> x >> y;
        f[x]++;
        f[y]++;
        g[x].push_back({y, i});
        g[y].push_back({x, i});
    }
    for (int i = 1; i <= n; ++i)
        if (f[i] & 1)
        {
            cout << "-1\n";
            return 0;
        }
    s.push(1);
    while (!s.empty())
    {
        int x = s.top();
        if (!g[x].empty())
        {
            pair <int, int> node = g[x].back();
            g[x].pop_back();
            if (!viz[node.second])
                viz[node.second] = 1, s.push(node.first);
        }
        else
            sol.push_back(x), s.pop();
    }
    reverse (sol.begin(), sol.end());
    for (auto it : sol)
        cout << it << ' ';
    return 0;
}