cswork / OAOpen practice workspace

Capital OneOA #4

Reconstruct Journey from Photo Pairs

Estimated MediumJudge-ready

Topics (AI-labelled)HashingUnion-Find

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.

Input

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

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.

Example 1

Input:

5
3 5
1 4
2 4
1 5

Output:

2 4 1 5 3

Example 2

Input:

2
9 -2

Output:

-2 9

Example 3

Input:

4
20 3
3 10
10 -7

Output:

-7 10 3 20

Editorial

Loading solution…