Cod sursa(job #2694868)

Utilizator sebastianp2003Popa Sebastian sebastianp2003 Data 10 ianuarie 2021 23:02:41
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
// #define f cin
// #define g cout
int n, m;
bool bif[500002];
vector<vector<pair<int, int>>> v;
stack<int> cp;
vector<int> rez;
int main()
{
    ios_base::sync_with_stdio(0);
    f.tie(0);
    g.tie(0);
    f >> n >> m;
    v.resize(n + 1);
    for (int i = 1, x, y; i <= m; i++)
        f >> x >> y, v[x].emplace_back(y, i), v[y].emplace_back(x, i);
    for (int i = 1; i <= n; i++)
        if (v[i].size() & 1)
        {
            g << -1;
            return 0;
        }
    cp.push(1);
    while (!cp.empty())
    {
        int ac = cp.top();
        if (v[ac].empty())
        {
            rez.emplace_back(ac);
            cp.pop();
        }
        else
        {
            if (bif[v[ac].back().second])
                v[ac].pop_back();
            else
            {
                auto ne = v[ac].back();
                v[ac].pop_back();
                cp.push(ne.first);
                bif[ne.second] = 1;
            }
        }
    }
    if (rez.size() != m + 1)
    {
        g << -1;
        return 0;
    }
    else
    {
        for (int i = rez.size() - 2; i >= 0; i--)
            g << rez[i] << " ";
    }
    return 0;
}