A - 散歩道のコイン集め

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

配点 : 233 点

問題文

高橋君は N 個の地点が一列に並ぶ散歩道を歩いています。地点には左から順に 1, 2, \ldots, N の番号が付いており、番号が小さい方を左、大きい方を右と呼びます。

この散歩道には M 枚のコインが落ちており、それぞれのコインは特定の地点に置かれています。 i 枚目のコインは地点 P_i に置かれており、その価値は V_i です。同じ地点に複数のコインが置かれていることもあります。

高橋君は地点 S から出発し、地点 T まで移動します。移動の仕方は次の通りです。

  • S < T のとき:地点 S, S+1, S+2, \ldots, T の順に右方向(番号が増える方向)へ 1 地点ずつ進み、各地点をちょうど 1 回ずつ訪れます。
  • S > T のとき:地点 S, S-1, S-2, \ldots, T の順に左方向(番号が減る方向)へ 1 地点ずつ進み、各地点をちょうど 1 回ずつ訪れます。
  • S = T のとき:移動せず、地点 S にとどまります。

いずれの場合も、出発地点 S と到着地点 T は訪れた地点に含まれます。

高橋君がある地点を訪れたとき、その地点に置かれているコインをすべて拾います。

高橋君が地点 S から地点 T まで移動したとき、拾ったコインの価値の合計を求めてください。コインを 1 枚も拾わなかった場合、合計は 0 です。

制約

  • 1 \leq N \leq 10^9
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq S \leq N
  • 1 \leq T \leq N
  • 1 \leq P_i \leq N (1 \leq i \leq M)
  • 1 \leq V_i \leq 10^4 (1 \leq i \leq M)
  • 入力はすべて整数である。

入力

N M S T
P_1 V_1
P_2 V_2
\vdots
P_M V_M
  • 1 行目には、地点の数を表す N 、コインの枚数を表す M 、出発地点を表す S 、到着地点を表す T が、スペース区切りで与えられる。
  • 続く M 行では、各コインの情報が与えられる。 M = 0 の場合、この部分は存在しない。
  • 1 + i 行目 (1 \leq i \leq M) では、 i 枚目のコインが置かれている地点の番号 P_i と、そのコインの価値 V_i が、スペース区切りで与えられる。

出力

高橋君が拾ったコインの価値の合計を 1 行で出力してください。


入力例 1

10 5 3 7
1 100
3 10
5 20
5 30
7 40

出力例 1

100

入力例 2

8 4 6 2
2 5
4 10
6 20
8 100

出力例 2

35

入力例 3

100 12 20 75
5 100
20 1
25 2
30 3
40 4
40 5
60 6
75 7
76 1000
90 9
55 10
10 11

出力例 3

38

入力例 4

1000000000 30 800000000 200000000
1 9999
1000000000 8888
800000000 10000
200000000 1
500000000 1234
750000000 4321
250000000 2222
199999999 7777
800000001 6666
600000000 3456
600000000 6544
300000000 1111
400000000 2222
700000000 3333
900000000 4444
100000000 5555
650000000 1010
550000000 2020
450000000 3030
350000000 4040
210000000 5050
790000000 6060
205000000 7070
795000000 8080
123456789 9090
987654321 123
333333333 321
666666666 654
777777777 987
222222222 789

出力例 4

73555

入力例 5

1 0 1 1

出力例 5

0

Score : 233 pts

Problem Statement

Takahashi is walking along a path with N points arranged in a line. The points are numbered 1, 2, \ldots, N from left to right, where smaller numbers are on the left and larger numbers are on the right.

There are M coins scattered along this path, each placed at a specific point. The i-th coin is placed at point P_i and has a value of V_i. Multiple coins may be placed at the same point.

Takahashi starts at point S and moves to point T. The movement rules are as follows:

  • When S < T: He moves one point at a time to the right (in the direction of increasing numbers) in the order S, S+1, S+2, \ldots, T, visiting each point exactly once.
  • When S > T: He moves one point at a time to the left (in the direction of decreasing numbers) in the order S, S-1, S-2, \ldots, T, visiting each point exactly once.
  • When S = T: He does not move and stays at point S.

In all cases, both the starting point S and the destination point T are included in the visited points.

When Takahashi visits a point, he picks up all coins placed at that point.

Find the total value of the coins Takahashi picks up when he moves from point S to point T. If he does not pick up any coins, the total is 0.

