A - Legendary Players

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

AtCoder では、レーティング上位 10 人のハンドルネームには金色の冠が、上位 1 人のハンドルネームには白金色の冠が表示されます。

このコンテストが開始した時点で、アルゴリズム部門での上位 10 人に入っているプレイヤーのハンドルネームとレーティングは以下のようになっています。

tourist 3858
ksun48 3679
Benq 3658
Um_nik 3648
apiad 3638
Stonefeang 3630
ecnerwala 3613
mnbvmar 3555
newbiedmy 3516
semiexp 3481

上記のプレイヤーのハンドルネーム S が与えられるので、その人のレーティングを出力してください。

制約

  • S はアルゴリズム部門でレーティング上位 10 人に入っているプレイヤーのハンドルネームのいずれかと等しい。

入力

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

S

出力

対応するプレイヤーのレーティングを 1 行で出力せよ。


入力例 1

tourist

出力例 1

3858

このコンテストが開始した時点において、 tourist さんのアルゴリズム部門のレーティングは 3858 です。


入力例 2

semiexp

出力例 2

3481

このコンテストが開始した時点において、 semiexp さんのアルゴリズム部門のレーティングは 3481 です。

Score : 100 points

Problem Statement

In AtCoder, the top 10 rated players' usernames are displayed with a gold crown, and the top-rated player's username is displayed with a platinum crown.

At the start of this contest, the usernames and ratings of the top 10 rated players in the algorithm category are as follows:

tourist 3858
ksun48 3679
Benq 3658
Um_nik 3648
apiad 3638
Stonefeang 3630
ecnerwala 3613
mnbvmar 3555
newbiedmy 3516
semiexp 3481

You are given the username S of one of these players. Print that player's rating.

Constraints

  • S is equal to one of the usernames of the top 10 rated players in the algorithm category.

Input

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

S

Output

Print the rating of the corresponding player in one line.


Sample Input 1

tourist

Sample Output 1

3858

At the start of this contest, the rating of tourist in the algorithm category is 3858.


Sample Input 2

semiexp

Sample Output 2

3481

At the start of this contest, the rating of semiexp in the algorithm category is 3481.

B - Measure

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

正整数 N が与えられるので、下記で定まる長さ (N+1) の文字列 s_0s_1\ldots s_N を出力してください。

i = 0, 1, 2, \ldots, N について、

  • 1 以上 9 以下の N の約数 j であって、iN/j の倍数であるものが存在するとき、そのような j のうち最小のものに対応する数字を s_i とする。(よって、この場合 s_i12\ldots9 のいずれかである。)
  • そのような j が存在しないとき、s_i- とする。

制約

  • 1 \leq N \leq 1000
  • 入力はすべて整数

入力

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

N

出力

答えを出力せよ。


入力例 1

12

出力例 1

1-643-2-346-1

以下で、いくつかの i について s_i の決め方を説明します。

  • i = 0 について、1 以上 9 以下の N の約数 j であって iN/j の倍数であるものは、j = 1, 2, 3, 4, 65 個です。そのうち最小のものは 1 であるので、s_0 = 1 です。

  • i = 4 について、1 以上 9 以下の N の約数 j であって iN/j の倍数であるものは、j = 3, 62 個です。そのうち最小のものは 3 であるので、s_4 = 3 です。

  • i = 11 について、1 以上 9 以下の N の約数 j であって iN/j の倍数であるものは存在しないので、s_{11} = - です。


入力例 2

7

出力例 2

17777771

入力例 3

1

出力例 3

11

Score : 200 points

Problem Statement

You are given a positive integer N. Print a string of length (N+1), s_0s_1\ldots s_N, defined as follows.

For each i = 0, 1, 2, \ldots, N,

  • if there is a divisor j of N that is between 1 and 9, inclusive, and i is a multiple of N/j, then s_i is the digit corresponding to the smallest such j (s_i will thus be one of 1, 2, ..., 9);
  • if no such j exists, then s_i is -.

Constraints

  • 1 \leq N \leq 1000
  • All input values are integers.

Input

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

N

Output

Print the answer.


Sample Input 1

12

Sample Output 1

1-643-2-346-1

