A - ^{-1}

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

(1,2,…,N) を並び替えた数列 P と整数 X が与えられます。 数列 Pi 番目の項の値は P_i です。 P_k = X を満たす k を出力してください。

制約

  • 1 \leq N \leq 100
  • 1 \leq X \leq N
  • P(1,2,…,N) を並び替えてできる数列
  • 入力はすべて整数

入力

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

N X
P_1 P_2 \ldots P_N

出力

答えを出力せよ。


入力例 1

4 3
2 3 1 4

出力例 1

2

P = (2,3,1,4) なので、P_2 = 3 です。したがって、2 を出力します。


入力例 2

5 2
3 5 1 4 2

出力例 2

5

入力例 3

6 6
1 2 3 4 5 6

出力例 3

6

Score : 100 points

Problem Statement

You are given a sequence P that is a permutation of (1,2,…,N), and an integer X. The i-th term of P has a value of P_i. Print k such that P_k = X.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq X \leq N
  • P is a permutation of (1,2,…,N).
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N X
P_1 P_2 \ldots P_N

Output

Print the answer.


Sample Input 1

4 3
2 3 1 4

Sample Output 1

2

We have P = (2,3,1,4), so P_2 = 3. Thus, you should print 2.


Sample Input 2

5 2
3 5 1 4 2

Sample Output 2

5

Sample Input 3

6 6
1 2 3 4 5 6

Sample Output 3

6
B - Playing Cards Validation

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

英大文字および数字からなる 2 文字の文字列が N 個与えられます。i 個目の文字列は S_i です。
以下の 3 つの条件をすべて満たすか判定してください。
・すべての文字列に対して、1 文字目は H , D , C , S のどれかである。
・すべての文字列に対して、2 文字目は A , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , T , J , Q , K のどれかである。
・すべての文字列は相異なる。つまり、i \neq j ならば S_i \neq S_j である。

制約

  • 1 \leq N \leq 52
  • S_i は英大文字および数字からなる 2 文字の文字列

入力

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

N
S_1
S_2
\vdots
S_N

出力

3 つの条件をすべて満たす場合は Yes、そうでない場合は No を出力せよ。


入力例 1

4
H3
DA
D3
SK

出力例 1

Yes

このとき 3 つの条件をすべて満たすことが確認できます。


入力例 2

5
H3
DA
CK
H3
S7

出力例 2

No

S_1S_4 がともに H3 となってしまっているため、3 番目の条件に反します。


入力例 3

4
3H
AD
3D
KS

出力例 3

No

入力例 4

5
00
AA
XX
YY
ZZ

出力例 4

No

Score : 200 points

Problem Statement

You are given N strings, each of length 2, consisting of uppercase English letters and digits. The i-th string is S_i.
Determine whether the following three conditions are all satisfied.
・For every string, the first character is one of H, D, C, and S.
・For every string, the second character is one of A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K.
・All strings are pairwise different. That is, if i \neq j, then S_i \neq S_j.

Constraints

  • 1 \leq N \leq 52
  • S_i is a string of length 2 consisting of uppercase English letters and digits.

Input

The input is given from Standard Input in the following format:

N
S_1
S_2
\vdots
S_N

Output

If the three conditions are all satisfied, print Yes; otherwise, print No.


Sample Input 1

4
H3
DA
D3
SK

Sample Output 1

Yes

One can verify that the three conditions are all satisfied.


Sample Input 2

5
H3
DA
CK
H3
S7

Sample Output 2

No

Both S_1 and S_4 are H3, violating the third condition.


Sample Input 3

4
3H
AD
3D
KS

Sample Output 3

No

Sample Input 4

5
00
AA
XX
YY
ZZ

Sample Output 4

No
C - Ladder Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

