Cod sursa(job #2860479)

Utilizator DordeDorde Matei Dorde Data 2 martie 2022 18:01:45
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include<bits/stdc++.h>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int const N = 1e5 + 3 , M = 5e5 + 3;
int n , m , a , b;
vector<int> v[N];
pair<int , int> e[M];
int vizm[M] , from[M] , to[M];
int main(){
    fin >> n >> m;
    for(int i = 1 ; i <= m ; ++ i){
        fin >> a >> b;
        v[a].push_back(i);
        v[b].push_back(i);
        from[i] = a;
        to[i] = b;
    }
    stack<int> st;
    vector<int> ans;
    st.push(1);
    while(!st.empty()){
        int node = st.top();
        if(v[node].size()){
            int e = v[node].back();
            v[node].pop_back();
            if(!vizm[e]){
                vizm[e] = 1;
                int _to = (from[e] ^ to[e] ^ node);
                st.push(_to);
            }
        }
        else{
            ans.push_back(node);
            st.pop();
        }
    }
    ans.pop_back();
    for(auto y : ans)
        fout << y << ' ';
    fout << '\n';
    return 0;
}