A - お弁当の注文

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

配点 : 233

問題文

高橋君はお弁当屋さんで働いています。お店では N 種類のおかずを取り扱っており、おかずには 1 から N までの番号が付いています。おかず i の価格は W_i 円です。

今日は M 件の注文が入っています。 j 件目の注文では K_j 種類のおかずが選ばれており、具体的にはおかず A_{j, 1}, A_{j, 2}, \dots, A_{j, K_j} が選ばれています。同じ注文の中で同じおかずが複数回選ばれることはありません。

各注文について、選ばれたおかずの合計金額を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq W_i \leq 10^4 (1 \leq i \leq N)
  • 1 \leq K_j \leq N (1 \leq j \leq M)
  • 1 \leq A_{j, k} \leq N (1 \leq j \leq M,\ 1 \leq k \leq K_j)
  • j 件目の注文において、A_{j, 1}, A_{j, 2}, \dots, A_{j, K_j} はすべて異なる
  • \displaystyle \sum_{j=1}^{M} K_j \leq 10^6
  • 入力はすべて整数である

入力

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

N M
W_1 W_2 \dots W_N
K_1 A_{1, 1} A_{1, 2} \dots A_{1, K_1}
K_2 A_{2, 1} A_{2, 2} \dots A_{2, K_2}
\vdots
K_M A_{M, 1} A_{M, 2} \dots A_{M, K_M}
  • 1 行目には、おかずの種類数 N と注文の件数 M がスペース区切りで与えられる。
  • 2 行目には、各おかずの価格を表す N 個の整数 W_1, W_2, \dots, W_N がスペース区切りで与えられる。
  • 続く M 行にわたって、各注文で選ばれたおかずの情報が与えられる。
  • 2 + j 行目には、 j 件目の注文で選ばれたおかずの数 K_j と、選ばれたおかずの番号を表す K_j 個の整数 A_{j, 1}, A_{j, 2}, \dots, A_{j, K_j} がスペース区切りで与えられる。

出力

出力は M 行からなる。

j 行目には、 j 件目の注文で選ばれたおかずの合計金額を出力せよ。


入力例 1

4 3
300 500 120 450
2 1 3
3 2 4 1
1 2

出力例 1

420
1250
500

入力例 2

5 4
1000 250 400 800 150
5 1 2 3 4 5
2 5 2
3 3 1 4
1 4

出力例 2

2600
400
2200
800

入力例 3

9 6
130 260 390 520 150 280 410 540 170
4 1 3 5 7
3 9 2 4
5 8 6 4 2 1
2 5 9
6 3 4 5 6 7 8
1 2

出力例 3

1080
950
1730
320
2290
260

入力例 4

20 10
95 180 260 340 420 510 605 715 830 940 55 165 275 385 495 610 720 845 960 1000
5 1 5 10 15 20
8 2 4 6 8 12 14 16 18
10 1 2 3 4 5 6 7 8 9 10
7 11 12 13 14 15 16 17
3 19 20 1
12 20 18 16 14 12 10 8 6 4 2 1 3
1 11
9 5 7 9 11 13 15 17 19 20
4 3 6 9 12
20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

出力例 4

2950
3750
4895
2705
2055
6045
55
5360
1765
10405

入力例 5

1 1
9999
1 1

出力例 5

9999

Score : 233 pts

Problem Statement

Takahashi works at a bento (lunch box) shop. The shop offers N types of side dishes, numbered from 1 to N. The price of side dish i is W_i yen.

Today, there are M orders. The j-th order has K_j types of side dishes selected, specifically side dishes A_{j, 1}, A_{j, 2}, \dots, A_{j, K_j}. The same side dish is never selected more than once within the same order.

For each order, calculate the total price of the selected side dishes.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq W_i \leq 10^4 (1 \leq i \leq N)
  • 1 \leq K_j \leq N (1 \leq j \leq M)
  • 1 \leq A_{j, k} \leq N (1 \leq j \leq M,\ 1 \leq k \leq K_j)
  • In the j-th order, A_{j, 1}, A_{j, 2}, \dots, A_{j, K_j} are all distinct
  • \displaystyle \sum_{j=1}^{M} K_j \leq 10^6
  • All input values are integers