10^9 階建てのビルがあり、N 本のはしごがかかっています。
ビルの 1 階にいる高橋君ははしごを繰り返し使って(0 回でもよい)できるだけ高い階へ上りたいと考えています。
はしごには 1 から N までの番号がついており、はしご iA_i 階と B_i 階を結んでいます。はしご i を利用すると A_i 階から B_i 階へ、または B_i 階から A_i 階へ双方向に移動することができますが、それ以外の階の間の移動は行うことはできません。
また、高橋君は同じ階での移動は自由に行うことができますが、はしご以外の方法で他の階へ移動することはできません。
高橋君は最高で何階へ上ることができますか?

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq 10^9
  • A_i \neq B_i
  • 入力はすべて整数

入力

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

N
A_1 B_1
A_2 B_2
\ldots
A_N B_N

出力

答えを出力せよ。


入力例 1

4
1 4
4 3
4 10
8 3

出力例 1

10

はしご 14 階に進み、はしご 310 階に進むことにより、10 階にたどり着くことができます。


入力例 2

6
1 3
1 5
1 12
3 5
3 12
5 12

出力例 2

12

入力例 3

3
500000000 600000000
600000000 700000000
700000000 800000000

出力例 3

1

他の階への移動ができない場合もあります。

Score : 300 points

Problem Statement

There is a 10^9-story building with N ladders.
Takahashi, who is on the 1-st (lowest) floor, wants to reach the highest floor possible by using ladders (possibly none).
The ladders are numbered from 1 to N, and ladder i connects the A_i-th and B_i-th floors. One can use ladder i in either direction to move from the A_i-th floor to the B_i-th floor or vice versa, but not between other floors.
Takahashi can freely move within the same floor, but cannot move between floors without using ladders.
What is the highest floor Takahashi can reach?

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq 10^9
  • A_i \neq B_i
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N
A_1 B_1
A_2 B_2
\ldots
A_N B_N

Output

Print an integer representing the answer.


Sample Input 1

4
1 4
4 3
4 10
8 3

Sample Output 1

10

He can reach the 10-th floor by using ladder 1 to get to the 4-th floor and then ladder 3 to get to the 10-th floor.


Sample Input 2

6
1 3
1 5
1 12
3 5
3 12
5 12

Sample Output 2

12

Sample Input 3

3
500000000 600000000
600000000 700000000
700000000 800000000

Sample Output 3

1

He may be unable to move between floors.

D - Takahashi's Solitaire

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

高橋君は N 枚のカードを手札として持っています。 i = 1, 2, \ldots, N について、i 番目のカードには非負整数 A_i が書かれています。

高橋君は、まず手札から好きなカードを 1 枚選んで、テーブルの上に置きます。 その後、高橋君は好きな回数( 0 回でも良い)だけ、下記の操作を繰り返します。

  • 最後にテーブルの上に置いたカードに書かれた整数を X とする。手札に整数 X または整数 (X+1)\bmod M が書かれたカードがあれば、そのうち好きなものを 1 枚選んで、テーブルの上に置く。ここで、(X+1)\bmod M(X+1)M で割ったあまりを表す。

最終的に手札に残ったカードに書かれている整数の総和としてあり得る最小値を出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 2 \leq M \leq 10^9
  • 0 \leq A_i \lt M
  • 入力はすべて整数

入力

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

N M
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

9 7
3 0 2 5 5 3 0 6 3

出力例 1

11

高橋君が、まず 4 番目のカード(書かれた整数は 5 )をテーブルの上に置き、その後、以下の手順で操作を行う場合を考えます。

  • 5 番目のカード(書かれた整数は 5 )をテーブルの上に置く。
  • 8 番目のカード(書かれた整数は 6 )をテーブルの上に置く。
  • 2 番目のカード(書かれた整数は 0 )をテーブルの上に置く。
  • 7 番目のカード(書かれた整数は 0 )をテーブルの上に置く。