We will explain how to determine s_i for some i.

  • For i = 0, the divisors j of N between 1 and 9 such that i is a multiple of N/j are 1, 2, 3, 4, 6. The smallest of these is 1, so s_0 = 1.

  • For i = 4, the divisors j of N between 1 and 9 such that i is a multiple of N/j are 3, 6. The smallest of these is 3, so s_4 = 3.

  • For i = 11, there are no divisors j of N between 1 and 9 such that i is a multiple of N/j, so s_{11} = -.


Sample Input 2

7

Sample Output 2

17777771

Sample Input 3

1

Sample Output 3

11
C - False Hope

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

3\times3 のマス目に 1 から 9 までの数字が書き込まれており、上から i 行目、左から j 列目 (1\leq i\leq3,1\leq j\leq3) に書き込まれている数字は c _ {i,j} です。

異なるマスに同じ数字が書き込まれている場合もありますが、同じ数字が縦・横・斜めに 3 つ連続して書き込まれていることはありません。 より厳密には、c _ {i,j} について次の条件のすべてが成り立っていることが保証されます。

  • どの 1\leq i\leq3 についても、c _ {i,1}=c _ {i,2}=c _ {i,3} ではない
  • どの 1\leq j\leq3 についても、c _ {1,j}=c _ {2,j}=c _ {3,j} ではない
  • c _ {1,1}=c _ {2,2}=c _ {3,3} ではない
  • c _ {3,1}=c _ {2,2}=c _ {1,3} ではない

高橋くんは、それぞれのマスに書かれている数字をランダムな順番で知ります。 高橋くんは、縦・横・斜めの列のうちの 1 つでも次の条件を満たしたときがっかりします。

  • はじめに知ったほうの 2 マスに書かれた数字が同じであり、最後に知ったマスに書かれた数字がそれと異なる。

高橋くんががっかりせずにすべてのマスに書かれた数字を知る確率を求めてください。

制約

  • c _ {i,j}\in\lbrace1,2,3,4,5,6,7,8,9\rbrace\ (1\leq i\leq3,1\leq j\leq3)
  • c _ {i,1}=c _ {i,2}=c _ {i,3} ではない (1\leq i\leq3)
  • c _ {1,j}=c _ {2,j}=c _ {3,j} ではない (1\leq j\leq3)
  • c _ {1,1}=c _ {2,2}=c _ {3,3} ではない
  • c _ {1,3}=c _ {2,2}=c _ {3,1} ではない

入力

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

c _ {1,1} c _ {1,2} c _ {1,3}
c _ {2,1} c _ {2,2} c _ {2,3}
c _ {3,1} c _ {3,2} c _ {3,3}

出力

高橋くんががっかりせずにすべてのマスに書かれた数字を知る確率を 1 行で出力せよ。 真の値からの絶対誤差が 10 ^ {-8} 以下であるとき、正答と判定される。


入力例 1

3 1 9
2 5 6
2 7 1

出力例 1

0.666666666666666666666666666667

例えば、高橋くんが c _ {3,1}=2,c _ {2,1}=2,c _ {1,1}=3 の順に知った場合、高橋くんはがっかりしてしまいます。

対して、高橋くんが c _ {1,1},c _ {1,2},c _ {1,3},c _ {2,1},c _ {2,2},c _ {2,3},c _ {3,1},c _ {3,2},c _ {3,3} の順に数字を知った場合、がっかりすることなくすべての数字を知ることができます。

高橋くんががっかりすることなくすべての数字を知ることができる確率は \dfrac 23 です。 絶対誤差が 10 ^ {-8} 以下であれば正答と判定されるため、0.6666666570.666666676 のように出力しても正解になります。


入力例 2

7 7 6
8 6 8
7 7 6

出力例 2

0.004982363315696649029982363316

入力例 3

3 6 7
1 9 7
5 7 5

出力例 3

0.4

Score : 300 points

Problem Statement

There is a 3\times3 grid with numbers between 1 and 9, inclusive, written in each square. The square at the i-th row from the top and j-th column from the left (1\leq i\leq3,1\leq j\leq3) contains the number c _ {i,j}.

The same number may be written in different squares, but not in three consecutive cells vertically, horizontally, or diagonally. More precisely, it is guaranteed that c _ {i,j} satisfies all of the following conditions.

  • c _ {i,1}=c _ {i,2}=c _ {i,3} does not hold for any 1\leq i\leq3.
  • c _ {1,j}=c _ {2,j}=c _ {3,j} does not hold for any 1\leq j\leq3.
  • c _ {1,1}=c _ {2,2}=c _ {3,3} does not hold.
  • c _ {3,1}=c _ {2,2}=c _ {1,3} does not hold.

