Cod sursa(job #2958603)

Utilizator Luka77Anastase Luca George Luka77 Data 27 decembrie 2022 09:24:01
Problema Ciclu Eulerian Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.38 kb
#include <bits/stdc++.h>
using namespace std;

/// INPUT / OUTPUT
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");

/// STRUCTS
struct graph
{
    int nod, muchie;
};

/// GLOBAL VARIABLES
const int NMAX = 1e5+5;
int n, m;
vector<graph>g[NMAX];
vector<int>ans;
bool viz[NMAX];


inline void dfs(int start)
{
    stack<int>st;
    st.push(start);
    while(!st.empty())
    {
        int node = st.top();
        st.pop();
        if(!g[node].size())
            ans.push_back(node);
        else
        {
            int new_node = g[node].back().nod;
            int new_muchie = g[node].back().muchie;

            g[node].pop_back();
            st.push(node);
            if(!viz[new_muchie])
            {
                viz[new_muchie] = 1;
                st.push(new_node);
            }
        }
    }
}

/// SOLUTION
inline void solve()
{
    for(int i = 1; i <= n; ++ i)
    {
        if(g[i].size()%2==1)
        {
            cout << -1;
            return;
        }
    }
    dfs(1);
    ans.pop_back();
    for(auto x : ans)
        cout << x << ' ';
}

/// READING THE INPUT
int main()
{
    cin >> n >> m;
    for(int i = 1 ; i <= m; ++ i)
    {
        int node1, node2;
        cin >> node1 >> node2;
        g[node1].push_back({node2, i});
        g[node2].push_back({node1, i});
    }
    solve();
}