このとき、最終的に手札に残ったカードは、1, 3, 6, 9 枚目のカードであり、それらに書かれた整数の総和は 3 + 2 + 3 +3 = 11 です。 これが、最終的に手札に残ったカードに書かれている整数の総和としてあり得る最小値です。


入力例 2

1 10
4

出力例 2

0

入力例 3

20 20
18 16 15 9 8 8 17 1 3 17 11 9 12 11 7 3 2 14 3 12

出力例 3

99

Score : 400 points

Problem Statement

Takahashi has N cards in his hand. For i = 1, 2, \ldots, N, the i-th card has an non-negative integer A_i written on it.

First, Takahashi will freely choose a card from his hand and put it on a table. Then, he will repeat the following operation as many times as he wants (possibly zero).

  • Let X be the integer written on the last card put on the table. If his hand contains cards with the integer X or the integer (X+1)\bmod M written on them, freely choose one of those cards and put it on the table. Here, (X+1)\bmod M denotes the remainder when (X+1) is divided by M.

Print the smallest possible sum of the integers written on the cards that end up remaining in his hand.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 2 \leq M \leq 10^9
  • 0 \leq A_i \lt M
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N M
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

9 7
3 0 2 5 5 3 0 6 3

Sample Output 1

11

Assume that he first puts the fourth card (5 is written) on the table and then performs the following.

  • Put the fifth card (5 is written) on the table.
  • Put the eighth card (6 is written) on the table.
  • Put the second card (0 is written) on the table.
  • Put the seventh card (0 is written) on the table.

Then, the first, third, sixth, and ninth cards will end up remaining in his hand, and the sum of the integers on those cards is 3 + 2 + 3 +3 = 11. This is the minimum possible sum of the integers written on the cards that end up remaining in his hand.


Sample Input 2

1 10
4

Sample Output 2

0

Sample Input 3

20 20
18 16 15 9 8 8 17 1 3 17 11 9 12 11 7 3 2 14 3 12

Sample Output 3

99
E - Crystal Switches

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 個の頂点と M 本の辺からなる無向グラフが与えられます。
i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結ぶ無向辺であり、 a_i = 1 ならばはじめは通行可能、a_i = 0 ならばはじめは通行不能です。 また、頂点 s_1 、頂点 s_2\ldots 、頂点 s_KK 個の頂点にはスイッチがあります。

高橋君は、はじめ頂点 1 におり、「下記の移動スイッチを押す2 つの行動のどちらかを行うこと」を好きなだけ繰り返します。

  • 移動 : いまいる頂点と通行可能な辺を介して隣接する頂点を 1 つ選び、その頂点に移動する。
  • スイッチを押す : いまいる頂点にスイッチがあるならば、そのスイッチを押す。その結果、グラフ上のすべての辺の通行可能・通行不能の状態が反転する。すなわち、通行可能である辺は通行不能に、通行不能である辺は通行可能に変化する。

高橋君が頂点 N に到達することが可能かどうかを判定し、可能な場合は頂点 N に到達するまでに行う移動の回数としてあり得る最小値を出力してください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 0 \leq K \leq N
  • 1 \leq u_i, v_i \leq N
  • u_i \neq v_i
  • a_i \in \lbrace 0, 1\rbrace
  • 1 \leq s_1 \lt s_2 \lt \cdots \lt s_K \leq N
  • 入力はすべて整数

入力

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

N M K
u_1 v_1 a_1
u_2 v_2 a_2
\vdots
u_M v_M a_M
s_1 s_2 \ldots s_K

出力

高橋君が頂点 N に到達することが不可能な場合は -1 を、 可能な場合は頂点 N に到達するまでに行う移動の回数としてあり得る最小値を出力せよ。


入力例 1

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

出力例 1

5

