Cod sursa(job #3000844)

Utilizator Luka77Anastase Luca George Luka77 Data 12 martie 2023 22:44:53
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.56 kb
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(WHATEVER) for(int i = 1; i <= WHATEVER; ++ i)

/// INPUT / OUTPUT
const string problem = "ciclueuler";
ifstream fin(problem + ".in");
ofstream fout(problem + ".out");

struct graph
{
    int node, edge;
};

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

/// SOLUTION
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().node;
            int new_edge = g[node].back().edge;
            g[node].pop_back();
            st.push(node);
            if(!viz[new_edge])
            {
                viz[new_edge] = 1;
                st.push(new_node);
            }
        }
    }
}

/// READING THE INPUT
int main()
{
    ios::sync_with_stdio(false);
    fin.tie(NULL);
    fout.tie(NULL);

    fin >> n >> m;

    for(int i = 1; i <= m; ++ i)
    {
        int node1, node2;
        fin >> node1 >> node2;
        g[node1].push_back({node2, i});
        g[node2].push_back({node1, i});
    }

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

    dfs(1);
    ans.pop_back();
    for(auto x : ans)
        fout << x << ' ';
}