Cod sursa(job #3226412)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 21 aprilie 2024 12:54:26
Problema Ciclu Eulerian Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.75 kb
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#pragma GCC optimize ("O1")
#pragma GCC optimize ("O2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target ("avx2")

using namespace std;
using namespace __gnu_pbds;

#define ordered_set tree <long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update>
#define lsb(x)(x & (-x))

const int max_size = 1e5 + 20, max_mc = 5e5 + 20;

int uz[max_mc];
vector <pair <int, int>> mc[max_size];
vector <int> ans;

void dfs (int nod)
{
    for (auto f : mc[nod])
    {
        int urm = f.first, idx = f.second;
        mc[nod].pop_back();
        if (uz[f.second] == 1)
        {
            continue;
        }
        uz[idx] = 1;
        dfs(urm);
    }
    ans.push_back(nod);
}

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;
        }
    }
    dfs(1);
    for (auto f : ans)
    {
        cout << f << " ";
    }
    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;
}