cswork / OA打开练习工作台

Capital OneOA #4

Reconstruct Journey from Photo Pairs

估计中等可在线判题

考点(AI 标注)哈希计数并查集

During a journey, each landmark is visited exactly once. Each photo records a pair of distinct landmarks visited consecutively, and the order of the photo pairs is unknown. Reconstruct the complete journey from all the photos; either the forward or the reverse direction is valid. For deterministic output, this site requires the output to start from the endpoint with the smaller label.

输入格式

The first line contains n (2≤n≤200000), followed by n−1 lines, each containing two distinct integers u and v representing one photo. Landmark numbers serve as distinct node labels, with absolute value at most 10^9. Site supplement: the edges are guaranteed to form a simple path.

输出格式

Output the n landmark labels starting from the endpoint with the smaller label, separated by spaces. The reversed path starting from the larger endpoint is equivalent, but this site uniformly requires starting from the smaller endpoint for exact comparison.

样例 1

输入:

5
3 5
1 4
2 4
1 5

输出:

2 4 1 5 3

样例 2

输入:

2
9 -2

输出:

-2 9

样例 3

输入:

4
20 3
3 10
10 -7

输出:

-7 10 3 20