D - Takahashi Tour Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400400

問題文

AtCoder 国には 11 から NN の番号がついた NN 個の都市と、11 から N1N-1 の番号がついた N1N-1 個の道路があります。
道路 ii を通ると都市 AiA_i と都市 BiB_i の間を相互に移動することができます。全ての都市は道路を使って互いに行き来できることが保証されます。

高橋くんは都市 11 を出発し、次のルールで旅をします。

  • いまいる都市と道路で直接つながっている都市のうち、まだ訪れたことがない都市が存在するとき、そのような都市のうち番号が最も小さい都市へ移動する
  • そのような都市が存在しないとき
    • いまいる都市が都市 11 なら旅を終了する
    • そうでないなら、いまいる都市を初めて訪れたときに直前にいた都市へ移動する

高橋くんが旅の過程で訪れる都市を順に答えてください。

制約

  • 2N2×1052 \leq N \leq 2\times 10^5
  • 1Ai,BiN1\leq A_i,B_i \leq N
  • 全ての都市は道路を使って互いに行き来できる

入力

入力は以下の形式で標準入力から与えられる。

NN
A1A_1 B1B_1
\vdots
AN1A_{N-1} BN1B_{N-1}

出力

高橋くんが訪れる都市を、旅の開始時及び終了時の都市 11 も含めて順に空白区切りで出力せよ。


入力例 1Copy

Copy
4
1 2
4 2
3 1

出力例 1Copy

Copy
1 2 4 2 1 3 1

高橋くんの旅は次のようになります。

  • 最初都市 11 にいます。
  • 都市 11 から道路で直接つながっている都市のうち未訪問なのは都市 2,32,3 です。番号が小さい都市 22 へ移動します。
  • 都市 22 から道路で直接つながっている都市のうち未訪問なのは都市 44 です。都市 44 へ移動します。
  • 都市 44 から道路で直接つながっている都市のうち未訪問の都市はありません。直前にいた都市 22 へ移動します。
  • 都市 22 から道路で直接つながっている都市のうち未訪問の都市はありません。初めて都市 22 に来る直前にいた都市 11 へ移動します。
  • 都市 11 から道路で直接つながっている都市のうち未訪問なのは都市 33 です。都市 33 へ移動します。
  • 都市 33 から道路で直接つながっている都市のうち未訪問の都市はありません。直前にいた都市 11 へ移動します。
  • 都市 11 から道路で直接つながっている都市のうち未訪問の都市はありません。旅を終了します。

入力例 2Copy

Copy
5
1 2
1 3
1 4
1 5

出力例 2Copy

Copy
1 2 1 3 1 4 1 5 1

Score : 400400 points

Problem Statement

The Republic of AtCoder has NN cities numbered 11 through NN and N1N-1 roads numbered 11 through N1N-1. Road ii connects City AiA_i and City BiB_i bidirectionally. It is guaranteed that one can travel between every pair of cities using roads.

Takahashi will depart City 11 and have a journey by repeating the following.

  • If there are unvisited cities that are directly connected to the city Takahashi is in now, he goes to the city with the smallest number among those cities.
  • Otherwise,
    • if he is in City 11, he ends the journey;
    • otherwise, he goes to the city he was in just before visiting the current city for the first time.

Find the sequence of cities visited by Takahashi in the order he visits them.

Constraints

  • 2N2×1052 \leq N \leq 2\times 10^5
  • 1Ai,BiN1\leq A_i,B_i \leq N
  • It is possible to travel between every pair of cities using roads.

Input

Input is given from Standard Input in the following format:

NN
A1A_1 B1B_1
\vdots
AN1A_{N-1} BN1B_{N-1}

Output

Print the sequence of cities visited by Takahashi in the order he visits them, including City 11 at the beginning and end of the journey, with spaces in between.


Sample Input 1Copy

Copy
4
1 2
4 2
3 1

Sample Output 1Copy

Copy
1 2 4 2 1 3 1

His journey will be as follows.

  • Start in City 11.
  • The unvisited cities directly connected to City 11 are City 22 and 33. Go to the city with the smaller number, that is, City 22.
  • The unvisited city directly connected to City 22 is City 44. Go there.
  • There is no unvisited city directly connected to City 44. Go back to City 22.
  • There is no unvisited city directly connected to City 22. Go to City 11, where he was in just before visiting City 22 for the first time.
  • The unvisited city directly connected to City 11 is City 33. Go there.
  • There is no unvisited city directly connected to City 33. Go back to City 11.
  • There is no unvisited city directly connected to City 11. End the journey.

Sample Input 2Copy

Copy
5
1 2
1 3
1 4
1 5

Sample Output 2Copy

Copy
1 2 1 3 1 4 1 5 1


2025-04-25 (Fri)
10:01:59 +00:00