Pagini recente » Cod sursa (job #1588291) | Cod sursa (job #175706) | Cod sursa (job #399075) | Cod sursa (job #513494) | Cod sursa (job #2245651)
#include<bits/stdc++.h>
using namespace std;
stack<int>st;
int viz[100005];
vector<int>G[100005];
vector<int>:: iterator it;
int can=1,n;
void dfs(int u, int cc)
{
int h;
vector<int>:: iterator it1;
viz[u]=cc;
for(it1=G[u].begin();it1!=G[u].end();it1++)
{
h=(*it1);
if(viz[h]==0) dfs(h,cc);
}
}
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 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);
}
int cc=0;
for(i=1;i<=n;i++)
{
if(viz[i]==0)
{
++cc;
dfs(i,cc);
}
}
if(cc!=1) printf("-1");
fastboi(1);
while(st.size()>0)
{
if(st.size()!=1) printf("%d ",st.top());
st.pop();
}
}