Cod sursa(job #551758)
#include <stdio.h>
#include <vector>
#include <list>
using namespace std;
#define N 800000
struct Nod {
int x;
Nod *next;
};
int n, m;
int que[N];
list <int> a[N];
int lista[N];
int gr[N];
int stack[N];
int viz[N];
void find_tour(int s) {
int top = 1;
int it;
stack[top] = s;
while (top > 0) {
while (a[stack[top]].size() != 0) {
int n = a[stack[top]].front();
a[stack[top]].pop_front();
list<int>::iterator it2;
for(it2 = a[n].begin(); it2 != a[n].end(); it2++)
if (*it2 == stack[top]) {a[n].erase(it2); break;}
stack[++top] = n;
}
lista[++lista[0]] = stack[top];
top--;
}
}
int bf(int s) {
int st = 1, dr = 1;
list<int>::iterator it;
que[1] = s;
viz[s] = 1;
while (st <= dr) {
for(it = a[que[st]].begin(); it != a[que[st]].end(); it++)
if (!viz[*it]) {
que[++dr] = *it;
viz[*it] = 1;
}
st++;
}
}
int este_conex() {
bf(1);
for(int i = 1; i <= n; i++)
if (viz[i] == 0) return 0;
return 1;
}
int grad_par() {
for(int i = 1; i <= n; i++)
if (gr[i] % 2 != 0) return 0;
return 1;
}
int main()
{
freopen("ciclueuler.in","r",stdin);
freopen("ciclueuler.out","w",stdout);
scanf("%d %d",&n,&m);
for(int i = 1; i <= m; i++) {
int u,v;
scanf("%d %d",&u,&v);
gr[u]++;
gr[v]++;
a[u].push_back(v);
a[v].push_back(u);
/*insert(a[u], v);
insert(a[v], u);*/
}
if (este_conex() && grad_par()) {
find_tour(1);
for(int i = 1; i < lista[0]; i++)
printf("%d ",lista[i]);
printf("\n");
}
else
printf("-1\n");
return 0;
}