Input

The input is given in the following format.

N M
W_1 W_2 \dots W_N
K_1 A_{1, 1} A_{1, 2} \dots A_{1, K_1}
K_2 A_{2, 1} A_{2, 2} \dots A_{2, K_2}
\vdots
K_M A_{M, 1} A_{M, 2} \dots A_{M, K_M}
  • The first line contains the number of side dish types N and the number of orders M, separated by a space.
  • The second line contains N integers W_1, W_2, \dots, W_N representing the price of each side dish, separated by spaces.
  • The following M lines contain the information about the side dishes selected in each order.
  • The (2 + j)-th line contains the number of side dishes selected in the j-th order K_j, followed by K_j integers A_{j, 1}, A_{j, 2}, \dots, A_{j, K_j} representing the numbers of the selected side dishes, separated by spaces.

Output

The output consists of M lines.

On the j-th line, output the total price of the side dishes selected in the j-th order.


Sample Input 1

4 3
300 500 120 450
2 1 3
3 2 4 1
1 2

Sample Output 1

420
1250
500

Sample Input 2

5 4
1000 250 400 800 150
5 1 2 3 4 5
2 5 2
3 3 1 4
1 4

Sample Output 2

2600
400
2200
800

Sample Input 3

9 6
130 260 390 520 150 280 410 540 170
4 1 3 5 7
3 9 2 4
5 8 6 4 2 1
2 5 9
6 3 4 5 6 7 8
1 2

Sample Output 3

1080
950
1730
320
2290
260

Sample Input 4

20 10
95 180 260 340 420 510 605 715 830 940 55 165 275 385 495 610 720 845 960 1000
5 1 5 10 15 20
8 2 4 6 8 12 14 16 18
10 1 2 3 4 5 6 7 8 9 10
7 11 12 13 14 15 16 17
3 19 20 1
12 20 18 16 14 12 10 8 6 4 2 1 3
1 11
9 5 7 9 11 13 15 17 19 20
4 3 6 9 12
20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Sample Output 4

2950
3750
4895
2705
2055
6045
55
5360
1765
10405

Sample Input 5

1 1
9999
1 1

Sample Output 5

9999
B - メッセージの転送

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

配点 : 300

問題文

高橋君は N 人のメンバーからなるグループでメッセージの転送実験を行っています。

メンバーには 1 から N までの番号が付けられており、メンバー i (1 \leq i \leq N) にはあらかじめ転送先のメンバー番号 P_i がちょうど 1 つ設定されています(ただし P_i \neq i)。

高橋君はメンバー 1 にメッセージを渡します。メッセージは常にちょうど 1 人のメンバーだけが持っており、最初はメンバー 1 が持っています。メッセージを受け取ったメンバーは 受け取り済み として記録されます。メンバー 1 は最初にメッセージを受け取ったものとして、受け取り済みとなります。

その後、転送は次のルールに従って繰り返し行われます:

  1. 現在メッセージを持っているメンバーを i とする。
  2. メンバー i の転送先であるメンバー P_i が受け取り済みであるかを確認する。
  • メンバー P_iまだ受け取り済みでない 場合、メンバー i はメッセージをメンバー P_i へ転送する。これにより、メンバー i はメッセージを持たなくなり、メンバー P_i がメッセージを持つようになる。メンバー P_i は受け取り済みとして記録される。その後、ステップ 1 に戻る。
  • メンバー P_iすでに受け取り済みである 場合、転送は行われず、メンバー i がメッセージを持ったまま実験は終了する。

実験が終了するまでに受け取り済みとなったメンバーの人数(メンバー 1 を含む)を求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq P_i \leq N
  • P_i \neq i (1 \leq i \leq N)
  • 入力はすべて整数

入力

N
P_1 P_2 \ldots P_N
  • 1 行目には、メンバーの人数を表す整数 N が与えられる。
  • 2 行目には、メンバー 1, 2, \ldots, N のそれぞれの転送先の番号を表す整数 P_1, P_2, \ldots, P_N が、この順にスペース区切りで与えられる。

