Cod sursa(job #2859216)

Utilizator rares89_Dumitriu Rares rares89_ Data 1 martie 2022 00:06:01
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <bits/stdc++.h>
 
using namespace std;
 
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");

int n, m, x, y;
vector< pair<int, int> > G[100005];
vector<int> ans;
stack<int> st;
bitset<500005> v; // muchii

int main() {
    fin >> n >> m;
    for(int i = 1; i <= m; i++) {
        fin >> x >> y;
        G[x].push_back({y, i});
        G[y].push_back({x, i});
    }
    fin.close();
    for(int i = 1; i <= n; i++) {
        if(G[i].size() % 2 != 0) { // daca nodul i are grad impar
            fout << "-1";
            return 0;
        }
    }
    st.push(1);
    while(!st.empty()) {
        int nod = st.top();
        if(G[nod].size() > 0) {
            int e = G[nod][G[nod].size() - 1].second;
            if(!v[e]) {
                v[e] = true;
                st.push(G[nod][G[nod].size() - 1].first);
            }
            G[nod].pop_back();
        } else {
            st.pop();
            ans.push_back(nod);
        }
    }
    for(int val : ans) {
        fout << val << " ";
    }
	return 0;
}