Cod sursa(job #1371001)

Utilizator tudorv96Tudor Varan tudorv96 Data 3 martie 2015 18:33:50
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include <fstream>
#include <vector>
using namespace std;

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

const int N = 1e5 + 5;

int n, m;
vector <int> g[N], st;

int main() {
    fin >> n >> m;
    for (int x, y, i = 0; i < m; ++i) {
        fin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    for (int i = 1; i <= n; ++i)
        if (g[i].size() & 1) {
            fout << -1;
            return 0;
        }
    int x = 0;
    for (int i = 1; i <= n && !x; ++i)
        if (g[i].size())
            x = i;
    st.push_back(x);
    while (st.size()) {
        int x = st.back();
        if (!g[x].size()) {
            fout << x << " ";
            st.pop_back();
            continue;
        }
        st.push_back(*g[x].begin());
        int y = *g[x].begin();
        swap(g[x][0], g[x].back());
        g[x].pop_back();
        vector <int> :: iterator it = g[y].begin();
        while (*it != x)
            ++it;
        swap(*it, g[y].back());
        g[y].pop_back();
    }
}