出力

実験が終了するまでに受け取り済みとなったメンバーの人数を 1 行で出力してください。


入力例 1

4
2 3 4 1

出力例 1

4

入力例 2

6
3 1 2 5 6 4

出力例 2

3

入力例 3

10
2 3 1 5 6 7 8 9 10 4

出力例 3

3

Score : 300 pts

Problem Statement

Takahashi is conducting a message forwarding experiment with a group of N members.

The members are numbered from 1 to N, and each member i (1 \leq i \leq N) has exactly one predetermined forwarding destination member number P_i (where P_i \neq i).

Takahashi gives a message to member 1. The message is always held by exactly one member, and initially member 1 holds it. A member who receives the message is recorded as received. Member 1 is considered to have received the message at the start, and is thus marked as received.

After that, forwarding is repeated according to the following rules:

  1. Let i be the member currently holding the message.
  2. Check whether member P_i, the forwarding destination of member i, has already received the message.
  • If member P_i has not yet received the message, member i forwards the message to member P_i. As a result, member i no longer holds the message, and member P_i now holds the message. Member P_i is recorded as received. Then, return to step 1.
  • If member P_i has already received the message, no forwarding takes place, and the experiment ends with member i still holding the message.

Determine the number of members who have been recorded as received by the time the experiment ends (including member 1).

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq P_i \leq N
  • P_i \neq i (1 \leq i \leq N)
  • All inputs are integers

Input

N
P_1 P_2 \ldots P_N
  • The first line contains an integer N, representing the number of members.
  • The second line contains integers P_1, P_2, \ldots, P_N, representing the forwarding destination numbers of members 1, 2, \ldots, N respectively, separated by spaces in this order.

Output

Print in one line the number of members who have been recorded as received by the time the experiment ends.


Sample Input 1

4
2 3 4 1

Sample Output 1

4

Sample Input 2

6
3 1 2 5 6 4

Sample Output 2

3

Sample Input 3

10
2 3 1 5 6 7 8 9 10 4

Sample Output 3

3
C - 花壇の水やり

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

配点 : 366

問題文

高橋君は、庭にある花壇の管理を任されました。

花壇には N 個の区画が一列に並んでおり、左から順に区画 1 、区画 2 、…、区画 N と番号が付けられています。最初、区画 i の土には A_i リットルの水分が含まれています。

高橋君はこれから M 回の作業を順番に行います。 j 回目( 1 \leq j \leq M )の作業では、区画 L_j から区画 R_j までの連続する範囲に含まれるすべての区画に対して、それぞれの水分量を D_j リットルだけ変化させます。すなわち、各区画の水分量に D_j を加えます( D_j が正のときは水やり、負のときは排水を意味します)。

ただし、どの時点においても、各区画の水分量は 0 以上 10^{18} 以下であることが保証されています。

すべての作業が終了した後の、各区画の水分量をそれぞれ求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^91 \leq i \leq N
  • 1 \leq L_j \leq R_j \leq N1 \leq j \leq M
  • -10^9 \leq D_j \leq 10^91 \leq j \leq M
  • どの時点においても、各区画の水分量は 0 以上 10^{18} 以下である
  • 入力はすべて整数である

入力

N M
A_1 A_2 \ldots A_N
L_1 R_1 D_1
L_2 R_2 D_2
\vdots
L_M R_M D_M
  • 1 行目には、区画の個数を表す整数 N と、作業の回数を表す整数 M が、スペース区切りで与えられる。
  • 2 行目には、各区画の初期水分量を表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
  • 続く M 行で、各作業の内容が与えられる。
  • そのうち j 行目(入力全体では 2 + j 行目)には、 j 回目の作業の対象範囲の左端 L_j 、右端 R_j 、水分の増減量 D_j が、スペース区切りで与えられる。

出力

すべての作業が終了した後の、区画 1 から区画 N までの水分量を整数としてスペース区切りで 1 行に出力してください。


入力例 1

5 3
10 5 8 3 6
1 3 2
2 5 -1
4 4 5

出力例 1

12 6 9 7 5

入力例 2

