Cod sursa(job #2712622)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 26 februarie 2021 09:36:09
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 100005, mmax = 500005;
int n, m, from[mmax], to[mmax];
bool viz[mmax];

vector <int> G[nmax];

int main(){
    fin >> n >> m;
    for (int i = 1; i <= m; ++i){
        int x, y;
        fin >> x >> y;
        G[x].push_back(i);
        G[y].push_back(i);
        from[i] = x;
        to[i] = y;
    }
    bool euler = true;
    for (int i = 1; i <= n; ++i){
        if (G[i].size() & 1){
            euler = false;
            break;
        }
    }
    if (!euler){
        fout << -1;
        return 0;
    }
    vector <int> s, ans;
    s.push_back(1);
    while (s.size() > 0){
        int nod = s.back();
        if (G[nod].size() > 0){
            int muchie = G[nod].back();
            G[nod].pop_back();
            if (viz[muchie] == false){
                viz[muchie] = true;
                s.push_back(from[muchie] + to[muchie] - nod);
            }
        }
        else{
            ans.push_back(nod);
            s.pop_back();
        }
    }
    ans.pop_back();
    for (auto it : ans){
        fout << it << " ";
    }
    return 0;
}