Cod sursa(job #2647444)

Utilizator pregoliStana Andrei pregoli Data 4 septembrie 2020 17:42:53
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#include <bits/stdc++.h>
#define newline '\n'
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
///***********************
const int NMAX = 1e5 + 3, MMAX = 5e5 + 3;
int n, m;
vector<pair<int, int>> graph[NMAX];//{node, nredge}
bool visEdge[MMAX];

int main() {
    fin >> n >> m;
    for (int i = 1, a, b; i <= m; i++) {
        fin >> a >> b;
        graph[a].push_back(make_pair(b, i));
        graph[b].push_back(make_pair(a, i));
    }
    for (int i = 1; i <= n; i++)
        if (graph[i].size() & 1)
            return fout << -1 << newline, 0;

    stack<int> st;
    vector<int> ans;
    st.push(1);
    while (!st.empty()) {
        int node = st.top(), to, edge;
        if (!graph[node].empty()) {
            tie(to, edge) = graph[node].back();
            graph[node].pop_back();
            if (!visEdge[edge]) {
                visEdge[edge] = true;
                st.push(to);
            }
        } else {
            ans.push_back(node);
            st.pop();
        }
    }
    ans.pop_back();
    for (auto it : ans)
        fout << it << ' ';
    fout << newline;
    fout.close();
    return 0;
}