8 5
100 200 300 400 500 600 700 800
1 8 -50
3 6 100
1 4 -30
5 8 200
2 7 10

出力例 2

20 130 330 430 760 860 860 950

入力例 3

10 8
1000000000 500000000 0 300000000 700000000 100000000 999999999 250000000 800000000 450000000
1 10 1000000000
3 7 -500000000
1 5 200000000
6 10 300000000
2 4 -100000000
8 10 -200000000
1 1 500000000
5 9 100000000

出力例 3

2700000000 1600000000 600000000 900000000 1500000000 1000000000 1899999999 1450000000 2000000000 1550000000

Score : 366 pts

Problem Statement

Takahashi has been put in charge of managing a flower bed in the garden.

The flower bed consists of N sections arranged in a row, numbered section 1, section 2, …, section N from left to right. Initially, the soil in section i contains A_i liters of moisture.

Takahashi will perform M operations in order. In the j-th operation (1 \leq j \leq M), for every section in the contiguous range from section L_j to section R_j, he changes the moisture level by D_j liters. That is, he adds D_j to the moisture level of each such section (a positive D_j means watering, and a negative D_j means draining).

It is guaranteed that at any point in time, the moisture level of each section is between 0 and 10^{18}, inclusive.

After all operations are completed, determine the moisture level of each section.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq L_j \leq R_j \leq N (1 \leq j \leq M)
  • -10^9 \leq D_j \leq 10^9 (1 \leq j \leq M)
  • At any point in time, the moisture level of each section is between 0 and 10^{18}, inclusive
  • All input values are integers

Input

N M
A_1 A_2 \ldots A_N
L_1 R_1 D_1
L_2 R_2 D_2
\vdots
L_M R_M D_M
  • The first line contains an integer N representing the number of sections and an integer M representing the number of operations, separated by a space.
  • The second line contains integers A_1, A_2, \ldots, A_N representing the initial moisture levels of each section, separated by spaces.
  • The following M lines describe each operation.
  • The j-th of these lines (the (2 + j)-th line of the entire input) contains the left endpoint L_j, the right endpoint R_j, and the moisture change amount D_j for the j-th operation, separated by spaces.

Output

Print the moisture levels of sections 1 through N after all operations are completed, as integers separated by spaces, on a single line.


Sample Input 1

5 3
10 5 8 3 6
1 3 2
2 5 -1
4 4 5

Sample Output 1

12 6 9 7 5

Sample Input 2

8 5
100 200 300 400 500 600 700 800
1 8 -50
3 6 100
1 4 -30
5 8 200
2 7 10

Sample Output 2

20 130 330 430 760 860 860 950

Sample Input 3

10 8
1000000000 500000000 0 300000000 700000000 100000000 999999999 250000000 800000000 450000000
1 10 1000000000
3 7 -500000000
1 5 200000000
6 10 300000000
2 4 -100000000
8 10 -200000000
1 1 500000000
5 9 100000000

Sample Output 3

2700000000 1600000000 600000000 900000000 1500000000 1000000000 1899999999 1450000000 2000000000 1550000000
D - 登山ルートの選択

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

配点 : 400

問題文

高橋君は、山岳地帯のハイキングコースを計画しています。この山岳地帯には N 個の地点があり、各地点には 1 から N までの番号が付けられています。地点同士は M 本の双方向の登山道で結ばれています。ただし、すべての地点間が登山道で行き来できるとは限りません。

各地点 i (1 \leq i \leq N) には標高 H_i が設定されています。

高橋君は、地点 1 (登山口)から出発し、地点 N (山頂の展望台)に到達するルートを選びたいと考えています。ここで、ルートとは地点の列 p_1, p_2, \ldots, p_L であって、以下の条件をすべて満たすものを指します。

  • p_1 = 1 かつ p_L = N である。
  • すべての 1 \leq k \leq L-1 について、地点 p_k と地点 p_{k+1} を結ぶ登山道が存在する。
  • p_1, p_2, \ldots, p_L はすべて異なる(同じ地点を 2 度以上通らない)。

