Cod sursa(job #2203704)

Utilizator Menage_a_011UPB Cheseli Neatu Popescu Menage_a_011 Data 12 mai 2018 22:02:57
Problema Ciclu Eulerian Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.7 kb
// https://goo.gl/fBmFxu
#include <bits/stdc++.h>
using namespace std;

#define NMAX        100009
#define MMAX        500099
#define kInf        (1 << 30)
#define kInfLL      (1LL << 60)
#define kMod        666013
#define edge pair<int, int>
#define x first
#define y second


#define USE_FILES "MLC"

#ifdef USE_FILES
#define cin fin
#define cout fout
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
#endif

// number of tests from "in"
int test_cnt = 1;
void clean_test();

int n, m;
vector<int> G[NMAX];

edge edges[MMAX];
bitset<MMAX> visited;
vector<int> cycle;

int has_cycle() {
    for (int i = 1; i <= n; ++i) {
        if (G[i].size() & 1) {
            return 0;
        }
    }

    return 1;
}

inline void dfs(int node) {
    for (auto i : G[node]) {
        if (!visited[i]) {
            visited[i] = 1;
            dfs(edges[i].first + edges[i].second - node);
        }
    }

    cycle.push_back(node);
}

// your solution is here
void solve() {
    cin >> n >> m;

    for (int i = 1; i <= m; ++i) {
        int x, y;
        cin >> x >> y;

        edges[i] = {x, y};
        G[x].push_back(i);
        G[y].push_back(i);
    }

    if (!has_cycle()) {
        cout << "-1\n";
        goto out_label;
    }


    cycle.clear();
    cycle.reserve(m + 10);
    dfs(1);
    if (cycle.size()) {
        cycle.pop_back();
    }

    for (auto &x : cycle) {
        cout << x << " ";
    }
    cout << "\n";


out_label:
    if (test_cnt > 0) {
        clean_test();
    }
}


void clean_test() {
    // clean if needed
    visited.reset();
    for (int i = 1; i <= n; ++i) {
        G[i].clear();
    }
}

int main() {
//     cin >> test_cnt;
    while (test_cnt--) {
        solve();
    }

    return 0;
}