Cod sursa(job #3226786)

Utilizator _andrei4567Stan Andrei _andrei4567 Data 22 aprilie 2024 20:08:50
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.41 kb
#include <fstream>
#include <stack>
#include <vector>
#include <algorithm>

using namespace std;

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

using pa = pair <int, int>;

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

bool degree[N + 1];

bool viz[M + 1];

vector <pa> g[N + 1];

stack <int> s;

vector <int> sol;

struct ceva
{
    int x, y, cost;
};

vector <ceva> edges;

int n, m, x, y;

int main()
{
    cin >> n >> m;
    for (int i = 1; i <= m; ++i)
    {
        cin >> x >> y;
        degree[x] ^= 1;
        degree[y] ^= 1;
        edges.push_back({x, y, i});
        edges.push_back({y, x, i});
        g[x].push_back({y, i});
        g[y].push_back({x, i});
    }
    for (int i = 1; i <= n; ++i)
        if (degree[i])
    {
        cout << "-1\n";
        return 0;
    }
    for (int i = 1; i <= n; ++i)
        if (!g[i].empty())
    {
        s.push(i);
        break;
    }
    while (!s.empty())
    {
        int x = s.top();
        if (!g[x].empty())
        {
            int node = g[x].front().first;
            int muchie = g[x].front().second;
            g[x].erase (g[x].begin());
            if (!viz[muchie])
                viz[muchie] = 1, s.push(node);
        }
        else
            sol.push_back(x), s.pop();
    }
    sol.erase (sol.end() - 1);
    for (auto it : sol)
        cout << it << ' ';
    return 0;
}