Constraints

  • 1 \leq N \leq 10^9
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq S \leq N
  • 1 \leq T \leq N
  • 1 \leq P_i \leq N (1 \leq i \leq M)
  • 1 \leq V_i \leq 10^4 (1 \leq i \leq M)
  • All input values are integers.

Input

N M S T
P_1 V_1
P_2 V_2
\vdots
P_M V_M
  • The first line contains N (the number of points), M (the number of coins), S (the starting point), and T (the destination point), separated by spaces.
  • The following M lines provide information about each coin. If M = 0, this part does not exist.
  • The (1 + i)-th line (1 \leq i \leq M) contains the point number P_i where the i-th coin is placed and its value V_i, separated by a space.

Output

Output the total value of the coins Takahashi picked up in a single line.


Sample Input 1

10 5 3 7
1 100
3 10
5 20
5 30
7 40

Sample Output 1

100

Sample Input 2

8 4 6 2
2 5
4 10
6 20
8 100

Sample Output 2

35

Sample Input 3

100 12 20 75
5 100
20 1
25 2
30 3
40 4
40 5
60 6
75 7
76 1000
90 9
55 10
10 11

Sample Output 3

38

Sample Input 4

1000000000 30 800000000 200000000
1 9999
1000000000 8888
800000000 10000
200000000 1
500000000 1234
750000000 4321
250000000 2222
199999999 7777
800000001 6666
600000000 3456
600000000 6544
300000000 1111
400000000 2222
700000000 3333
900000000 4444
100000000 5555
650000000 1010
550000000 2020
450000000 3030
350000000 4040
210000000 5050
790000000 6060
205000000 7070
795000000 8080
123456789 9090
987654321 123
333333333 321
666666666 654
777777777 987
222222222 789

Sample Output 4

73555

Sample Input 5

1 0 1 1

Sample Output 5

0
B - ロボットの宝集め

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

配点 : 333 点

問題文

高橋君は、1 から N まで番号のついた N 個の部屋が一列に並んだ通路でロボットを操作します。各部屋 i (1 \leq i \leq N) には価値 A_i の宝が 1 つ置かれています。

ロボットには長さ Q の命令列 S が与えられます。ロボットは部屋 1 から出発し、獲得スコア 0 の状態で、命令列を先頭から順に 1 文字ずつ実行します。各文字の意味は次の通りです。

  • L: 現在の部屋が 1 より大きければ、左(番号が 1 小さい部屋)に移動する。そうでなければ何もしない。
  • R: 現在の部屋が N より小さければ、右(番号が 1 大きい部屋)に移動する。そうでなければ何もしない。
  • P: 現在いる部屋の宝がまだ回収されていなければ、その宝を回収し、獲得スコアにその宝の価値を加算する。すでに回収済みであれば何もしない。
  • B: 部屋 1 に移動する。

青木君は、命令列 S に対して M 回の操作を順に行います。操作は次の 2 種類です。

  • 1 p c: 命令列 S の p 文字目を文字 c に書き換える。
  • 2: 現在の命令列 S をロボットが実行したときの最終的な獲得スコアを求める。

操作 2 のたびに、ロボットは部屋 1 から出発し、すべての宝が未回収かつ獲得スコアが 0 の初期状態から命令列 S を実行します。この実行は質問に答えるためのシミュレーションであり、命令列 S への書き換えを除いて、各操作の結果が他の操作に影響することはありません。

各操作 2 について、答えを出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq A_i \leq 10^9
  • S は L, R, P, B からなる長さ Q の文字列である。
  • 書き換え操作において、1 \leq p \leq Q であり、c は L, R, P, B のいずれかである。
  • 操作 2 は 1 回以上与えられる。
  • 操作 2 の回数を K とすると、KQ \leq 2 \times 10^7 である。
  • N, Q, M, A_i, p はすべて整数である。

入力

N Q M
A_1 A_2 \dots A_N
S
op_1
op_2
:
op_M
  • 1 行目には、部屋の数 N、命令列の長さ Q、操作の回数 M がスペース区切りで与えられる。
  • 2 行目には、各部屋の宝の価値 A_1, A_2, \dots, A_N がスペース区切りで与えられる。
  • 3 行目には、長さ Q の文字列 S が与えられる。S の各文字は L, R, P, B のいずれかである。
  • 続く M 行のうち j 行目には、j 番目の操作 op_j が与えられる。
  • 書き換え操作の場合、1 p c の形式で与えられる。ここで p は書き換える位置、c は L, R, P, B のいずれかである。
  • 質問操作の場合、2 の形式で与えられる。