高橋君は、下記の手順で行動することで頂点 N に到達できます。

  • 頂点 1 から頂点 2 に移動する。
  • 頂点 2 から頂点 3 に移動する。
  • 頂点 3 にあるスイッチを押す。これによって、グラフ上のすべての辺の通行可能・通行不能の状態が反転する。
  • 頂点 3 から頂点 1 に移動する。
  • 頂点 1 から頂点 4 に移動する。
  • 頂点 4 にあるスイッチを押す。これによって、グラフ上のすべての辺の通行可能・通行不能の状態が再び反転する。
  • 頂点 4 から頂点 5 に移動する。

この手順における移動の回数は 5 回であり、これが考えられる最小の回数です。


入力例 2

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

出力例 2

-1

与えられるグラフは、連結でないことや、多重辺を含むことがあります。 この入力例では、高橋君はどのように行動しても頂点 N に到達することはできないので、-1 を出力します。

Score : 500 points

Problem Statement

You are given an undirected graph consisting of N vertices and M edges.
For i = 1, 2, \ldots, M, the i-th edge is an undirected edge connecting vertex u_i and v_i that is initially passable if a_i = 1 and initially impassable if a_i = 0. Additionally, there are switches on K of the vertices: vertex s_1, vertex s_2, \ldots, vertex s_K.

Takahashi is initially on vertex 1, and will repeat performing one of the two actions below, Move or Hit Switch, which he may choose each time, as many times as he wants.

  • Move: Choose a vertex adjacent to the vertex he is currently on via an edge, and move to that vertex.
  • Hit Switch: If there is a switch on the vertex he is currently on, hit it. This will invert the passability of every edge in the graph. That is, a passable edge will become impassable, and vice versa.

Determine whether Takahashi can reach vertex N, and if he can, print the minimum possible number of times he performs Move before reaching vertex N.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 0 \leq K \leq N
  • 1 \leq u_i, v_i \leq N
  • u_i \neq v_i
  • a_i \in \lbrace 0, 1\rbrace
  • 1 \leq s_1 \lt s_2 \lt \cdots \lt s_K \leq N
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N M K
u_1 v_1 a_1
u_2 v_2 a_2
\vdots
u_M v_M a_M
s_1 s_2 \ldots s_K

Output

If Takahashi cannot reach vertex N, print -1; if he can, print the minimum possible number of times he performs Move before reaching vertex N.


Sample Input 1

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

Sample Output 1

5

Takahashi can reach vertex N as follows.

  • Move from vertex 1 to vertex 2.
  • Move from vertex 2 to vertex 3.
  • Hit the switch on vertex 3. This inverts the passability of every edge in the graph.
  • Move from vertex 3 to vertex 1.
  • Move from vertex 1 to vertex 4.
  • Hit the switch on vertex 4. This again inverts the passability of every edge in the graph.
  • Move from vertex 4 to vertex 5.

Here, Move is performed five times, which is the minimum possible number.


Sample Input 2

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

Sample Output 2

-1

The given graph may be disconnected or contain multi-edges. In this sample input, there is no way for Takahashi to reach vertex N, so you should print -1.

F - Sorting a Matrix

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 500

問題文

非負整数を要素とする HW 列の行列 A が与えられます。 1 \leq i \leq H かつ 1 \leq j \leq W を満たす整数の組 (i, j) について、 Ai 行目 j 列目の要素を A_{i, j} で表します。

A に対して以下の手順を行います。

  • まず、A の要素のうち 0 であるものそれぞれを、任意の正の整数で置き換える( 0 である要素が複数ある場合、それぞれを異なる正の整数で置き換えることもできます)。
  • その後、「下記の 2 つの操作のどちらかを行うこと」を好きな回数( 0 回でも良い)だけ行う。

    • 1 \leq i \lt j \leq H を満たす整数の組 (i, j) を選び、Ai 行目と j 行目を入れ替える。
    • 1 \leq i \lt j \leq W を満たす整数の組 (i, j) を選び、Ai 列目と j 列目を入れ替える。

