Cod sursa(job #2669091)

Utilizator felix24mihaiPrava Felix Mihai felix24mihai Data 6 noiembrie 2020 02:00:30
Problema Ciclu Eulerian Scor 80
Compilator cpp-32 Status done
Runda Arhiva educationala Marime 1.71 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;
    }
}
int main()
{
    read();
    for (int i = 1; i <= n; ++i) {
        if (sizeOf[i] & 1) {
            g << "-1\n";
            return 0;
        }
    }
    vector<int> stk;
    stk.push_back(1);
    while(!stk.empty()){
        int node = stk.back();
        bool findNode = false;
        int i = 0;
        while (i < nodes[node].size() && findNode == false){
            int from_ = from[nodes[node][i]];
            if (from_ != 0){
                findNode = true;
                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;
                stk.push_back(next);
            }
            i++;
        }
        if (findNode == false){
            stk.pop_back();
            result.push_back(node);
        }
    }
    for (int i = result.size() - 1; i > 0; i--)
        g << result[i] << " ";
    return 0;
}