Pagini recente » Cod sursa (job #2958992) | Cod sursa (job #1605486) | Cod sursa (job #291024) | Cod sursa (job #1533390) | Cod sursa (job #2188654)
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
const int MaxN = 100005;
int N,M,X,Y,fr[MaxN],st[5*MaxN],top=0;
deque < pair <int,int> > v[MaxN];
bool found[5*MaxN];
void Euler(int nod)
{
st[++top]=nod;
while(top)
{
nod=st[top];
while(!v[nod].empty()&&found[v[nod][0].second])
v[nod].pop_front();
if(!v[nod].empty())
{
st[++top]=v[nod][0].first;
found[v[nod][0].second]=true;
v[nod].pop_front();
}
else
{
if(top>1) printf("%d ",nod);
top--;
}
}
}
int main()
{
freopen("ciclueuler.in","r",stdin);
freopen("ciclueuler.out","w",stdout);
scanf("%d%d",&N,&M);
for(int i=1;i<=M;i++)
{
scanf("%d%d",&X,&Y);
v[X].push_back(make_pair(Y,i));
v[Y].push_back(make_pair(X,i));
fr[X]++,fr[Y]++;
}
bool pos=true;
for(int i=1;i<=N;i++)
if(fr[i]%2==1)
{
pos=false;
break;
}
if(pos)
Euler(1);
else
printf("-1");
return 0;
}