A が次の条件を満たすようにすることができるかどうかを判定してください。

  • A_{1, 1} \leq A_{1, 2} \leq \cdots \leq A_{1, W} \leq A_{2, 1} \leq A_{2, 2} \leq \cdots \leq A_{2, W} \leq A_{3, 1} \leq \cdots \leq A_{H, 1} \leq A_{H, 2} \leq \cdots \leq A_{H, W}

  • 言い換えると、1 \leq i, i' \leq H および 1 \leq j, j' \leq W を満たす任意の 2 つの整数の組 (i, j)(i', j') について、下記の 2 つの条件がともに成り立つ。

    • i \lt i' ならば A_{i, j} \leq A_{i', j'}
    • i = i' かつ j \lt j' 」ならば A_{i, j} \leq A_{i', j'}

制約

  • 2 \leq H, W
  • H \times W \leq 10^6
  • 0 \leq A_{i, j} \leq H \times W
  • 入力はすべて整数

入力

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

H W
A_{1, 1} A_{1, 2} \ldots A_{1, W}
A_{2, 1} A_{2, 2} \ldots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \ldots A_{H, W}

出力

A が問題文中の条件を満たすようにできる場合は Yes を、できない場合は No を出力せよ。


入力例 1

3 3
9 6 0
0 4 0
3 0 3

出力例 1

Yes

以下の手順で操作を行うことで、A が問題文中の条件を満たすようにすることができるため、Yes を出力します。

  • まず、A0 である要素を下記の通りに置き換える。
9 6 8
5 4 4
3 1 3
  • 2 列目と 3 列目を入れ替える。その結果、A は下記の通りとなる。
9 8 6
5 4 4
3 3 1
  • 1 行目と 3 行目を入れ替える。その結果、A は下記の通りとなる。
3 3 1
5 4 4
9 8 6
  • 1 列目と 3 列目を入れ替える。その結果、A は下記の通りとなり、問題文中の条件を満たすようになる。
1 3 3
4 4 5
6 8 9

入力例 2

2 2
2 1
1 2

出力例 2

No

どのように操作を行っても A が問題文中の条件を満たすようにすることはできないため、No を出力します。

Score : 500 points

Problem Statement

You are given a matrix A whose elements are non-negative integers. For a pair of integers (i, j) such that 1 \leq i \leq H and 1 \leq j \leq W, let A_{i, j} denote the element at the i-th row and j-th column of A.

Let us perform the following procedure on A.

  • First, replace each element of A that is 0 with an arbitrary positive integer (if multiple elements are 0, they may be replaced with different positive integers).
  • Then, repeat performing one of the two operations below, which may be chosen each time, as many times as desired (possibly zero).

    • Choose a pair of integers (i, j) such that 1 \leq i \lt j \leq H and swap the i-th and j-th rows of A.
    • Choose a pair of integers (i, j) such that 1 \leq i \lt j \leq W and swap the i-th and j-th columns of A.

Determine whether A can be made to satisfy the following condition.

  • A_{1, 1} \leq A_{1, 2} \leq \cdots \leq A_{1, W} \leq A_{2, 1} \leq A_{2, 2} \leq \cdots \leq A_{2, W} \leq A_{3, 1} \leq \cdots \leq A_{H, 1} \leq A_{H, 2} \leq \cdots \leq A_{H, W}.
  • In other words, for every two pairs of integers (i, j) and (i', j') such that 1 \leq i, i' \leq H and 1 \leq j, j' \leq W, both of the following conditions are satisfied.

    • If i \lt i', then A_{i, j} \leq A_{i', j'}.
    • If i = i' and j \lt j', then A_{i, j} \leq A_{i', j'}.

Constraints

  • 2 \leq H, W
  • H \times W \leq 10^6
  • 0 \leq A_{i, j} \leq H \times W
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

H W
A_{1, 1} A_{1, 2} \ldots A_{1, W}
A_{2, 1} A_{2, 2} \ldots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \ldots A_{H, W}

Output