ルートに含まれる地点の個数 L を、そのルートの通過地点数と呼びます。通過地点数には出発地点 1 と到着地点 N の両方を含みます。上記の条件より、L \geq 2 です。

また、ルート上で通過するすべての地点の標高の最大値、すなわち \max(H_{p_1}, H_{p_2}, \ldots, H_{p_L}) を、そのルートの最高標高と呼びます。

ルートの最高標高が大きいほど、高山病のリスクが高まります。高橋君はできるだけ高山病のリスクを抑えたいと考えています。一方で、通過する地点が多いルートを選ぶと体力が持ちません。そこで高橋君は、通過地点数 LK 以下であるルートの中から選ぶことにしました。

通過地点数が K 以下であるルートの中で、最高標高として達成できる最小の値を求めてください。

条件を満たすルートが存在しない場合(地点 1 から地点 N に通過地点数 K 以下で到達できない場合)は -1 を出力してください。

制約

  • 2 \leq N \leq 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 2 \leq K \leq N
  • 1 \leq H_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq U_j < V_j \leq N (1 \leq j \leq M)
  • 同じ地点の組を結ぶ登山道は高々 1 本である(多重辺はない)
  • 自己ループはない
  • グラフは連結とは限らない
  • 入力はすべて整数である

入力

N M K
H_1 H_2 \ldots H_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M
  • 1 行目には、地点の数 N 、登山道の数 M 、通過地点数の上限 K が、スペース区切りで与えられる。
  • 2 行目には、各地点の標高 H_1, H_2, \ldots, H_N が、スペース区切りで与えられる。
  • 続く M 行のうち j 行目 (1 \leq j \leq M) には、j 番目の登山道が結ぶ 2 つの地点 U_jV_j がスペース区切りで与えられる。

出力

条件を満たすルートが存在する場合、最高標高として達成可能な最小の値を 1 行で出力せよ。条件を満たすルートが存在しない場合は -1 を出力せよ。


入力例 1

5 6 4
10 50 30 20 40
1 2
2 3
3 5
1 4
4 5
2 5

出力例 1

40

入力例 2

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

出力例 2

-1

入力例 3

8 10 5
5 100 3 7 200 4 8 6
1 2
1 3
2 5
3 4
4 6
6 7
7 8
5 8
2 8
3 7

出力例 3

8

入力例 4

10 15 4
10 500 20 15 300 25 12 8 400 30
1 2
1 3
1 7
2 5
3 4
4 6
6 10
7 8
8 10
5 10
2 10
3 10
4 10
7 10
9 10

出力例 4

30

入力例 5

2 1 2
1000000000 1
1 2

出力例 5

1000000000

Score : 400 pts

Problem Statement

Takahashi is planning a hiking course in a mountainous area. This mountainous area has N locations, each numbered from 1 to N. The locations are connected by M bidirectional mountain trails. However, it is not guaranteed that all locations are reachable from each other via trails.

Each location i (1 \leq i \leq N) has an elevation H_i.

Takahashi wants to choose a route starting from location 1 (the trailhead) and reaching location N (the summit observation deck). Here, a route is a sequence of locations p_1, p_2, \ldots, p_L that satisfies all of the following conditions:

  • p_1 = 1 and p_L = N.
  • For all 1 \leq k \leq L-1, there exists a mountain trail connecting location p_k and location p_{k+1}.
  • p_1, p_2, \ldots, p_L are all distinct (no location is visited more than once).

The number of locations L included in a route is called the number of waypoints of that route. The number of waypoints includes both the starting location 1 and the destination location N. From the above conditions, L \geq 2.

Additionally, the maximum elevation among all locations visited along the route, namely \max(H_{p_1}, H_{p_2}, \ldots, H_{p_L}), is called the maximum elevation of that route.

The higher the maximum elevation of a route, the greater the risk of altitude sickness. Takahashi wants to minimize the risk of altitude sickness as much as possible. On the other hand, choosing a route that passes through many locations will exhaust his stamina. Therefore, Takahashi has decided to choose from routes whose number of waypoints L is at most K.

Among all routes with a number of waypoints at most K, find the minimum achievable maximum elevation.

