Cod sursa(job #2931776)

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

using namespace std;

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

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

struct edge{
int id, u, v;
};

bitset <M + 1> viz;

vector <edge> 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 (edge {++k, x, y}), g[y].push_back({k, y, 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().id;
            int urm = g[node].back().v;
            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;
}