出力

各操作 2 について、その時点の命令列 S を実行したときの最終的な獲得スコアを、操作が与えられた順に 1 行ずつ出力してください。


入力例 1

5 8 6
3 10 5 8 2
PRRPLPBR
2
1 2 P
2
1 7 R
1 8 P
2

出力例 1

18
13
13

入力例 2

3 7 7
4 7 9
LLPPRRB
2
1 1 R
2
1 4 B
2
1 7 P
2

出力例 2

4
4
4
13

入力例 3

10 24 15
6 1 13 8 21 5 34 2 55 3
PRRPRRPLLBPRRPPBLLRPRBRP
2
1 10 R
2
1 1 B
1 18 P
2
1 24 L
2
1 6 B
1 7 R
2
1 15 L
2
1 3 P
2

出力例 3

41
54
54
54
28
28
15

入力例 4

30 80 25
12 45 7 100 23 56 89 14 67 31 90 4 28 73 11 62 39 85 16 54 200 3 99 41 77 25 68 150 8 33
PRRPLBRRPPBLLRPRBPLRRRPPBPLLRPRRBLPPRRLBPBRRLLPPRBRPLLRRPPBBRPLPBRRPPBLLRPRRLBPP
2
1 6 R
1 20 P
2
1 35 B
1 48 P
2
1 1 L
1 80 B
2
1 12 P
1 13 R
1 14 R
2
1 50 L
1 51 L
2
1 60 P
1 70 B
2
1 25 R
2
1 40 P
1 41 P
2

出力例 4

164
87
87
87
87
87
87
187
187

入力例 5

1 1 9
1000000000
P
2
1 1 L
2
1 1 R
2
1 1 B
2
1 1 P
2

出力例 5

1000000000
0
0
0
1000000000

Score : 333 pts

Problem Statement

Takahashi operates a robot in a corridor consisting of N rooms numbered from 1 to N arranged in a row. Each room i (1 \leq i \leq N) contains one treasure with value A_i.

The robot is given a command sequence S of length Q. The robot starts in room 1 with a score of 0, and executes the command sequence one character at a time from the beginning. The meaning of each character is as follows:

  • L: If the current room number is greater than 1, move left (to the room with number 1 smaller). Otherwise, do nothing.
  • R: If the current room number is less than N, move right (to the room with number 1 larger). Otherwise, do nothing.
  • P: If the treasure in the current room has not yet been collected, collect it and add its value to the score. If it has already been collected, do nothing.
  • B: Move to room 1.

Aoki performs M operations on the command sequence S in order. There are two types of operations:

  • 1 p c: Replace the p-th character of the command sequence S with character c.
  • 2: Determine the final score when the robot executes the current command sequence S.

For each operation 2, the robot starts from room 1 with all treasures uncollected and a score of 0, and executes the command sequence S from this initial state. This execution is a simulation to answer the query, and apart from the replacements to the command sequence S, the result of each operation does not affect other operations.

For each operation 2, output the answer.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq A_i \leq 10^9
  • S is a string of length Q consisting of L, R, P, B.
  • In replacement operations, 1 \leq p \leq Q and c is one of L, R, P, B.
  • At least one operation 2 is given.
  • Letting K be the number of operation 2 queries, KQ \leq 2 \times 10^7.
  • N, Q, M, A_i, p are all integers.

Input

N Q M
A_1 A_2 \dots A_N
S
op_1
op_2
:
op_M
  • The first line contains the number of rooms N, the length of the command sequence Q, and the number of operations M, separated by spaces.
  • The second line contains the values of the treasures in each room A_1, A_2, \dots, A_N, separated by spaces.
  • The third line contains the string S of length Q. Each character of S is one of L, R, P, B.
  • The following M lines each contain the j-th operation op_j on the j-th line.
  • For a replacement operation, it is given in the format 1 p c, where p is the position to replace and c is one of L, R, P, B.
  • For a query operation, it is given in the format 2.

Output

For each operation 2, output the final score when executing the command sequence S at that point, one per line in the order the operations are given.