If A can be made to satisfy the condition in the problem statement, print Yes; otherwise, print No.


Sample Input 1

3 3
9 6 0
0 4 0
3 0 3

Sample Output 1

Yes

One can perform the operations as follows to make A satisfy the condition in the problem statement, so you should print Yes.

  • First, replace the elements of A that are 0, as shown below:
9 6 8
5 4 4
3 1 3
  • Swap the second and third columns. Then, A becomes:
9 8 6
5 4 4
3 3 1
  • Swap the first and third rows. Then, A becomes:
3 3 1
5 4 4
9 8 6
  • Swap the first and third columns. Then, A becomes the following and satisfies the condition in the problem statement.
1 3 3
4 4 5
6 8 9

Sample Input 2

2 2
2 1
1 2

Sample Output 2

No

There is no way to perform the operations to make A satisfy the condition in the problem statement, so you should print No.

G - Random Walk to Millionaire

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 個の頂点と M 本の辺からなる連結かつ単純な無向グラフが与えられます。
i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結んでいます。

高橋君は、はじめレベル0 の状態で頂点 1 におり、下記の行動をちょうど K 回行います。

  • まず、いまいる頂点に隣接する頂点の中から、1 つを等確率でランダムに選択し、その頂点に移動する。
  • その後、移動後の頂点 v に応じて、下記のイベントが発生します。
    • C_v = 0 のとき : 高橋君のレベルが 1 だけ増加する。
    • C_v = 1 のとき : 高橋君のいまのレベルを X とする。高橋君は X^2 円のお金を獲得する。

上記の K 回の行動の過程で高橋君が獲得するお金の合計金額の期待値を \mathrm{mod}\, 998244353 で出力してください(注記参照)。

注記

求める期待値は必ず有理数となることが証明できます。またこの問題の制約下では、その値を互いに素な 2 つの整数 P, Q を用いて \frac{P}{Q} と表したとき、R \times Q \equiv P\pmod{998244353} かつ 0 \leq R \lt 998244353 を満たす整数 R がただ一つ存在することが証明できます。この R を求めてください。

制約

  • 2 \leq N \leq 3000
  • N-1 \leq M \leq \min\lbrace N(N-1)/2, 3000\rbrace
  • 1 \leq K \leq 3000
  • 1 \leq u_i, v_i \leq N
  • u_i \neq v_i
  • i \neq j \implies \lbrace u_i, v_i\rbrace \neq \lbrace u_j, v_j \rbrace
  • 与えられるグラフは連結
  • C_i \in \lbrace 0, 1\rbrace
  • 入力はすべて整数

入力

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

N M K
u_1 v_1
u_2 v_2
\vdots
u_M v_M
C_1 C_2 \ldots C_N

出力

答えを出力せよ。


入力例 1

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

出力例 1

89349064

高橋君の移動経路として考えられるものは複数ありますが、ここでは例として、高橋君が頂点 1 を始点として、1 \rightarrow 2 \rightarrow 4 \rightarrow 5 \rightarrow 4 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 と移動する場合に獲得するお金の合計金額を計算します。

  1. 高橋君は 1 回目の行動で、いまいる頂点 1 に隣接する頂点 2 に移動します。C_2 = 0 であるため、その後高橋君のレベルが 1 に上がります。
  2. 高橋君は 2 回目の行動で、いまいる頂点 2 に隣接する頂点 4 に移動します。C_4 = 1 であるため、その後高橋君は 1^2 = 1 円を獲得します。
  3. 高橋君は 3 回目の行動で、いまいる頂点 4 に隣接する頂点 5 に移動します。C_5 = 0 であるため、その後高橋君のレベルが 2 に上がります。
  4. 高橋君は 4 回目の行動で、いまいる頂点 5 に隣接する頂点 4 に移動します。C_4 = 1 であるため、その後高橋君は 2^2 = 4 円を獲得します。
  5. 高橋君は 5 回目の行動で、いまいる頂点 4 に隣接する頂点 2 に移動します。C_2 = 0 であるため、その後高橋君のレベルが 3 に上がります。
  6. 高橋君は 6 回目の行動で、いまいる頂点 2 に隣接する頂点 1 に移動します。C_1 = 0 であるため、その後高橋君のレベルが 4 に上がります。
  7. 高橋君は 7 回目の行動で、いまいる頂点 1 に隣接する頂点 2 に移動します。C_2 = 0 であるため、その後高橋君のレベルが 5 に上がります。
  8. 高橋君は 8 回目の行動で、いまいる頂点 2 に隣接する頂点 3 に移動します。C_3 = 1 であるため、その後高橋君は 5^2 = 25 円を獲得します。