Takahashi will see the numbers written in each cell in random order. He will get disappointed when there is a line (vertical, horizontal, or diagonal) that satisfies the following condition.

  • The first two squares he sees contain the same number, but the last square contains a different number.

Find the probability that Takahashi sees the numbers in all the squares without getting disappointed.

Constraints

  • c _ {i,j}\in\lbrace1,2,3,4,5,6,7,8,9\rbrace\ (1\leq i\leq3,1\leq j\leq3)
  • c _ {i,1}=c _ {i,2}=c _ {i,3} does not hold for any 1\leq i\leq3.
  • c _ {1,j}=c _ {2,j}=c _ {3,j} does not hold for any 1\leq j\leq3.
  • c _ {1,1}=c _ {2,2}=c _ {3,3} does not hold.
  • c _ {3,1}=c _ {2,2}=c _ {1,3} does not hold.

Input

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

c _ {1,1} c _ {1,2} c _ {1,3}
c _ {2,1} c _ {2,2} c _ {2,3}
c _ {3,1} c _ {3,2} c _ {3,3}

Output

Print one line containing the probability that Takahashi sees the numbers in all the squares without getting disappointed. Your answer will be considered correct if the absolute error from the true value is at most 10 ^ {-8}.


Sample Input 1

3 1 9
2 5 6
2 7 1

Sample Output 1

0.666666666666666666666666666667

For example, if Takahashi sees c _ {3,1}=2,c _ {2,1}=2,c _ {1,1}=3 in this order, he will get disappointed.

On the other hand, if Takahashi sees c _ {1,1},c _ {1,2},c _ {1,3},c _ {2,1},c _ {2,2},c _ {2,3},c _ {3,1},c _ {3,2},c _ {3,3} in this order, he will see all numbers without getting disappointed.

The probability that Takahashi sees all the numbers without getting disappointed is \dfrac 23. Your answer will be considered correct if the absolute error from the true value is at most 10 ^ {-8}, so outputs such as 0.666666657 and 0.666666676 would also be accepted.


Sample Input 2

7 7 6
8 6 8
7 7 6

Sample Output 2

0.004982363315696649029982363316

Sample Input 3

3 6 7
1 9 7
5 7 5

Sample Output 3

0.4
D - Minimum Width

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

高橋くんは、N 個の単語からなる文章をウィンドウに表示させようとしています。 すべての単語の縦幅は等しく、i 番目 (1\leq i\leq N) の単語の横幅は L _ i です。

文章は、横幅 1 の空白を単語の区切りとしてウィンドウに表示されます。 より厳密には、高橋くんが横幅 W のウィンドウに文章を表示しているとき、次の条件が成り立っています。

  • 文章はいくつかの行に分かれている。
  • 1 番目の単語は一番上の行の先頭に表示されている。
  • i 番目 (2\leq i\leq N) の単語は、i-1 番目の単語の次に間隔を 1 だけ開けて表示されているか、i-1 番目の単語が含まれる行の下の行の先頭に表示されているかの一方である。それ以外の場所に表示されていることはない。
  • それぞれの行の横幅は W を超えない。ここで、行の横幅とは最も左にある単語の左端から最も右にある単語の右端までの距離を指す。

高橋くんが文章をウィンドウに表示したとき、文章が M 行に収まりました。 ウィンドウの横幅としてありえる最小の値を求めてください。

制約

  • 1\leq M\leq N\leq2\times10 ^ 5
  • 1\leq L _ i\leq10^9\ (1\leq i\leq N)
  • 入力はすべて整数

入力

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

N M
L _ 1 L _ 2 \ldots L _ N

出力

答えを 1 行で出力せよ。


入力例 1

13 3
9 5 2 7 1 8 8 2 1 5 2 3 6

出力例 1

26

ウィンドウの横幅が 26 のとき、以下のようにして与えられた文章を 3 行に収めることができます。

ウィンドウの横幅が 25 以下のときは与えられた文章を 3 行に収めることができないため、26 を出力してください。

単語を複数の行にまたがって表示させたり、行の横幅がウィンドウの横幅を上回ったり、単語を並べ替えたりしてはいけないことに注意してください。


入力例 2

10 1
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

出力例 2

10000000009

答えが 32\operatorname{bit} 整数に収まらない場合があることに注意してください。


入力例 3