Sample Input 1

5 8 6
3 10 5 8 2
PRRPLPBR
2
1 2 P
2
1 7 R
1 8 P
2

Sample Output 1

18
13
13

Sample Input 2

3 7 7
4 7 9
LLPPRRB
2
1 1 R
2
1 4 B
2
1 7 P
2

Sample Output 2

4
4
4
13

Sample Input 3

10 24 15
6 1 13 8 21 5 34 2 55 3
PRRPRRPLLBPRRPPBLLRPRBRP
2
1 10 R
2
1 1 B
1 18 P
2
1 24 L
2
1 6 B
1 7 R
2
1 15 L
2
1 3 P
2

Sample Output 3

41
54
54
54
28
28
15

Sample Input 4

30 80 25
12 45 7 100 23 56 89 14 67 31 90 4 28 73 11 62 39 85 16 54 200 3 99 41 77 25 68 150 8 33
PRRPLBRRPPBLLRPRBPLRRRPPBPLLRPRRBLPPRRLBPBRRLLPPRBRPLLRRPPBBRPLPBRRPPBLLRPRRLBPP
2
1 6 R
1 20 P
2
1 35 B
1 48 P
2
1 1 L
1 80 B
2
1 12 P
1 13 R
1 14 R
2
1 50 L
1 51 L
2
1 60 P
1 70 B
2
1 25 R
2
1 40 P
1 41 P
2

Sample Output 4

164
87
87
87
87
87
87
187
187

Sample Input 5

1 1 9
1000000000
P
2
1 1 L
2
1 1 R
2
1 1 B
2
1 1 P
2

Sample Output 5

1000000000
0
0
0
1000000000
C - お菓子の詰め合わせ

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

配点 : 366 点

問題文

高橋君はお菓子屋さんで N 個のお菓子を見つけました。i 番目 (1 \leq i \leq N) のお菓子のカロリーは正整数 A_i です。

高橋君は、これらのお菓子の中から 1 個以上を選んで詰め合わせを作ろうとしています。高橋君はカロリー管理にこだわりがあり、選んだお菓子のカロリーの総和が K で割り切れるようにしたいと考えています。

条件を満たすお菓子の選び方の数を求めてください。すなわち、\{1, 2, \ldots, N\} の空でない部分集合 S であって、\displaystyle\sum_{i \in S} A_i が K で割り切れるものの個数を求めてください。

ただし、お菓子は番号で区別します。カロリーの値が同じお菓子が複数あっても、番号が異なれば異なる選び方として数えます。

答えは非常に大きくなる場合があるので、998244353 で割った余りを出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 2 \times 10^5
  • N \times K \leq 2 \times 10^7
  • 1 \leq A_i \leq 10^9
  • 入力はすべて整数である。

入力

N K
A_1 A_2 \ldots A_N
  • 1 行目には、お菓子の個数を表す整数 N と、倍数の基準となる正整数 K が、スペース区切りで与えられる。
  • 2 行目には、各お菓子のカロリーを表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。

出力

選んだお菓子のカロリーの総和が K で割り切れるような、\{1, 2, \ldots, N\} の空でない部分集合の数を 998244353 で割った余りを 1 行で出力せよ。


入力例 1

4 3
1 2 3 4

出力例 1

5

入力例 2

2 5
1 2

出力例 2

0

入力例 3

12 10
7 13 20 4 16 9 25 30 11 6 18 2

出力例 3

411

入力例 4

50 60
1 999999937 120 45 78 300 17 2048 999999999 60 61 122 183 244 305 366 427 488 549 610 671 732 793 854 915 976 1037 1098 1159 1220 1281 1342 1403 1464 1525 1586 1647 1708 1769 1830 1891 1952 2013 2074 2135 2196 2257 2318 2379 1000000000

出力例 4

1108801

入力例 5

1 1
1000000000

出力例 5

1

Score : 366 pts

Problem Statement

Takahashi found N sweets in a sweet shop. The calorie of the i-th sweet (1 \leq i \leq N) is a positive integer A_i.

Takahashi wants to make an assortment by choosing one or more of these sweets. He is very particular about calorie control and wants the sum of the calories of the chosen sweets to be divisible by K.

Find the number of ways to choose sweets that satisfy this condition. Specifically, find the number of non-empty subsets S of \{1, 2, \ldots, N\} such that \displaystyle\sum_{i \in S} A_i is divisible by K.

