Cod sursa(job #2172753)

Utilizator infomaxInfomax infomax Data 15 martie 2018 17:45:53
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <bits/stdc++.h>

using namespace std;

ifstream F("ciclueuler.in");
ofstream G("ciclueuler.out");

int n, m, x, y, deg[100005], z, l[100005], L;
stack<int> st;
bitset<500005> w;
vector<pair<int, int> > a[100005];

int main()
{
    F >> n >> m;
    for(int i = 1; i <= m; ++ i){
        F>>x>>y;
        a[x].push_back({y, i});
        a[y].push_back({x, i});
        deg[x]++;
        deg[y]++;
    }
    for(int i = 1; i <= n; ++ i)
        if(!deg[i]||deg[i]%2){
            G<<-1;
            return 0;
        }
    st.push(1);
    while(!st.empty()){
        x=st.top();
        L=a[x].size();
        while(l[x]<L){
            y=a[x][l[x]].first;
            z=a[x][l[x]].second;
            if(!w[z]){
                st.push(y);
                w[z]=1;
                break;
            }
            l[x]++;
        }
        if(l[x]==L){
            G << x<<" ";
            st.pop();
        }
    }
    return 0;
}