RT,求助大佬,调了一晚上,救救蒟蒻吧。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
#define max_(a, b) a > b ? a : b
#define min_(a, b) a < b ? a : b
#define INF 0x3f3f3f3f
namespace IO {
char buf[1<<21], *p1 = buf, *p2 = buf, buf1[1<<21];
inline char gc () {return p1 == p2 && (p2 = (p1 = buf) + fread (buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;}
#ifndef ONLINE_JUDGE
#endif
template<class I>
inline void read(I &x) {
x = 0; I f = 1; char c = gc();
while(c < '0' || c > '9') {if(c == '-') f = -1; c = gc(); }
while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = gc(); }
x *= f;
}
template<class I>
inline void write(I x) {
if(x == 0) {putchar('0'); return;}
I tmp = x > 0 ? x : -x;
if(x < 0) putchar('-');
int cnt = 0;
while(tmp > 0) {
buf1[cnt++] = tmp % 10 + '0';
tmp /= 10;
}
while(cnt > 0) putchar(buf1[--cnt]);
}
#define in(x) read(x)
#define outn(x) write(x), putchar('\n')
#define out(x) write(x), putchar(' ')
} using namespace IO;
int n;
const int maxn = 1e5+1;
struct node{
int to, next;
}e[maxn<<1];
int cnt, head[maxn];
inline void add(int u, int v) {
e[++cnt] = (node){v, head[u]};
head[u] = cnt;
}
int u, v, sum;
map<int,int>f;
int tot;
map<int,int>g;
int in[maxn];
int vis[maxn];
int st;
int ans[maxn], tnt;
bool dfs(int u, int now) {
// out(g[u]), out("\n");
if(now == tot) {
out(g[u]);
return true;
}
for(register int i(head[u]); i; i = e[i].next) {
int v = e[i].to;
// out(g[v]), out(" ");
if(!vis[v]) {
vis[v] = 1;
if(dfs(v, now+1)) {
out(g[u]);
return true;
}
vis[v] = 0;
}
}
}
int main() {
read(n);
for(register int i(1); i <= n; ++i) {
read(u), read(v);
if(!f[u]) f[u] = ++tot, g[tot] = u;
if(!f[v]) f[v] = ++tot, g[tot] = v;
add(f[u], f[v]), add(f[v], f[u]);
++in[f[u]], ++in[f[v]];
}
st = 1;
for(register int i(1); i <= tot; ++i) {
if(in[i] == 1) st = i;
}
vis[st] = 1;
dfs(st, 1);
return 0;
}