30 8
8 55 26 97 48 37 47 35 55 5 17 62 2 60 23 99 73 34 75 7 46 82 84 29 41 32 31 52 32 60

出力例 3

189

Score : 400 points

Problem Statement

Takahashi is displaying a sentence with N words in a window. All words have the same height, and the width of the i-th word (1\leq i\leq N) is L _ i.

The words are displayed in the window separated by a space of width 1. More precisely, when the sentence is displayed in a window of width W, the following conditions are satisfied.

  • The sentence is divided into several lines.
  • The first word is displayed at the beginning of the top line.
  • The i-th word (2\leq i\leq N) is displayed either with a gap of 1 after the (i-1)-th word, or at the beginning of the line below the line containing the (i-1)-th word. It will not be displayed anywhere else.
  • The width of each line does not exceed W. Here, the width of a line refers to the distance from the left end of the leftmost word to the right end of the rightmost word.

When Takahashi displayed the sentence in the window, the sentence fit into M or fewer lines. Find the minimum possible width of the window.

Constraints

  • 1\leq M\leq N\leq2\times10 ^ 5
  • 1\leq L _ i\leq10^9\ (1\leq i\leq N)
  • All input values are integers.

Input

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

N M
L _ 1 L _ 2 \ldots L _ N

Output

Print the answer in one line.


Sample Input 1

13 3
9 5 2 7 1 8 8 2 1 5 2 3 6

Sample Output 1

26

When the width of the window is 26, you can fit the given sentence into three lines as follows.

You cannot fit the given sentence into three lines when the width of the window is 25 or less, so print 26.

Note that you should not display a word across multiple lines, let the width of a line exceed the width of the window, or rearrange the words.


Sample Input 2

10 1
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 2

10000000009

Note that the answer may not fit into a 32\operatorname{bit} integer.


Sample Input 3

30 8
8 55 26 97 48 37 47 35 55 5 17 62 2 60 23 99 73 34 75 7 46 82 84 29 41 32 31 52 32 60

Sample Output 3

189
E - Bus Stops

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 450

問題文

高橋君ははじめ高橋君の家におり、これから青木君の家に遊びに行きます。

2 人の家の間には 1 から N までの番号がつけられた N 個のバス停があり、高橋君はそれらの間を下記の方法で移動できます。

  • 高橋君の家からバス停 1 まで X だけの時間をかけて徒歩で移動できます。
  • i = 1, 2, \ldots, N-1 について、バス停 i からは P_i の倍数である時刻それぞれにバスが出発し、そのバスに乗ることで T_i だけの時間をかけてバス停 (i+1) に移動できます。ここで、1 \leq P_i \leq 8 が制約として保証されます。
  • バス停 N から青木君の家まで、Y だけの時間をかけて徒歩で移動できます。

i = 1, 2, \ldots, Q に対して下記のクエリを処理してください。

高橋君が高橋君の家を時刻 q_i に出発するときの、高橋君が青木君の家に到着する時刻としてあり得る最も早いものを求めよ。

なお、バスの出発時刻ちょうどにそのバスが出発するバス停に到着した場合であっても、そのバスに乗ることができます。

制約

  • 2 \leq N \leq 10^5
  • 1 \leq X, Y \leq 10^9
  • 1 \leq P_i \leq 8
  • 1 \leq T_i \leq 10^9
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq q_i \leq 10^9
  • 入力はすべて整数

入力

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

N X Y
P_1 T_1
P_2 T_2
\vdots
P_{N-1} T_{N-1}
Q
q_1
q_2
\vdots
q_Q

出力

Q 行出力せよ。 i = 1, 2, \ldots, Q について、i 行目には i 番目のクエリに対する答えを出力せよ。


入力例 1

4 2 3
5 4
6 6
3 1
7
13
0
710511029
136397527
763027379
644706927
447672230

出力例 1

34
22
710511052
136397548
763027402
644706946
447672250

1 番目のクエリについて、高橋君は下記の通りに移動を行って、時刻 34 に青木君の家に到着することができます。

  • 時刻 13 に高橋君の家を出発する。
  • 高橋君の家から徒歩で移動し、時刻 15 にバス停 1 に到着する。
  • 時刻 15 にバス停 1 を出発するバスに乗り、時刻 19 にバス停 2 に到着する。
  • 時刻 24 にバス停 2 を出発するバスに乗り、時刻 30 にバス停 3 に到着する。
  • 時刻 30 にバス停 3 を出発するバスに乗り、時刻 31 にバス停 4 に到着する。
  • バス停 4 から徒歩で移動し、時刻 34 に青木君の家に到着する。

