Cod sursa(job #2981303)

Utilizator Elvis_CostinTuca Elvis-Costin Elvis_Costin Data 17 februarie 2023 17:49:27
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <bits/stdc++.h>
using namespace std;
string np = "ciclueuler";
ifstream f(np + ".in");
ofstream g(np + ".out");

// #define f cin
// #define g cout

int n, m;
vector<bool> viz(500003);
vector<pair<int, int>> adj[100003];
vector<int> rez;
stack<int> st;

int main()
{
    f >> n >> m;
    for (int i = 1, a, b; i <= m; i++)
        f >> a >> b,
            adj[a].emplace_back(b, i),
            adj[b].emplace_back(a, i);

    for (int i = 1; i <= n; i++)
        if (adj[i].size() % 2 == 1)
        {
            g << -1;
            return 0;
        }

    st.push(1);
    while (!st.empty())
    {
        int nod = st.top();
        if (adj[nod].empty())
            rez.push_back(nod), st.pop();
        else
        {
            if (viz[adj[nod].back().second])
                adj[nod].pop_back();
            else
            {
                auto next = adj[nod].back();
                st.push(next.first);
                viz[next.second] = 1;
                adj[nod].pop_back();
            }
        }
    }

    if (rez.size() != m + 1)
        g << -1;
    else
    {
        rez.pop_back();
        for (auto x : rez)
            g << x << " ";
    }

    return 0;
}