Cod sursa(job #1870678)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 6 februarie 2017 20:28:25
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <fstream>
#include <vector>
#define SZ(x) ((int) (x).size())
using namespace std;
 
const int NMAX = 100000, MMAX = 500000;
 
vector<int> G[NMAX];
int E[MMAX];
 
int main(void) {
    ifstream cin("ciclueuler.in");
    ofstream cout("ciclueuler.out");
    cin.tie(0);
    ios_base::sync_with_stdio(false);
 
    int n, m; cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int x, y; cin >> x >> y; x--; y--;
        E[i] = x ^ y;
        G[x].push_back(i);
        G[y].push_back(i);
    }
     
    for (int i = 0; i < n; i++) {
        if (SZ(G[i]) & 1) {
            cout << "-1\n";
            return 0;
        }
    }
 
    vector<int> stk;
    stk.push_back(0);
    while (not stk.empty()) {
        const int node = stk.back();
        while (not G[node].empty() and E[G[node].back()] == -1) {
            G[node].pop_back();
        }   
        if (G[node].empty()) {
            stk.pop_back();
            if (not stk.empty()) {
                cout << node + 1 << ' ';
            }
        } else {
            stk.push_back(E[G[node].back()] ^ node);
            E[G[node].back()] = -1;
            G[node].pop_back();
        }
    }
}