Cod sursa(job #1898429)

Utilizator savigunFeleaga Dragos-George savigun Data 2 martie 2017 00:34:49
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;

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

int n, m;
vector<list<int> > g(100001);
vector<int> st;

bool eulerian() {
    for (int i = 1; i <= n; ++i)
        if (g[i].size() % 2 != 0) return false;
    return true;
}

void remove_edge(int x, int y) {
    g[x].erase(g[x].begin());
    g[y].erase(find(g[y].begin(), g[y].end(), x));
}


int main() {
    int x;

    in >> n >> m;
    for (int i = 1; i <= m; ++i) {
        int x, y;
        in >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }

    if (!eulerian()) {
        out << -1;
        return 0;
    }

    st.push_back(1);

    while (!st.empty()) {
        x = st.back();
        while(!g[x].empty()) {
            int y = g[x].front();
            remove_edge(x, y);
            st.push_back(y);
            x = y;
        }

        if (st.size() >= 2) out << st.back()<< " ";
        st.pop_back();
    }

    return 0;
}