Cod sursa(job #3215204)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 14 martie 2024 18:51:39
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.43 kb
#include <bits/stdc++.h>

using namespace std;

const int max_size = 1e5 + 20, INF = 2e9 + 1;

int viz[5 * max_size];
vector <pair <int, int>> mc[max_size];
stack <int> stk;

void solve ()
{
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        int x, y;
        cin >> x >> y;
        mc[x].push_back({y, i});
        mc[y].push_back({x, i});
    }
    for (int i = 1; i <= n; i++)
    {
        if (mc[i].size() % 2 == 1)
        {
            cout << "-1";
            return;
        }
    }
    stk.push(1);
    for (int i = 1; i <= m; i++)
    {
        int nod = stk.top();
        while (!mc[nod].empty())
        {
            int vec = mc[nod].back().first, idx = mc[nod].back().second;
            mc[nod].pop_back();
            if (viz[idx] == 1)
            {
                continue;
            }
            viz[idx] = 1;
            nod = vec;
            stk.push(nod);
        }
        cout << nod << " ";
        stk.pop();
    }
    cout << '\n';
}

signed main ()
{
#ifdef LOCAL
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
#else
    freopen("ciclueuler.in", "r", stdin);
    freopen("ciclueuler.out", "w", stdout);
#endif // LOCAL
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long tt;
    //cin >> tt;
    tt = 1;
    while (tt--)
    {
        solve();
    }
    return 0;
}