## 【AtCoder】[Grand Contest 013 B] Hamiltonish Path

### Code

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int N = 100009;
const int M = N << 1;

int n, m, head[N], nxt[M], to[M], vis[N];
deque<int> ans;

inline int read() {
char c=getchar(); int f=1,ret=0;
while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
while (c<='9'&&c>='0') {ret=ret*10+c-'0';c=getchar();}
return ret * f;
}

inline void AddEdge(int u, int v) {
static int E = 1;
to[++E] = v; nxt[E] = head[u]; head[u] = E;
to[++E] = u; nxt[E] = head[v]; head[v] = E;
}

int main() {
#ifdef DBG
freopen("11input.in", "r", stdin);
#endif
n = read(), m = read();
for (int i = 1; i <= m; i++) {
int u = read(), v = read();
AddEdge(u, v);
if (i == 1) {
ans.push_back(u);
ans.push_back(v);
vis[u] = vis[v] = 1;
}
}
for (int mk = 1; mk; ) {
mk = 0;
for (int i = head[ans.front()]; i; i = nxt[i]) {
if (!vis[to[i]]) {
mk = 1;
ans.push_front(to[i]);
vis[to[i]] = 1;
break;
}
}
}
for (int mk = 1; mk; ) {
mk = 0;
for (int i = head[ans.back()]; i; i = nxt[i]) {
if (!vis[to[i]]) {
mk = 1;
ans.push_back(to[i]);
vis[to[i]] = 1;
break;
}
}
}
cout<<ans.size()<<endl;
for (; !ans.empty(); ans.pop_front()) {
printf("%d ", ans.front());
}
return 0;
}