Cod sursa(job #2859213)

Utilizator rares89_Dumitriu Rares rares89_ Data 28 februarie 2022 23:49:23
Problema Ciclu Eulerian Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <bits/stdc++.h>
 
using namespace std;
 
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");

int n, m, x, y, f[500005], t[500005];
vector<int> G[100005], ans, 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);
        G[y].push_back(x);
        f[i] = x;
        t[i] = y;
    }
    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_back(1);
    while(!st.empty()) {
        int nod = st.back();
        if(!G[nod].empty()) {
            int e = G[nod].back();
            G[nod].pop_back();
            if(!v[e]) {
                v[e] = true;
                int to = ::f[e] ^ ::t[e] ^ nod;
                cout << (::t[e]) << "\n";
                st.push_back(to);
            }
        } else {
            st.pop_back();
            ans.push_back(nod);
        }
    }
    for(unsigned int i = 0; i < ans.size(); i++) {
        fout << ans[i] << " ";
    }
	return 0;
}