よって、高橋君が獲得するお金の合計金額は、1 + 4 + 25 = 30 円です。


入力例 2

8 12 20
7 6
2 6
6 4
2 1
8 5
7 2
7 5
3 7
3 5
1 8
6 3
1 4
0 0 1 1 0 0 0 0

出力例 2

139119094

Score : 600 points

Problem Statement

You are given a connected simple undirected graph consisting of N vertices and M edges.
For i = 1, 2, \ldots, M, the i-th edge connects vertex u_i and vertex v_i.

Takahashi starts at Level 0 on vertex 1, and will perform the following action exactly K times.

  • First, choose one of the vertices adjacent to the vertex he is currently on, uniformly at random, and move to the chosen vertex.
  • Then, the following happens according to the vertex v he has moved to.
    • If C_v = 0: Takahashi's Level increases by 1.
    • If C_v = 1: Takahashi receives a money of X^2 yen, where X is his current Level.

Print the expected value of the total amount of money Takahashi receives during the K actions above, modulo 998244353 (see Notes).

Notes

It can be proved that the sought expected value is always a rational number. Additionally, under the Constraints of this problem, when that value is represented as \frac{P}{Q} using two coprime integers P and Q, it can also be proved that there is a unique integer R such that R \times Q \equiv P\pmod{998244353} and 0 \leq R \lt 998244353. Find this R.

Constraints

  • 2 \leq N \leq 3000
  • N-1 \leq M \leq \min\lbrace N(N-1)/2, 3000\rbrace
  • 1 \leq K \leq 3000
  • 1 \leq u_i, v_i \leq N
  • u_i \neq v_i
  • i \neq j \implies \lbrace u_i, v_i\rbrace \neq \lbrace u_j, v_j \rbrace
  • The given graph is connected.
  • C_i \in \lbrace 0, 1\rbrace
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N M K
u_1 v_1
u_2 v_2
\vdots
u_M v_M
C_1 C_2 \ldots C_N

Output

Print the answer.


Sample Input 1

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

Sample Output 1

89349064

Among the multiple paths that Takahashi may traverse, let us take a case where Takahashi starts on vertex 1 and goes along the path 1 \rightarrow 2 \rightarrow 4 \rightarrow 5 \rightarrow 4 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3, and compute the total amount of money he receives.

  1. In the first action, he moves from vertex 1 to an adjacent vertex, vertex 2. Then, since C_2 = 0, his Level increases to 1.
  2. In the second action, he moves from vertex 2 to an adjacent vertex, vertex 4. Then, since C_4 = 1, he receives 1^2 = 1 yen.
  3. In the third action, he moves from vertex 4 to an adjacent vertex, vertex 5. Then, since C_5 = 0, his Level increases to 2.
  4. In the fourth action, he moves from vertex 5 to an adjacent vertex, vertex 4. Then, since C_4 = 1, he receives 2^2 = 4 yen.
  5. In the fifth action, he moves from vertex 4 to an adjacent vertex, vertex 2. Then, since C_2 = 0, his Level increases to 3.
  6. In the sixth action, he moves from vertex 2 to an adjacent vertex, vertex 1. Then, since C_1 = 0, his Level increases to 4.
  7. In the seventh action, he moves from vertex 1 to an adjacent vertex, vertex 2. Then, since C_2 = 0, his Level increases to 5.
  8. In the eighth action, he moves from vertex 2 to an adjacent vertex, vertex 3. Then, since C_3 = 1, he receives 5^2 = 25 yen.

