Cod sursa(job #2561561)

Utilizator mihneacazCazacu Mihnea mihneacaz Data 28 februarie 2020 22:07:04
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#include <fstream>
#include <vector>
#include <stack>

using namespace std;

const int NMAX = 100505;
const int MMAX = 500505;

ifstream cin("ciclueuler.in");
ofstream cout("ciclueuler.out");

vector <pair <int, int> > g[NMAX];
int f[NMAX];
bool viz[MMAX];
stack <int> st;
vector <int> sol;

void dfs(int first)
{
    st.emplace(first);
    while(!st.empty()) {
        int node = st.top();
        if(g[node].size() > 0) {
            auto curr = g[node].back();
            g[node].pop_back();
            if(viz[curr.second] == 0) {
                viz[curr.second] = 1;
                st.emplace(curr.first);
            }
        } else {
            st.pop();
            sol.emplace_back(node);
        }
    }
}

int main() {
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= m; ++i) {
        int x, y;
        cin >> x >> y;
        g[x].emplace_back(y, i);
        g[y].emplace_back(x, i);
        f[x]++;
        f[y]++;
    }
    for(int i = 1; i <= n; ++i) {
        if(f[i] % 2 == 1) {
            cout << "-1\n";
            return 0;
        }
    }
    dfs(1);
    sol.pop_back();
    for(auto x: sol) {
        cout << x << " ";
    }
    cout << "\n";
    return 0;
}