C - 電車の連結順序 解説 /

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300 点

問題文

高橋君は鉄道模型が趣味で、N 両の車両を連結して一本の列車を作っていました。車両には 1 から N までの番号が重複なく付けられています。

ある日、青木君がいたずらで車両をすべてバラバラにしてしまいました。高橋君は元の連結順序に戻したいのですが、どの車両が先頭から何両目だったかを覚えていません。

幸い、高橋君は各車両の連結情報を記録していました。車両 i(1 \leq i \leq N)について、以下の情報が分かっています。

  • prev_i:車両 i の先頭側に直接連結されていた車両の番号。車両 i が先頭車両の場合は prev_i = 0。
  • next_i:車両 i の最後尾側に直接連結されていた車両の番号。車両 i が最後尾車両の場合は next_i = 0。

ここで「先頭側」とは列車の先頭に近い方向、「最後尾側」とは列車の最後尾に近い方向を指します。

N 両すべての連結情報が与えられるので、先頭車両から最後尾車両までの車両番号を元の連結順序で出力してください。

制約

  • 1 \leq N \leq 10^5
  • 0 \leq prev_i \leq N(1 \leq i \leq N)
  • 0 \leq next_i \leq N(1 \leq i \leq N)
  • prev_i \neq i かつ next_i \neq i(1 \leq i \leq N)
  • prev_i = 0 となる i はちょうど 1 つ存在する(先頭車両)。
  • next_i = 0 となる i はちょうど 1 つ存在する(最後尾車両)。
  • 与えられる連結情報は矛盾なく、N 両すべてが一本の列車として連結された正しい順序を構成する。すなわち、先頭車両から next を順にたどると N 両すべてをちょうど 1 回ずつ通って最後尾車両に到達する。また、next_j = k(k \neq 0)ならば prev_k = j が成り立ち、prev_k = j(j \neq 0)ならば next_j = k が成り立つ。
  • 入力はすべて整数である。

入力

入力は N + 1 行からなります。

N
prev_1 next_1
prev_2 next_2
\vdots
prev_N next_N

1 行目には車両の数 N が与えられます。i + 1 行目(1 \leq i \leq N)には、車両 i の連結情報として prev_i と next_i が空白区切りで与えられます。

出力

元の連結順序に並べた車両番号を、先頭車両から順に 1 行に 1 つずつ、計 N 行で出力してください。


入力例 1

4
2 4
0 1
4 0
1 3

出力例 1

2
1
4
3

入力例 2

3
2 0
3 1
0 2

出力例 2

3
2
1

入力例 3

7
7 6
6 5
5 0
0 7
2 3
1 2
4 1

出力例 3

4
7
1
6
2
5
3

入力例 4

12
11 10
12 9
8 11
7 0
9 7
10 12
5 4
0 3
2 5
1 6
3 1
6 2

出力例 4

8
3
11
1
10
6
12
2
9
5
7
4

入力例 5

1
0 0

出力例 5

1

Score : 300 pts

Problem Statement

Takahashi's hobby is model railroads, and he had built a single train by coupling N cars together. The cars are uniquely numbered from 1 to N.

One day, Aoki mischievously disconnected all the cars. Takahashi wants to restore the original coupling order, but he doesn't remember which car was in which position from the front.

Fortunately, Takahashi had recorded the coupling information for each car. For car i (1 \leq i \leq N), the following information is known:

  • prev_i: The number of the car that was directly coupled to the front side of car i. If car i was the front car, then prev_i = 0.
  • next_i: The number of the car that was directly coupled to the rear side of car i. If car i was the rear car, then next_i = 0.

Here, "front side" refers to the direction toward the front of the train, and "rear side" refers to the direction toward the rear of the train.

Given the coupling information for all N cars, output the car numbers in the original coupling order from the front car to the rear car.

Constraints

  • 1 \leq N \leq 10^5
  • 0 \leq prev_i \leq N (1 \leq i \leq N)
  • 0 \leq next_i \leq N (1 \leq i \leq N)
  • prev_i \neq i and next_i \neq i (1 \leq i \leq N)
  • There is exactly one i such that prev_i = 0 (the front car).
  • There is exactly one i such that next_i = 0 (the rear car).
  • The given coupling information is consistent and forms a valid order in which all N cars are coupled as a single train. That is, starting from the front car and following next successively, all N cars are visited exactly once before reaching the rear car. Furthermore, if next_j = k (k \neq 0), then prev_k = j holds, and if prev_k = j (j \neq 0), then next_j = k holds.
  • All input values are integers.

Input

The input consists of N + 1 lines.

N
prev_1 next_1
prev_2 next_2
\vdots
prev_N next_N

The first line gives the number of cars N. The (i + 1)-th line (1 \leq i \leq N) gives the coupling information for car i: prev_i and next_i, separated by a space.

Output

Output the car numbers arranged in the original coupling order, one per line from the front car to the rear car, for a total of N lines.


Sample Input 1

4
2 4
0 1
4 0
1 3

Sample Output 1

2
1
4
3

Sample Input 2

3
2 0
3 1
0 2

Sample Output 2

3
2
1

Sample Input 3

7
7 6
6 5
5 0
0 7
2 3
1 2
4 1

Sample Output 3

4
7
1
6
2
5
3

Sample Input 4

12
11 10
12 9
8 11
7 0
9 7
10 12
5 4
0 3
2 5
1 6
3 1
6 2

Sample Output 4

8
3
11
1
10
6
12
2
9
5
7
4

Sample Input 5

1
0 0

Sample Output 5

1