Thus, he receives a total of 1 + 4 + 25 = 30 yen.


Sample Input 2

8 12 20
7 6
2 6
6 4
2 1
8 5
7 2
7 5
3 7
3 5
1 8
6 3
1 4
0 0 1 1 0 0 0 0

Sample Output 2

139119094
Ex - Constrained Sums

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 600

問題文

以下の条件すべてを満たす長さ N の整数列 X = (X_1, X_2, \ldots ,X_N) が存在するか判定し、存在する場合 1 通り構成してください。

  • すべての 1 \leq i \leq N に対して 0 \leq X_i \leq M
  • すべての 1 \leq i \leq Q に対して L_i \leq X_{A_i} + X_{B_i} \leq R_i

制約

  • 1 \leq N \leq 10000
  • 1 \leq M \leq 100
  • 1 \leq Q \leq 10000
  • 1 \leq A_i, B_i \leq N
  • 0 \leq L_i \leq R_i \leq 2 \times M
  • 入力はすべて整数

入力

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

N M Q
A_1 B_1 L_1 R_1
A_2 B_2 L_2 R_2
\vdots
A_Q B_Q L_Q R_Q

出力

問題文中の条件すべてを満たす整数列が存在する場合、そのうちの 1 つの X_1, X_2, \ldots, X_N を空白区切りで出力せよ。存在しない場合は -1 を出力せよ。


入力例 1

4 5 3
1 3 5 7
1 4 1 2
2 2 3 8

出力例 1

2 4 3 0

X = (2,4,3,0) のとき X_1 + X_3 = 5, X_1 + X_4 = 2, X_2 + X_2 = 8 よりすべての条件を満たします。この他、X = (0,2,5,2), X = (1,3,4,1) などもすべての条件を満たすため、これらを出力しても正解となります。


入力例 2

3 7 3
1 2 3 4
3 1 9 12
2 3 2 4

出力例 2

-1

すべての条件を満たす数列 X は存在しません。

Score : 600 points

Problem Statement

Determine whether there is a sequence of N integers X = (X_1, X_2, \ldots ,X_N) that satisfies all of the following conditions, and construct one such sequence if it exists.

  • 0 \leq X_i \leq M for every 1 \leq i \leq N.
  • L_i \leq X_{A_i} + X_{B_i} \leq R_i for every 1 \leq i \leq Q.

Constraints

  • 1 \leq N \leq 10000
  • 1 \leq M \leq 100
  • 1 \leq Q \leq 10000
  • 1 \leq A_i, B_i \leq N
  • 0 \leq L_i \leq R_i \leq 2 \times M
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N M Q
A_1 B_1 L_1 R_1
A_2 B_2 L_2 R_2
\vdots
A_Q B_Q L_Q R_Q

Output

If there is an integer sequence that satisfies all of the conditions in the Problem Statement, print the elements X_1, X_2, \ldots, X_N of one such sequence, separated by spaces. Otherwise, print -1.


Sample Input 1

4 5 3
1 3 5 7
1 4 1 2
2 2 3 8

Sample Output 1

2 4 3 0

For X = (2,4,3,0), we have X_1 + X_3 = 5, X_1 + X_4 = 2, and X_2 + X_2 = 8, so all conditions are satisfied. There are other sequences, such as X = (0,2,5,2) and X = (1,3,4,1), that satisfy all conditions, and those will also be accepted.


Sample Input 2

3 7 3
1 2 3 4
3 1 9 12
2 3 2 4

Sample Output 2

-1

No sequence X satisfies all conditions.