Cod sursa(job #3040848)

Utilizator DordeDorde Matei Dorde Data 30 martie 2023 15:09:39
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.03 kb
#include <fstream>
#include <vector>
#include <stack>

using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int const N = 5e5 + 3;
int n , m;
int st[500001] , dr[500001] , viz[500001];
vector<int> v[100001];
int other(int e , int x){
    return (x ^ st[e] ^ dr[e]);
}
int main(){
    fin >> n >> m;
    for(int i = 1 ; i <= m ; ++ i){
        fin >> st[i] >> dr[i];
        v[st[i]].push_back(i);
        v[dr[i]].push_back(i);
    }
    for(int i = 1 ; i <= n ; ++ i){
        if(v[i].size() & 1){
            fout << "-1";
            return 0;
        }
    }
    stack<int> st;
    st.push(1);
    while(!st.empty()){
        int x = st.top();
        if(v[x].empty()){
            st.pop();
            if(!st.empty())
                fout << x << ' ';
        }else{
            int e = v[x].back();
            v[x].pop_back();
            if(viz[e])
                continue;
            viz[e] = 1;
            st.push(other(e , x));
        }
    }
    return 0;
}