2 番目のクエリについて、高橋君は下記の通りに移動を行って、時刻 22 に青木君の家に到着することができます。

  • 時刻 0 に高橋君の家を出発する。
  • 高橋君の家から徒歩で移動し、時刻 2 にバス停 1 に到着する。
  • 時刻 5 にバス停 1 を出発するバスに乗り、時刻 9 にバス停 2 に到着する。
  • 時刻 12 にバス停 2 を出発するバスに乗り、時刻 18 にバス停 3 に到着する。
  • 時刻 18 にバス停 3 を出発するバスに乗り、時刻 19 にバス停 4 に到着する。
  • バス停 4 から徒歩で移動し、時刻 22 に青木君の家に到着する。

Score : 450 points

Problem Statement

Takahashi is initially at his house and is about to visit Aoki's house.

There are N bus stops numbered 1 to N between the two houses, and Takahashi can move between them in the following ways:

  • He can walk from his house to bus stop 1 in X units of time.
  • For each i = 1, 2, \ldots, N-1, a bus departs from bus stop i at each time that is a multiple of P_i, and by taking this bus, he can get to bus stop (i+1) in T_i units of time. Here, the constraints guarantee that 1 \leq P_i \leq 8.
  • Takahashi can walk from bus stop N to Aoki's house in Y units of time.

For each i = 1, 2, \ldots, Q, process the following query.

Find the earliest time that Takahashi can arrive at Aoki's house when he leaves his house at time q_i.

Note that if he arrives at a bus stop exactly at the departure time of a bus, he can take that bus.

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq X, Y \leq 10^9
  • 1 \leq P_i \leq 8
  • 1 \leq T_i \leq 10^9
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq q_i \leq 10^9
  • All input values are integers.

Input

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

N X Y
P_1 T_1
P_2 T_2
\vdots
P_{N-1} T_{N-1}
Q
q_1
q_2
\vdots
q_Q

Output

Print Q lines. For each i = 1, 2, \ldots, Q, the i-th line should contain the answer to the i-th query.


Sample Input 1

4 2 3
5 4
6 6
3 1
7
13
0
710511029
136397527
763027379
644706927
447672230

Sample Output 1

34
22
710511052
136397548
763027402
644706946
447672250

For the first query, Takahashi can move as follows to arrive at Aoki's house at time 34.

  • Leave his house at time 13.
  • Walk from his house and arrive at bus stop 1 at time 15.
  • Take the bus departing from bus stop 1 at time 15 and arrive at bus stop 2 at time 19.
  • Take the bus departing from bus stop 2 at time 24 and arrive at bus stop 3 at time 30.
  • Take the bus departing from bus stop 3 at time 30 and arrive at bus stop 4 at time 31.
  • Walk from bus stop 4 and arrive at Aoki's house at time 34.

For the second query, Takahashi can move as follows and arrive at Aoki's house at time 22.

  • Leave his house at time 0.
  • Walk from his house and arrive at bus stop 1 at time 2.
  • Take the bus departing from bus stop 1 at time 5 and arrive at bus stop 2 at time 9.
  • Take the bus departing from bus stop 2 at time 12 and arrive at bus stop 3 at time 18.
  • Take the bus departing from bus stop 3 at time 18 and arrive at bus stop 4 at time 19.
  • Walk from bus stop 4 and arrive at Aoki's house at time 22.
F - Fighter Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 550

問題文

N 頂点の木があります。 1 番目の頂点が根であり、i 番目 (2\leq i\leq N) の頂点の親は p _ i\ (1\leq p _ i\lt i) です。

根でない頂点には、のどちらか一方が配置されています。 高橋くんは、すべての敵を倒したいです。 はじめ、高橋くんの強さは 1 で、頂点 1 にいます。 i=2,\ldots,N について、i 番目の頂点の情報は整数の組 (t _ i,s _ i,g _ i) を用いて次のように表されます。

  • t _ i=1 ならば i 番目の頂点には敵がいます。この頂点に高橋くんが初めて訪れたとき、高橋くんの強さが s _ i 未満だった場合高橋くんは敵に倒されて敗北し、高橋くんは他の頂点に移動できなくなります。そうでなかった場合、高橋くんは敵を倒し、強さが g _ i 上昇します。
  • t _ i=2 ならば i 番目の頂点には薬があります。この頂点に高橋くんが初めて訪れたとき、高橋くんは薬を飲み、強さが g _ i 倍になります。(薬がある頂点では、s _ i=0 です。)