Note that the sweets are distinguished by their indices. Even if multiple sweets have the same calorie value, they are treated as distinct if their indices are different.

Since the answer can be very large, find the count modulo 998244353.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 2 \times 10^5
  • N \times K \leq 2 \times 10^7
  • 1 \leq A_i \leq 10^9
  • All input values are integers.

Input

N K
A_1 A_2 \ldots A_N
  • The first line contains the integer N, representing the number of sweets, and the positive integer K, representing the divisor, separated by a space.
  • The second line contains the integers A_1, A_2, \ldots, A_N, representing the calories of each sweet, separated by spaces.

Output

Print the number of non-empty subsets of \{1, 2, \ldots, N\} such that the sum of the calories of the chosen sweets is divisible by K, modulo 998244353, in a single line.


Sample Input 1

4 3
1 2 3 4

Sample Output 1

5

Sample Input 2

2 5
1 2

Sample Output 2

0

Sample Input 3

12 10
7 13 20 4 16 9 25 30 11 6 18 2

Sample Output 3

411

Sample Input 4

50 60
1 999999937 120 45 78 300 17 2048 999999999 60 61 122 183 244 305 366 427 488 549 610 671 732 793 854 915 976 1037 1098 1159 1220 1281 1342 1403 1464 1525 1586 1647 1708 1769 1830 1891 1952 2013 2074 2135 2196 2257 2318 2379 1000000000

Sample Output 4

1108801

Sample Input 5

1 1
1000000000

Sample Output 5

1
D - お買い物チャレンジ

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

配点 : 400 点

問題文

高橋君はショッピングモールで開催されている「ぴったりお買い物チャレンジ」に参加しています。

このチャレンジのルールは次の通りです。会場には N 個の商品が 1 から N まで番号付けられて並んでおり、i 番目 (1 \leq i \leq N) の商品の価格は P_i 円です。高橋君はこれらの商品の中から 1 個以上を選んで購入し、選んだ商品の価格の合計がちょうど目標金額 S 円と一致すれば、チャレンジ成功となります。ただし、同じ商品を 2 回以上選ぶことはできません。

高橋君がチャレンジに臨むにあたって気にしているのは、成功する商品の選び方が何通りあるかです。成功する選び方が 1 通りもなければチャレンジは絶望的です。1 通りしかなければ、その唯一の組み合わせを正確に見つけ出さなければならず不安です。2 通り以上あれば、試行錯誤の余地があるので比較的気楽に挑めそうです。

ここで「選び方」とは、選ばれる商品の集合のことを指します。商品は番号で区別されるため、価格が同じ商品が複数あっても、それらは異なる商品として扱います。選ばれる商品の集合が異なれば、異なる選び方として数えます。

価格の合計がちょうど S 円になるような選び方の総数を C とするとき、C の値に応じて次のように出力してください。

  • C = 0 のとき:NO
  • C = 1 のとき:ALMOST
  • C \geq 2 のとき:YES

制約

  • 1 \leq N \leq 40
  • 1 \leq S \leq 10^{18}
  • 1 \leq P_i \leq 10^{18}
  • 入力はすべて整数

入力

N S
P_1 P_2 \ldots P_N
  • 1 行目には、商品の個数を表す整数 N と、目標金額を表す整数 S が、スペース区切りで与えられる。
  • 2 行目には、各商品の価格を表す N 個の整数 P_1, P_2, \ldots, P_N が、スペース区切りで与えられる。

出力

価格の合計がちょうど S 円となる選び方の総数 C に応じて、C = 0 なら NO を、C = 1 なら ALMOST を、C \geq 2 なら YES を 1 行で出力せよ。


入力例 1

4 7
2 3 4 5

出力例 1

YES

入力例 2

3 10
1 2 4

出力例 2

NO

入力例 3

12 50
1 2 4 8 16 32 64 128 256 512 1000 2000

出力例 3

ALMOST

入力例 4

35 1000000000000000000
1000000000000000000 1000000000000000000 600000000000000000 600000000000000001 600000000000000002 600000000000000003 600000000000000004 600000000000000005 600000000000000006 600000000000000007 600000000000000008 600000000000000009 600000000000000010 600000000000000011 600000000000000012 600000000000000013 600000000000000014 600000000000000015 600000000000000016 600000000000000017 600000000000000018 600000000000000019 600000000000000020 600000000000000021 600000000000000022 600000000000000023 600000000000000024 600000000000000025 600000000000000026 600000000000000027 600000000000000028 600000000000000029 600000000000000030 600000000000000031 600000000000000032

