C - 洞窟探検 解説 /

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

配点 : 366 点

問題文

高橋君は N 個の部屋からなる洞窟を探検します。部屋には 1 から N の番号が付いています。部屋と部屋の間には M 本の一方通行の通路があります。各通路は、ある部屋から別の部屋へ向かう向きが決まっています。

各部屋から出る通路には優先順位が定められています。この優先順位は入力で与えられる順番に対応しており、先に与えられた通路ほど優先順位が高くなります。

また、各部屋には照明が設置されており、その照明の色は赤(0)または青(1)のいずれかです。

高橋君は部屋 1 から探検を開始します。部屋 1 は探検開始時に訪問済みとなります。探検は以下のルールに従って行われます。

  1. 現在いる部屋から出ているすべての通路を、優先順位が高い順に 1 つずつ確認していきます。各通路について、その通路の先の部屋がすでに訪問済みであれば、その通路を飛ばして次の通路に進みます。通路の先の部屋が未訪問である場合、手順 2 に進みます。
  2. 確認した未訪問の部屋の照明の色が現在いる部屋の照明の色と同じ場合、その部屋には移動せず、その通路を飛ばして次の通路の確認に進みます。この部屋は訪問済みにはならず、今後別の機会に再び確認対象となりえます。
  3. 確認した未訪問の部屋の照明の色が現在いる部屋の照明の色と異なる場合、その部屋に移動し、訪問済みとします。移動先の部屋を新たに「現在の部屋」として、手順 1 からやり直します。
  4. 現在の部屋から出るすべての通路の確認が完了し、移動先が見つからなかった場合(すべて訪問済みまたは手順 2 で飛ばされた場合)、あるいはそもそも通路が出ていない場合は、バックトラックを行います。すなわち、通路の向きに関係なく、現在の部屋に移動してくる直前にいた部屋に戻ります。戻った先の部屋を「現在の部屋」として、その部屋で中断していた通路の確認を、中断した箇所の次の通路から再開し、手順 1〜3 の処理を続けます。
  5. 部屋 1 でバックトラックが発生した場合(すなわち、部屋 1 で移動先が見つからず、かつ戻れる部屋がない場合)、探検は終了します。

ここで、「中断していた通路の確認を再開する」とは、具体的には次のことを意味します。ある部屋 X で通路を優先順位順に確認している途中、手順 3 により別の部屋 Y に移動したとします。その後(部屋 Y 以降の探索やバックトラックを経て)部屋 X に戻ってきたとき、部屋 X では、部屋 Y への通路の次の優先順位の通路から確認を再開します。再開後も、各通路の先の部屋が訪問済みかどうかはその時点での状態で判定します。

探検終了時に、一度も訪れなかった部屋の番号を小さい順にすべて出力してください。すべての部屋を訪れた場合は COMPLETE と出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • C_i \in \{0, 1\} (1 \leq i \leq N)
  • 1 \leq U_j, V_j \leq N (1 \leq j \leq M)
  • U_j \neq V_j (1 \leq j \leq M)
  • 同じ (U_j, V_j) の組は複数回与えられない
  • グラフは連結とは限らない
  • 入力はすべて整数である

入力

N M
C_1 C_2 \cdots C_N
U_1 V_1
U_2 V_2
:
U_M V_M
  • 1 行目には、部屋の数を表す整数 N と、通路の本数を表す整数 M が、スペース区切りで与えられる。
  • 2 行目には、各部屋の照明の色を表す整数 C_i が N 個、スペース区切りで与えられる。C_i = 0 のとき赤、C_i = 1 のとき青を表す。
  • 3 行目から M 行にわたって、通路の情報が与えられる。各行の U_j V_j は「部屋 U_j から部屋 V_j への一方通行の通路がある」ことを意味する。同じ部屋 U_j から出る通路の優先順位は、入力で先に与えられたもの(上の行)ほど高い。

出力

一度も訪れなかった部屋の番号を小さい順にスペース区切りで 1 行で出力せよ。すべての部屋を訪れた場合は COMPLETE と出力せよ。


入力例 1

5 5
0 1 0 1 0
1 2
2 3
3 4
4 5
1 5

出力例 1

COMPLETE

入力例 2

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

出力例 2

2 4

入力例 3

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

出力例 3