薬がある頂点はたかだか 10 個です。

高橋くんは、隣接する頂点に移動することができます。 高橋くんがすべての敵を倒すことができるか判定してください。

制約

  • 2\leq N\leq 500
  • 1\leq p _ i\lt i\ (2\leq i\leq N)
  • t _ i\in\lbrace1,2\rbrace\ (2\leq i\leq N)
  • t _ i=1\implies1\leq s _ i\leq 10 ^ 9\ (2\leq i\leq N)
  • t _ i=2\implies s _ i=0\ (2\leq i\leq N)
  • 1\leq g _ i\leq 10 ^ 9\ (2\leq i\leq N)
  • t _ i=2 である頂点は 10 個以下
  • 入力はすべて整数

入力

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

N
p _ 2 t _ 2 s _ 2 g _ 2
p _ 3 t _ 3 s _ 3 g _ 3
\vdots
p _ N t _ N s _ N g _ N

出力

答え(Yes または No)を 1 行で出力せよ。


入力例 1

8
1 2 0 3
2 1 3 3
1 2 0 4
4 1 2 2
1 2 0 5
6 1 5 5
5 1 140 1

出力例 1

Yes

はじめ、木は以下のようになっています。

高橋くんは、頂点 1 から 2,3,2,1,6,7,6,1,4,5,8 の順に移動することで、すべての敵を倒すことができます。 このとき、高橋くんがいる頂点と高橋くんの強さは以下の図のように変化します(図では、すでに訪れたことのある頂点への移動は省略しています)。

例えば、頂点 1 から 4,5,8 の順に移動すると、頂点 8 に訪れた時点での強さが s _ 8=140 より小さいので高橋くんは敗北してしまい、すべての敵を倒すことができません。


入力例 2

12
1 1 166 619
1 1 17 592
2 1 222 983
2 1 729 338
5 1 747 62
3 1 452 815
3 2 0 1
4 2 0 40
4 1 306 520
6 1 317 591
1 1 507 946

出力例 2

No

入力例 3

12
1 1 1 791
2 2 0 410
2 1 724 790
2 1 828 599
5 2 0 13
3 1 550 803
1 1 802 506
5 1 261 587
6 1 663 329
8 1 11 955
9 1 148 917

出力例 3

Yes

入力例 4

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

出力例 4

Yes

Score : 550 points

Problem Statement

There is a tree with N vertices. The 1-st vertex is the root, and the parent of the i-th vertex (2\leq i\leq N) is p _ i\ (1\leq p _ i\lt i).

Each non-root vertex has an enemy or a medicine on it. Takahashi wants to defeat all the enemies. Initially, his strength is 1, and he is at vertex 1. For i=2,\ldots,N, the information of the i-th vertex is represented by a triple of integers (t _ i,s _ i,g _ i) as follows.

  • If t _ i=1, there is an enemy at the i-th vertex. When Takahashi visits this vertex for the first time, if his strength is less than s _ i, Takahashi is defeated by the enemy and loses, after which he cannot move to other vertices. Otherwise, he defeats the enemy, and his strength increases by g _ i.
  • If t _ i=2, there is a medicine at the i-th vertex. When Takahashi visits this vertex for the first time, he takes the medicine, and his strength is multiplied by g _ i. (For a vertex with a medicine, s _ i=0.)

There are at most 10 vertices with a medicine.

Takahashi can repeatedly move to an adjacent vertex. Determine if he can defeat all the enemies.

Constraints

  • 2\leq N\leq 500
  • 1\leq p _ i\lt i\ (2\leq i\leq N)
  • t _ i\in\lbrace1,2\rbrace\ (2\leq i\leq N)
  • t _ i=1\implies1\leq s _ i\leq 10 ^ 9\ (2\leq i\leq N)
  • t _ i=2\implies s _ i=0\ (2\leq i\leq N)
  • 1\leq g _ i\leq 10 ^ 9\ (2\leq i\leq N)
  • There are at most 10 vertices with t _ i=2.
  • All input values are integers.

Input

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

N
p _ 2 t _ 2 s _ 2 g _ 2
p _ 3 t _ 3 s _ 3 g _ 3
\vdots
p _ N t _ N s _ N g _ N