If no valid route exists (i.e., it is impossible to reach location N from location 1 with at most K waypoints), output -1.

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 2 \leq K \leq N
  • 1 \leq H_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq U_j < V_j \leq N (1 \leq j \leq M)
  • There is at most one trail connecting any pair of locations (no multi-edges)
  • There are no self-loops
  • The graph is not necessarily connected
  • All input values are integers

Input

N M K
H_1 H_2 \ldots H_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M
  • The first line contains the number of locations N, the number of trails M, and the upper limit on the number of waypoints K, separated by spaces.
  • The second line contains the elevations of each location H_1, H_2, \ldots, H_N, separated by spaces.
  • In the following M lines, the j-th line (1 \leq j \leq M) contains the two locations U_j and V_j connected by the j-th trail, separated by spaces.

Output

If a valid route exists, output the minimum achievable maximum elevation in one line. If no valid route exists, output -1.


Sample Input 1

5 6 4
10 50 30 20 40
1 2
2 3
3 5
1 4
4 5
2 5

Sample Output 1

40

Sample Input 2

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

Sample Output 2

-1

Sample Input 3

8 10 5
5 100 3 7 200 4 8 6
1 2
1 3
2 5
3 4
4 6
6 7
7 8
5 8
2 8
3 7

Sample Output 3

8

Sample Input 4

10 15 4
10 500 20 15 300 25 12 8 400 30
1 2
1 3
1 7
2 5
3 4
4 6
6 10
7 8
8 10
5 10
2 10
3 10
4 10
7 10
9 10

Sample Output 4

30

Sample Input 5

2 1 2
1000000000 1
1 2

Sample Output 5

1000000000
E - チーム分けの整合性

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

配点 : 466

問題文

N 人の人がおり、それぞれを 2 つのチームのいずれかに所属させることを考えます。ただし、どちらか一方のチームが空であっても構いません。

この問題では申告を扱います。申告は追加された順に 1, 2, \ldots と番号が付けられます。はじめ、申告は 0 件です。各申告は、対象となる 2 人の組 (u, v) と、内容 c からなります。内容 c0(同じチームに入るべき)、1(異なるチームに入るべき)、または X(未確定)のいずれかです。人 u と人 v の組は対称であり、(u, v)(v, u) は同じ組を表します。同じ人の組に対して複数の申告が存在することもあります。

Q 回の操作を順に行います。操作は次の 2 種類です。

  • A u v c(追加操作)

u と人 v の組を対象とする申告を末尾に追加します。追加される申告の内容は c です。

  • R k(伝搬操作)

申告 k現在の内容を読み取り、それに 1 を加えた値で申告 k+1 の内容を上書きします。申告 k+1 の対象となる人の組は変化しません。また、申告 k の内容も変化しません。

ここで加算は次のように定義します:0 + 1 = 11 + 1 = 0X + 1 = X(未確定のまま)。

注意: R 操作によって申告の内容は変更されることがあります。特に、申告 k の内容が X の場合、申告 k+1 の内容は(元々 01 であっても)X に上書きされます。ある申告の現在の内容は、その時点までに行われた全操作の履歴に依存します。

各操作の直後について、その時点で存在する全ての申告とそれらの現在の内容に基づく良い確定方法の個数を 998244353 で割った余りを求めてください。以下で用語を定義します。

確定方法

現在の全ての申告のうち、内容が X であるものそれぞれ0 または 1 を独立に選んで書き込み、全ての申告の内容を 01 にする方法を 確定方法 と呼びます。内容が既に 01 である申告の内容はそのまま変わりません。2 つの確定方法が異なるとは、ある内容 X の申告に対して書き込む値が異なることを意味します。内容が X の申告が 0 件の場合、確定方法はちょうど 1 通りです。

良い確定方法

ある確定方法によって全ての申告の内容を確定させたとき、全ての申告を同時に満たすチーム分け(各人を 2 つのチームのいずれかに割り当てる方法)が少なくとも 1 つ存在するならば、その確定方法を 良い確定方法 と呼びます。

