Cod sursa(job #2653590)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 28 septembrie 2020 16:21:32
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.31 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 100005;
int n, m, ans[5 * nmax], z, from[5 * nmax], to[5 * nmax];
bool viz[5 * nmax];
vector <int> G[nmax], st;

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 ciclu_eulerian = true;
    for (int i = 1; i <= n; ++i){
        if (G[i].size() % 2 == 1){
            ciclu_eulerian = false;
            break;
        }
    }
    if (ciclu_eulerian == false){
        fout << -1 << "\n";
    }
    st.push_back(1);
    while (!st.empty()){
        int nod = st.back();
        if (G[nod].size() > 0){
            int nod = st.back();
            int e = G[nod].back();
            G[nod].pop_back();
            if (viz[e] == false){
                viz[e] = true;
                int vecin = from[e] + to[e] - nod;
                st.push_back(vecin);
            }
        }
        else{
            ans[++z] = st.back();
            st.pop_back();
        }
    }
    for (int i = 1; i < z; ++i){
        fout << ans[i] << " ";
    }
    fin.close();
    fout.close();
    return 0;
}