出力例 4

YES

入力例 5

1 1
1

出力例 5

ALMOST

Score : 400 pts

Problem Statement

Takahashi is participating in the "Exact Shopping Challenge" held at a shopping mall.

The rules of this challenge are as follows. There are N items lined up in the venue, numbered from 1 to N, and the price of the i-th item (1 \leq i \leq N) is P_i yen. Takahashi must select and purchase 1 or more of these items, and if the total price of the selected items exactly matches the target amount of S yen, the challenge is successful. However, the same item cannot be selected more than once.

What Takahashi is concerned about as he faces the challenge is how many ways there are to succeed. If there are 0 ways to succeed, the challenge is hopeless. If there is only 1 way, he must find that unique combination precisely, which makes him anxious. If there are 2 or more ways, there is room for trial and error, so he can approach it relatively relaxed.

Here, a "way of selecting" refers to the set of items chosen. Since items are distinguished by their numbers, even if multiple items have the same price, they are treated as different items. If the sets of selected items differ, they are counted as different ways.

Let C be the total number of ways to select items such that the total price is exactly S yen. Output the following according to the value of C:

  • When C = 0: NO
  • When C = 1: ALMOST
  • When C \geq 2: YES

Constraints

  • 1 \leq N \leq 40
  • 1 \leq S \leq 10^{18}
  • 1 \leq P_i \leq 10^{18}
  • All inputs are integers

Input

N S
P_1 P_2 \ldots P_N
  • The first line contains an integer N representing the number of items and an integer S representing the target amount, separated by a space.
  • The second line contains N integers P_1, P_2, \ldots, P_N representing the price of each item, separated by spaces.

Output

According to the total number C of ways to select items such that the total price is exactly S yen, output NO if C = 0, ALMOST if C = 1, or YES if C \geq 2, on a single line.


Sample Input 1

4 7
2 3 4 5

Sample Output 1

YES

Sample Input 2

3 10
1 2 4

Sample Output 2

NO

Sample Input 3

12 50
1 2 4 8 16 32 64 128 256 512 1000 2000

Sample Output 3

ALMOST

Sample Input 4

35 1000000000000000000
1000000000000000000 1000000000000000000 600000000000000000 600000000000000001 600000000000000002 600000000000000003 600000000000000004 600000000000000005 600000000000000006 600000000000000007 600000000000000008 600000000000000009 600000000000000010 600000000000000011 600000000000000012 600000000000000013 600000000000000014 600000000000000015 600000000000000016 600000000000000017 600000000000000018 600000000000000019 600000000000000020 600000000000000021 600000000000000022 600000000000000023 600000000000000024 600000000000000025 600000000000000026 600000000000000027 600000000000000028 600000000000000029 600000000000000030 600000000000000031 600000000000000032

Sample Output 4

YES

Sample Input 5

1 1
1

Sample Output 5

ALMOST
E - 鬼ごっこ

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

配点 : 433 点

問題文

高橋君と青木君が、N 個の部屋からなる建物の中で鬼ごっこをしています。部屋には 1 から N までの番号が付けられており、部屋間は M 本の一方通行の廊下で結ばれています。i 番目の廊下は部屋 U_i から部屋 V_i への一方向にのみ通行できます。同じ部屋から同じ部屋への廊下(自己ループ)は存在せず、同じ始点・終点の組を持つ廊下が複数存在することもありません。

高橋君が逃げる側、青木君が鬼(追いかける側)です。二人はともにグラフの構造を完全に知っており、互いの現在位置を常に把握しています。

