Cod sursa(job #2669088)

Utilizator felix24mihaiPrava Felix Mihai felix24mihai Data 6 noiembrie 2020 01:42:34
Problema Ciclu Eulerian Scor 80
Compilator cpp-32 Status done
Runda Arhiva educationala Marime 1.41 kb
#include <iostream>
#include <fstream>
#include <vector>
#define NMAX 100001
#define MMAX 500005
#define SZ(x) ((int) (x).size())
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");

int n, m, sizeOf[NMAX], index = 0;
int from[MMAX], to[MMAX];
vector <int> nodes[NMAX];
vector <int> result;
void read(){
    f >> n >> m;
    int x, y;
    for (int i = 1; i <= m; i++){
        f >> x >> y;
        nodes[x].push_back(i);
        sizeOf[x]++;
        sizeOf[y]++;
        if (x != y){
            nodes[y].push_back(i);
        }
        if (x > y)
            swap(x, y);
        from[i] = x;
        to[i] = y;
    }
}
void euler(int node){
    int i = 0;
    while (i < nodes[node].size()){
        int from_ = from[nodes[node][i]];
        if (from_ != 0){
            int to_ = to[nodes[node][i]];
            int next;
            if (from_ == node)
                next = to_;
            else
                next = from_;
            from[nodes[node][i]] = 0;
            to[nodes[node][i]] = 0;
            euler(next);
        }
        i++;
    }
    result.push_back(node);
}
int main()
{
    read();
    for (int i = 1; i <= n; ++i) {
        if (sizeOf[i] & 1) {
            g << "-1\n";
            return 0;
        }
    }
    euler(1);
    for (int i = result.size() - 1; i > 0; i--)
        g << result[i] << " ";
    return 0;
}