ここで、内容が確定した申告 (u, v, c)c \in \{0, 1\})が満たされるとは、次を意味します。

  • c = 0 のとき:人 u と人 v が同じチームに属する
  • c = 1 のとき:人 u と人 v が異なるチームに属する

補足(グラフ理論的な言い換え): 各人を頂点、各申告を辺とする多重グラフ(u \neq v より自己ループはない)を考えます。確定後の各辺には 0 または 1 の重みが付いています。全ての申告を同時に満たすチーム分けが存在するための必要十分条件は、このグラフの全てのサイクルについて、そのサイクル上の辺の重みの XOR が 0 であることです。

制約

  • 2 \leq N \leq 3000
  • 1 \leq Q \leq 3000
  • A u v c において、1 \leq u, v \leq N かつ u \neq v
  • A u v c において、c01X のいずれかである
  • R k において、その操作時点での申告の総数を M とすると 1 \leq k \leq M - 1(申告 k と申告 k+1 がともに存在することが保証される)
  • 全操作を通じた申告の総数(A 操作の回数)は Q 以下である
  • R 操作では申告の追加・削除は行われない
  • N, Q, u, v, k は整数である

入力

N Q
query_1
query_2
\vdots
query_Q
  • 1 行目には、人数を表す整数 N と、操作回数を表す整数 Q が、スペース区切りで与えられる。
  • 続く Q 行では、操作 query_i が次のいずれかの形式で与えられる。
  • A u v c
  • u と人 v の組を対象とする申告を追加する。
  • c01X のいずれかである。
  • R k
  • 申告 k の現在の内容に 1 を加えた値で、申告 k+1 の内容を上書きする。

出力

Q 行出力せよ。

i 行目には、i 回目の操作の直後における良い確定方法の個数を 998244353 で割った余りを出力せよ。


入力例 1

3 5
A 1 2 0
A 2 3 X
A 1 3 1
R 2
R 1

出力例 1

1
2
1
2
1

入力例 2

3 6
A 1 2 0
A 2 3 0
A 1 3 1
A 1 3 X
R 3
R 1

出力例 2

1
1
0
0
0
0

入力例 3

7 15
A 1 2 X
A 2 3 0
A 3 4 1
A 4 5 X
A 5 1 0
R 1
A 2 5 1
R 5
A 6 7 X
A 1 7 1
R 7
A 3 6 0
R 8
R 3
A 2 4 X

出力例 3

2
2
2
4
2
4
2
2
4
4
8
4
8
4
4

入力例 4

12 30
A 1 2 0
A 2 3 X
A 3 4 1
A 4 5 0
A 5 6 X
A 6 1 1
R 2
A 7 8 0
A 8 9 X
A 9 10 1
A 10 11 0
A 11 12 X
A 12 7 1
R 8
R 11
A 1 7 X
A 3 9 0
R 12
A 5 11 1
R 14
A 2 8 X
A 4 10 0
R 16
A 6 12 1
R 17
R 1
R 3
A 1 12 X
R 18
A 2 11 0

出力例 4

1
2
2
2
4
2
4
4
8
8
8
16
8
16
32
64
32
32
16
16
16
0
16
8
16
8
16
16
16
8

入力例 5

2 1
A 1 2 X

出力例 5

2

Score : 466 pts

Problem Statement

There are N people, and we consider assigning each of them to one of 2 teams. Either team may be empty.

This problem deals with declarations. Declarations are numbered 1, 2, \ldots in the order they are added. Initially, there are 0 declarations. Each declaration consists of a pair of two people (u, v) as the target, and a content c. The content c is one of 0 (should be on the same team), 1 (should be on different teams), or X (undetermined). The pair of people u and v is symmetric; (u, v) and (v, u) represent the same pair. Multiple declarations may exist for the same pair of people.

Perform Q operations in order. There are two types of operations:

  • A u v c (Add operation)

Append a declaration targeting the pair of people u and v to the end. The content of the added declaration is c.

  • R k (Propagate operation)

Read the current content of declaration k, and overwrite the content of declaration k+1 with the value obtained by adding 1 to it. The target pair of people for declaration k+1 does not change. Also, the content of declaration k does not change.