ゲーム開始時、高橋君は部屋 S に、青木君は部屋 R にいます(S \neq R)。ゲーム開始時点では二人は異なる部屋にいるため捕獲は起こらず、直ちに最初のターンが始まります。各ターンは以下のステップ 1〜4 からなります。捕獲が発生しない限り、ターンは無限に繰り返されます。

  1. 高橋君の行動: 高橋君は、今いる部屋を始点とする廊下を 1 本選んでその先の部屋へ移動するか、現在の部屋にとどまるかを選びます。(今いる部屋を始点とする廊下が 1 本もない場合は、とどまることしかできません。)
  2. 捕獲判定: この時点で高橋君と青木君が同じ部屋にいれば、高橋君は捕まり、ゲーム終了です。
  3. 青木君の行動: 捕まっていなければ、青木君は、今いる部屋を始点とする廊下を 1 本選んでその先の部屋へ移動するか、現在の部屋にとどまるかを選びます。(今いる部屋を始点とする廊下が 1 本もない場合は、とどまることしかできません。)
  4. 捕獲判定: この時点で高橋君と青木君が同じ部屋にいれば、高橋君は捕まり、ゲーム終了です。

ステップ 2 およびステップ 4 のいずれでも捕まらなければ、次のターンのステップ 1 に進みます。

両者が最適に行動するとき、結果は次の二つのうちちょうど一方になります:

  • 捕まる: 高橋君がどのような戦略を取っても、青木君は有限ターン以内に必ず高橋君を捕まえることができる。
  • 逃げ切れる: 高橋君が適切な戦略を取れば、青木君がどのように行動しても永遠に捕まらずにいられる。

Q 個のクエリが与えられます。j 番目のクエリでは、高橋君の初期位置 S_j と青木君の初期位置 R_j が与えられるので、両者が最適に行動したとき高橋君が逃げ切れるかどうかを判定してください。各クエリは互いに独立です。

制約

  • 2 \leq N \leq 500
  • 0 \leq M \leq N(N-1)
  • 1 \leq U_i \leq N
  • 1 \leq V_i \leq N
  • U_i \neq V_i(自己ループはない)
  • (U_i, V_i) の組はすべて異なる(多重辺はない)
  • 1 \leq Q \leq 100000
  • 1 \leq S_j \leq N
  • 1 \leq R_j \leq N
  • S_j \neq R_j
  • (S_j, R_j) の組が複数のクエリで重複することもある
  • 入力はすべて整数

入力

N M
U_1 V_1
U_2 V_2
\vdots
U_M V_M
Q
S_1 R_1
S_2 R_2
\vdots
S_Q R_Q
  • 1 行目には、部屋の数を表す整数 N と、廊下の数を表す整数 M が、スペース区切りで与えられる。
  • 続く M 行のうち i 行目では、i 番目の廊下の始点 U_i と終点 V_i がスペース区切りで与えられる。
  • 次の行にはクエリ数 Q が与えられる。
  • 続く Q 行のうち j 行目では、j 番目のクエリにおける高橋君の初期位置 S_j と青木君の初期位置 R_j がスペース区切りで与えられる。

出力

Q 行出力してください。j 行目には、j 番目のクエリに対して、両者が最適に行動したとき高橋君が永遠に逃げ切れるなら YES を、高橋君が必ず有限ターン以内に捕まるなら NO を出力してください。


入力例 1

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

出力例 1

YES
YES
NO
YES
YES

入力例 2

5 5
1 2
2 3
3 4
4 5
5 4
6
1 3
5 4
4 5
2 5
3 1
5 1

出力例 2

YES
NO
NO
YES
NO
NO

入力例 3

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

出力例 3

YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
NO
YES

入力例 4

20 55
1 2
2 3
3 4
4 5
5 1
6 7
7 8
8 9
9 10
10 6
11 12
12 13
13 14
14 15
15 11
16 17
17 18
18 19
19 20
20 16
1 3
2 5
3 1
4 2
5 4
6 8
7 10
8 6
9 7
10 9
11 13
12 15
13 11
14 12
15 14
16 18
17 20
18 16
19 17
20 19
3 6
5 11
10 15
14 20
18 2
7 12
13 8
19 4
1 16
11 6
20 10
15 1
6 14
9 18
2 13
25
1 10
10 1
5 15
15 5
6 13
13 6
20 2
2 20
8 18
18 8
11 16
16 11
3 19
19 3
7 14
14 7
4 12
12 4
9 1
1 9
17 5
5 17
10 20
20 10
6 19

出力例 4

YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES

入力例 5

2 0
4
1 2
2 1
1 2
2 1

出力例 5

YES
YES
YES
YES

Score : 433 pts

Problem Statement

