#include <cstdio>
#include <set>
#include <map>
#include <vector>
#include <iostream>
#include <algorithm>
#define space " "
using namespace std;
const int MAXN = 1e6 + 10;
int u[MAXN], v[MAXN], n;
map<int, int> fp;
vector<int> G[MAXN];
bool vis[MAXN];
int rec[MAXN];
void DFS(int u) {
for(int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if(vis[v]) continue;
printf(" %d", rec[v]);
vis[v] = true; DFS(v);
}
}
int main() {
while (scanf("%d", &n) != EOF) {
int cnt = 0;
for(int i = 1; i <= n; i++) {
scanf("%d%d", &u[i], &v[i]);
rec[cnt++] = u[i];
rec[cnt++] = v[i];
fp[u[i]]++; fp[v[i]]++;
}
//先排序
sort(rec, rec + cnt);
//去重
int T = unique(rec, rec + cnt) - rec;
for(int i = 0; i < T; i++) {
G[i].clear(); vis[i] = false;
}
//离散化
for(int i = 1; i <= n; i++) {
int sx = lower_bound(rec, rec+T, u[i]) - rec;
int ex = lower_bound(rec, rec+T, v[i]) - rec;
G[sx].push_back(ex); G[ex].push_back(sx);
}
for(int i = 0; i < T; i++) {
if(fp[rec[i]] == 1) {
printf("%d", rec[i]);
vis[i] = true;
DFS(i); break;
}
}
printf("\n");
}
return 0;
}
Codeforces 29C Mail Stamps【离散化+DFS】
最新推荐文章于 2022-07-24 15:14:47 发布