Here, addition is defined as follows: 0 + 1 = 1, 1 + 1 = 0, X + 1 = X (remains undetermined).

Note: The R operation may change the content of a declaration. In particular, if the content of declaration k is X, the content of declaration k+1 is overwritten to X (even if it was originally 0 or 1). The current content of a declaration depends on the history of all operations performed up to that point.

After each operation, determine the number of good determination methods based on all existing declarations and their current contents at that point, modulo 998244353. The terms are defined below.

Determination Method

A determination method is a way to independently choose and write either 0 or 1 into each declaration whose content is X among all current declarations, making all declaration contents either 0 or 1. The contents of declarations that are already 0 or 1 remain unchanged. Two determination methods are different if they differ in the value written to some declaration with content X. If there are 0 declarations with content X, there is exactly 1 determination method.

Good Determination Method

A determination method is called a good determination method if, after determining all declaration contents using that method, there exists at least one team division (a way to assign each person to one of the two teams) that satisfies all declarations simultaneously.

Here, a declaration (u, v, c) with determined content (c \in \{0, 1\}) is satisfied when:

  • If c = 0: person u and person v belong to the same team
  • If c = 1: person u and person v belong to different teams

Supplement (graph-theoretic rephrasing): Consider a multigraph where each person is a vertex and each declaration is an edge (no self-loops since u \neq v). After determination, each edge has a weight of 0 or 1. The necessary and sufficient condition for the existence of a team division that satisfies all declarations simultaneously is that the XOR of edge weights along every cycle in this graph is 0.

Constraints

  • 2 \leq N \leq 3000
  • 1 \leq Q \leq 3000
  • In A u v c, 1 \leq u, v \leq N and u \neq v
  • In A u v c, c is one of 0, 1, X
  • In R k, letting M be the total number of declarations at the time of that operation, 1 \leq k \leq M - 1 (it is guaranteed that both declaration k and declaration k+1 exist)
  • The total number of declarations across all operations (number of A operations) is at most Q
  • R operations do not add or remove declarations
  • N, Q, u, v, k are integers

Input

N Q
query_1
query_2
\vdots
query_Q
  • The first line contains an integer N representing the number of people and an integer Q representing the number of operations, separated by a space.
  • The following Q lines give operation query_i in one of the following formats:
  • A u v c
  • Add a declaration targeting the pair of people u and v.
  • c is one of 0, 1, X.
  • R k
  • Overwrite the content of declaration k+1 with the value obtained by adding 1 to the current content of declaration k.

Output

Output Q lines.

On the i-th line, output the number of good determination methods immediately after the i-th operation, modulo 998244353.


Sample Input 1

3 5
A 1 2 0
A 2 3 X
A 1 3 1
R 2
R 1

Sample Output 1

1
2
1
2
1

Sample Input 2

3 6
A 1 2 0
A 2 3 0
A 1 3 1
A 1 3 X
R 3
R 1

Sample Output 2

1
1
0
0
0
0

Sample Input 3

7 15
A 1 2 X
A 2 3 0
A 3 4 1
A 4 5 X
A 5 1 0
R 1
A 2 5 1
R 5
A 6 7 X
A 1 7 1
R 7
A 3 6 0
R 8
R 3
A 2 4 X

Sample Output 3

2
2
2
4
2
4
2
2
4
4
8
4
8
4
4

Sample Input 4

12 30
A 1 2 0
A 2 3 X
A 3 4 1
A 4 5 0
A 5 6 X
A 6 1 1
R 2
A 7 8 0
A 8 9 X
A 9 10 1
A 10 11 0
A 11 12 X
A 12 7 1
R 8
R 11
A 1 7 X
A 3 9 0
R 12
A 5 11 1
R 14
A 2 8 X
A 4 10 0
R 16
A 6 12 1
R 17
R 1
R 3
A 1 12 X
R 18
A 2 11 0

Sample Output 4

1
2
2
2
4
2
4
4
8
8
8
16
8
16
32
64
32
32
16
16
16
0
16
8
16
8
16
16
16
8

Sample Input 5

2 1
A 1 2 X

Sample Output 5

2