Output

Print the answer (Yes or No) in one line.


Sample Input 1

8
1 2 0 3
2 1 3 3
1 2 0 4
4 1 2 2
1 2 0 5
6 1 5 5
5 1 140 1

Sample Output 1

Yes

Initially, the tree looks like this:

Takahashi can defeat all the enemies by moving from vertex 1 to 2,3,2,1,6,7,6,1,4,5,8 in this order. Here, his position and strength change as shown in the following figure (movements to vertices that have already been visited are omitted).

On the other hand, if he moves from vertex 1 to 4,5,8 in this order, for example, his strength when visiting vertex 8 will be less than s _ 8=140, so he will lose without defeating all the enemies.


Sample Input 2

12
1 1 166 619
1 1 17 592
2 1 222 983
2 1 729 338
5 1 747 62
3 1 452 815
3 2 0 1
4 2 0 40
4 1 306 520
6 1 317 591
1 1 507 946

Sample Output 2

No

Sample Input 3

12
1 1 1 791
2 2 0 410
2 1 724 790
2 1 828 599
5 2 0 13
3 1 550 803
1 1 802 506
5 1 261 587
6 1 663 329
8 1 11 955
9 1 148 917

Sample Output 3

Yes

Sample Input 4

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

Sample Output 4

Yes
G - Counting Shortest Paths

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 575

問題文

N 頂点の無向完全グラフ G に対して下記の操作を行います。

i = 1, 2, \ldots, M について、頂点 u_i と 頂点 v_i を結ぶ無向辺を削除する。

その後の G において、頂点 1 から頂点 N へのパスが存在するかどうかを判定し、 存在する場合は頂点 1 から 頂点 N への最短パスの個数を 998244353 で割った余りを求めてください。

ここで、頂点 1 から 頂点 N への最短パスとは、頂点 1 から頂点 N へのパスであって含む辺の本数が最小であるものです。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq \min\lbrace 2 \times 10^5, N(N-1)/2 \rbrace
  • 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
  • 入力はすべて整数

入力

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

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

出力

操作後の G において、頂点 1 から頂点 N へのパスが存在しない場合は -1 を出力し、 存在する場合は頂点 1 から頂点 N への最短パスの個数を 998244353 で割った余りを出力せよ。


入力例 1

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

出力例 1

3

操作後の G における頂点 1 から頂点 N への最短パスは、3 本の辺を含む下記の 3 個のパスです。

  • 頂点 1 \rightarrow 頂点 2 \rightarrow 頂点 3 \rightarrow 頂点 6
  • 頂点 1 \rightarrow 頂点 2 \rightarrow 頂点 5 \rightarrow 頂点 6
  • 頂点 1 \rightarrow 頂点 4 \rightarrow 頂点 5 \rightarrow 頂点 6

入力例 2

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

出力例 2

-1

操作後の G には辺が 1 本もありません。 頂点 1 から頂点 N へのパスが存在しないため -1 を出力します。

Score : 575 points

Problem Statement

We will perform the following operation on a complete undirected graph G with N vertices.

For each i = 1, 2, \ldots, M, delete the undirected edge connecting vertices u_i and v_i.

Determine if there is a path from vertex 1 to vertex N in G after the operation. If there is, find the number, modulo 998244353, of shortest paths from vertex 1 to vertex N.

Here, a shortest path from vertex 1 to vertex N is a path from vertex 1 to vertex N that contains the minimum number of edges.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq \min\lbrace 2 \times 10^5, N(N-1)/2 \rbrace
  • 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
  • All input values are integers.

Input

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

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

Output

If there is no path from vertex 1 to vertex N in G after the operation, print -1. If there is, print the number, modulo 998244353, of shortest paths from vertex 1 to vertex N.


Sample Input 1

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

Sample Output 1

3

In G after the operation, the shortest paths from vertex 1 to vertex N are the following three, each containing three edges.

  • vertex 1 \rightarrow vertex 2 \rightarrow vertex 3 \rightarrow vertex 6
  • vertex 1 \rightarrow vertex 2 \rightarrow vertex 5 \rightarrow vertex 6
  • vertex 1 \rightarrow vertex 4 \rightarrow vertex 5 \rightarrow vertex 6

Sample Input 2

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

Sample Output 2

-1

G has no edges after the operation. There is no path from vertex 1 to vertex N, so print -1.