Cod sursa(job #2931777)

Utilizator _andrei4567Stan Andrei _andrei4567 Data 31 octombrie 2022 22:10:45
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <fstream>
#include <vector>
#include <bitset>
#include <stack>

using namespace std;

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

using zu = pair <int, int>;

const int M = 5e5;
const int N = 1e5;

bitset <M + 1> viz;

vector <zu> g[N + 1];

vector <int> sol;

int n, m, x, y, k;

stack <int> s;

int main()
{

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