Takahashi and Aoki are playing tag in a building consisting of N rooms. The rooms are numbered from 1 to N, and they are connected by M one-way corridors. The i-th corridor allows passage only in one direction, from room U_i to room V_i. There are no corridors from a room to itself (no self-loops), and there are no multiple corridors with the same start and end points.

Takahashi is the runner (evader), and Aoki is the chaser (it). Both players have complete knowledge of the graph structure and always know each other's current position.

At the start of the game, Takahashi is in room S and Aoki is in room R (S \neq R). Since the two players are in different rooms at the start, no capture occurs, and the first turn begins immediately. Each turn consists of the following steps 1–4. Turns repeat indefinitely unless a capture occurs.

  1. Takahashi's action: Takahashi chooses one corridor starting from his current room and moves to the room at its end, or he chooses to stay in his current room. (If there are no corridors starting from his current room, he can only stay.)
  2. Capture check: If Takahashi and Aoki are in the same room at this point, Takahashi is captured and the game ends.
  3. Aoki's action: If Takahashi has not been captured, Aoki chooses one corridor starting from his current room and moves to the room at its end, or he chooses to stay in his current room. (If there are no corridors starting from his current room, he can only stay.)
  4. Capture check: If Takahashi and Aoki are in the same room at this point, Takahashi is captured and the game ends.

If Takahashi is not captured in either step 2 or step 4, the game proceeds to step 1 of the next turn.

When both players act optimally, the result is exactly one of the following two outcomes:

  • Captured: No matter what strategy Takahashi uses, Aoki can always capture Takahashi within a finite number of turns.
  • Escapes: If Takahashi uses an appropriate strategy, he can avoid being captured forever regardless of how Aoki acts.

You are given Q queries. In the j-th query, you are given Takahashi's initial position S_j and Aoki's initial position R_j. Determine whether Takahashi can escape forever when both players act optimally. Each query is independent.

Constraints

  • 2 \leq N \leq 500
  • 0 \leq M \leq N(N-1)
  • 1 \leq U_i \leq N
  • 1 \leq V_i \leq N
  • U_i \neq V_i (no self-loops)
  • All pairs (U_i, V_i) are distinct (no multiple edges)
  • 1 \leq Q \leq 100000
  • 1 \leq S_j \leq N
  • 1 \leq R_j \leq N
  • S_j \neq R_j
  • The pair (S_j, R_j) may appear in multiple queries
  • All inputs are integers

Input

N M
U_1 V_1
U_2 V_2
\vdots
U_M V_M
Q
S_1 R_1
S_2 R_2
\vdots
S_Q R_Q
  • The first line contains an integer N representing the number of rooms and an integer M representing the number of corridors, separated by a space.
  • The following M lines each contain the start U_i and end V_i of the i-th corridor, separated by a space.
  • The next line contains the number of queries Q.
  • The following Q lines each contain Takahashi's initial position S_j and Aoki's initial position R_j for the j-th query, separated by a space.

Output

Output Q lines. On the j-th line, output YES if Takahashi can escape forever when both players act optimally, or NO if Takahashi will always be captured within a finite number of turns.


Sample Input 1

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

Sample Output 1

YES
YES
NO
YES
YES

Sample Input 2

5 5
1 2
2 3
3 4
4 5
5 4
6
1 3
5 4
4 5
2 5
3 1
5 1

Sample Output 2

YES
NO
NO
YES
NO
NO

Sample Input 3

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

Sample Output 3

YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
NO
YES

Sample Input 4

20 55
1 2
2 3
3 4
4 5
5 1
6 7
7 8
8 9
9 10
10 6
11 12
12 13
13 14
14 15
15 11
16 17
17 18
18 19
19 20
20 16
1 3
2 5
3 1
4 2
5 4
6 8
7 10
8 6
9 7
10 9
11 13
12 15
13 11
14 12
15 14
16 18
17 20
18 16
19 17
20 19
3 6
5 11
10 15
14 20
18 2
7 12
13 8
19 4
1 16
11 6
20 10
15 1
6 14
9 18
2 13
25
1 10
10 1
5 15
15 5
6 13
13 6
20 2
2 20
8 18
18 8
11 16
16 11
3 19
19 3
7 14
14 7
4 12
12 4
9 1
1 9
17 5
5 17
10 20
20 10
6 19

Sample Output 4

YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES

Sample Input 5

2 0
4
1 2
2 1
1 2
2 1

Sample Output 5

YES
YES
YES
YES