COMPLETE

入力例 4

30 45
0 1 0 1 0 1 0 1 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0
1 3
1 2
1 4
2 5
2 6
5 7
5 8
8 9
8 10
10 11
11 12
12 13
12 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
20 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 1
4 9
4 10
6 13
6 14
7 15
9 16
13 21
21 22
3 6
3 7
11 4
18 2
23 5
25 9
27 13

出力例 4

3 6 7 9

入力例 5

1 0
0

出力例 5

COMPLETE

Score : 366 pts

Problem Statement

Takahashi explores a cave consisting of N rooms. The rooms are numbered from 1 to N. There are M one-way passages between rooms. Each passage has a fixed direction from one room to another.

For each room, the passages leading out of it have a defined priority order. This priority corresponds to the order given in the input, with passages given earlier having higher priority.

Additionally, each room has a light installed, and the color of the light is either red (0) or blue (1).

Takahashi starts exploring from room 1. Room 1 is marked as visited at the start of the exploration. The exploration follows these rules:

  1. Check all passages leading out of the current room one by one in order of decreasing priority. For each passage, if the room at the end of the passage has already been visited, skip that passage and move on to the next one. If the room at the end of the passage is unvisited, proceed to step 2.
  2. If the light color of the confirmed unvisited room is the same as the light color of the current room, do not move to that room, skip that passage, and continue checking the next passage. This room does not become visited, and it may be checked again on a different occasion in the future.
  3. If the light color of the confirmed unvisited room is different from the light color of the current room, move to that room and mark it as visited. Set the destination room as the new "current room" and restart from step 1.
  4. If all passages from the current room have been checked and no destination was found (all were visited or skipped in step 2), or if there are no passages leading out of the current room, perform a backtrack. That is, regardless of passage directions, return to the room you were in immediately before moving to the current room. Set that room as the "current room" and resume checking passages from where it was interrupted, starting from the next passage after the interruption point, continuing the process of steps 1–3.
  5. If a backtrack occurs at room 1 (that is, no destination is found from room 1 and there is no room to return to), the exploration ends.

Here, "resuming the interrupted passage check" specifically means the following: Suppose while checking passages in priority order from some room X, you moved to another room Y via step 3. After that (following exploration from room Y onward and backtracks), when you return to room X, you resume checking from the passage with the next priority after the passage to room Y. After resuming, whether each passage's destination room has been visited is judged based on the state at that point in time.

When the exploration ends, output all room numbers that were never visited in ascending order. If all rooms were visited, output COMPLETE.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • C_i \in \{0, 1\} (1 \leq i \leq N)
  • 1 \leq U_j, V_j \leq N (1 \leq j \leq M)
  • U_j \neq V_j (1 \leq j \leq M)
  • The same pair (U_j, V_j) is not given more than once
  • The graph is not necessarily connected
  • All input values are integers

Input

N M
C_1 C_2 \cdots C_N
U_1 V_1
U_2 V_2
:
U_M V_M
  • The first line contains an integer N representing the number of rooms and an integer M representing the number of passages, separated by a space.
  • The second line contains N integers C_i separated by spaces, representing the light color of each room. C_i = 0 means red, and C_i = 1 means blue.
  • From the third line onward, M lines of passage information are given. Each line U_j V_j means "there is a one-way passage from room U_j to room V_j." For passages leading out of the same room U_j, those given earlier in the input (upper lines) have higher priority.

Output

Output the room numbers that were never visited in ascending order, separated by spaces, on a single line. If all rooms were visited, output COMPLETE.


Sample Input 1

5 5
0 1 0 1 0
1 2
2 3
3 4
4 5
1 5

Sample Output 1

COMPLETE

Sample Input 2

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

Sample Output 2

2 4

Sample Input 3

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

Sample Output 3

COMPLETE

Sample Input 4

30 45
0 1 0 1 0 1 0 1 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0
1 3
1 2
1 4
2 5
2 6
5 7
5 8
8 9
8 10
10 11
11 12
12 13
12 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
20 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 1
4 9
4 10
6 13
6 14
7 15
9 16
13 21
21 22
3 6
3 7
11 4
18 2
23 5
25 9
27 13

Sample Output 4

3 6 7 9

Sample Input 5

1 0
0

Sample Output 5

COMPLETE