Cod sursa(job #2238952)

Utilizator MarutBrabete Marius Stelian Marut Data 8 septembrie 2018 14:56:40
Problema Ciclu Eulerian Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.04 kb
#include<bits/stdc++.h>
using namespace std;
stack<int>st;
vector<int>G[100005];
vector<int>:: iterator it;
int can=1;
void fastboi(int x)
{
    int other;
    st.push(x);
    if(G[x].size()==0) 
    {
        while(st.size()>0&&G[st.top()].size()==0)
        {
            if(st.size()!=1) printf("%d ",st.top());
            st.pop();
        }
        if(st.size()!=0) fastboi(st.top());
            else return;
    }
        else
        {
            other=G[x].back();
            G[x].pop_back();
            it=find(G[other].begin(),G[other].end(),x);
            G[other].erase(it);
            fastboi(other);
        }
}
int main()
{
    freopen("ciclueuler.in","r",stdin);
    freopen("ciclueuler.out","w",stdout);
    int n,i,j,x,x1,m;
    scanf("%d%d",&n,&m);
    for(i=1;i<=m;i++)
    {
        scanf("%d%d",&x,&x1);
        G[x].push_back(x1);
        G[x1].push_back(x);
    }
    fastboi(1);
    while(st.size()>0)
    {
        if(st.size()!=1) printf("%d ",st.top());
        st.pop();
    }
}