E - Martial artist

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

高橋君は武術家です。 武術家の覚えられる技は N 個あり、技 1, 2, \ldots, N と名前がついています。 1 \leq i \leq N について、技 i を習得するには時間 T_i の修練が必要で、 さらに、修練の開始時点で技 A_{i,1}, A_{i,2}, \ldots, A_{i,K_i} をすでに習得している必要があります。 ここで、1 \leq j \leq K_i について、A_{i,j} < i であることが保証されます。

高橋君は時刻 0 の時点で技を 1 つも覚えていません。 高橋君は同時に 1 つの技に対する修練しかできず、また、一度始めた修練を途中でやめることもできません。 高橋君が技 N を習得するのに必要な時間の最小値を求めてください。

制約

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq T_i \leq 10^9
  • 0 \leq K_i < i
  • 1 \leq A_{i,j} < i
  • \sum_{i=1}^N K_i \leq 2\times 10^5
  • A_{i,1}, A_{i,2}, \ldots, A_{i,K_i} はすべて異なる。
  • 入力は全て整数である。

入力

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

N
T_1 K_1 A_{1,1} A_{1,2} \ldots A_{1,K_1}
T_2 K_2 A_{2,1} A_{2,2} \ldots A_{2,K_2}
\vdots
T_N K_N A_{N,1} A_{N,2} \ldots A_{N,K_N}

出力

N を習得するのに必要な時間の最小値を出力せよ。


入力例 1

3
3 0
5 1 1
7 1 1

出力例 1

10

例えば高橋君は次のように行動することができます。

  • 高橋君は時刻 0 に技 1 の修練を開始し、時刻 3 に技 1 を習得します。
  • その後、時刻 3 に技 3 の修練を開始し、時刻 10 に技 3 を習得します。

このときが最短で、高橋君が技 3 を習得するのに必要な時間は 3+7=10 となります。 技 3 の習得のためには、技 2 を習得する必要はありません。


入力例 2

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

出力例 2

5000000000

答えが 32 bit 整数に収まらないことがある事に注意してください。

Score : 300 points

Problem Statement

Takahashi is a martial artist. There are N moves that a martial artist can learn, called Move 1, 2, \ldots, N. For each 1 \leq i \leq N, it takes T_i minutes of practice to learn Move i. Additionally, at the beginning of that practice, all of the Moves A_{i,1}, A_{i,2}, \ldots, A_{i,K_i} must be already learned. Here, it is guaranteed that A_{i,j} < i for each 1 \leq j \leq K_i.

Takahashi has not learned any move at time 0. He cannot practice for more than one move simultaneously, nor can he stop a practice he has already started. Find the minimum number of minutes needed for Takahashi to learn Move N.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq T_i \leq 10^9
  • 0 \leq K_i < i
  • 1 \leq A_{i,j} < i
  • \sum_{i=1}^N K_i \leq 2\times 10^5
  • A_{i,1}, A_{i,2}, \ldots, A_{i,K_i} are all distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
T_1 K_1 A_{1,1} A_{1,2} \ldots A_{1,K_1}
T_2 K_2 A_{2,1} A_{2,2} \ldots A_{2,K_2}
\vdots
T_N K_N A_{N,1} A_{N,2} \ldots A_{N,K_N}

Output

Print the minimum number of minutes needed for Takahashi to learn Move N.


Sample Input 1

3
3 0
5 1 1
7 1 1

Sample Output 1

10

Here is one possible plan for Takahashi.

  • At time 0, start practicing for Move 1 to learn Move 1 at time 3.
  • Then, at time 3, start practicing for Move 3 to learn Move 3 at time 10.

Here, Takahashi spends 3+7=10 minutes to learn Move 3, which is the fastest possible. Note that he does not need to learn Move 2 to learn Move 3.


Sample Input 2

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

Sample Output 2

5000000000

Note that the answer may not fit into a 32-bit integer.

F - Chinese Restaurant

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

回転テーブルの周りに人 0、人 1\ldots、人 N-1 がこの順番で反時計回りに等間隔で並んでいます。また、人 i の目の前には料理 p_i が置かれています。
あなたは次の操作を 0 回以上何度でも行うことが出来ます。

  • 回転テーブルを反時計回りに 1 周の \frac{1}{N} だけ回す。これによって、(この操作の直前に)人 i の目の前にあった料理は人 (i+1) \bmod N の目の前に移動する。

操作を完了させた後において、人 i は料理 i が人 (i-1) \bmod N、人 i、人 (i+1) \bmod N のいずれかの目の前に置かれていると喜びます。
喜ぶ人数の最大値を求めてください。

a \bmod m とは 整数 a と正整数 m に対し、a \bmod ma-xm の倍数となるような 0 以上 m 未満の整数 x を表します。(このような x は一意に定まることが証明できます)

制約

  • 3 \leq N \leq 2 \times 10^5
  • 0 \leq p_i \leq N-1
  • i \neq j ならば p_i \neq p_j
  • 入力はすべて整数

入力

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

N
p_0 \ldots p_{N-1}

出力

答えを出力せよ。


入力例 1

4
1 2 0 3

出力例 1

4

操作を 1 回行うと下の画像のようになります。

この時、4 人が喜ぶことを以下のように確かめられます。

  • 0 は料理 0 が人 3\ (=(0-1) \bmod 4) の目の前に置かれているので喜びます。
  • 1 は料理 1 が人 1\ (=1) の目の前に置かれているので喜びます。
  • 2 は料理 2 が人 2\ (=2) の目の前に置かれているので喜びます。
  • 3 は料理 3 が人 0\ (=(3+1) \bmod 4) の目の前に置かれているので喜びます。

5 人以上が喜ぶことは無いため、答えは 4 です。


入力例 2

3
0 1 2

出力例 2

3

入力例 3

10
3 9 6 1 7 2 8 0 5 4

出力例 3

5

Score : 300 points

Problem Statement

Person 0, Person 1, \ldots, and Person (N-1) are sitting around a turntable in their counterclockwise order, evenly spaced. Dish p_i is in front of Person i on the table.
You may perform the following operation 0 or more times:

  • Rotate the turntable by one N-th of a counterclockwise turn. As a result, the dish that was in front of Person i right before the rotation is now in front of Person (i+1) \bmod N.

When you are finished, Person i is happy if Dish i is in front of Person (i-1) \bmod N, Person i, or Person (i+1) \bmod N.
Find the maximum possible number of happy people.

What is a \bmod m? For an integer a and a positive integer m, a \bmod m denotes the integer x between 0 and (m-1) (inclusive) such that (a-x) is a multiple of m. (It can be proved that such x is unique.)

Constraints

  • 3 \leq N \leq 2 \times 10^5
  • 0 \leq p_i \leq N-1
  • p_i \neq p_j if i \neq j.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
p_0 \ldots p_{N-1}

Output

Print the answer.


Sample Input 1

4
1 2 0 3

Sample Output 1

4

The figure below shows the table after one operation.

Here, there are four happy people:

  • Person 0 is happy because Dish 0 is in front of Person 3\ (=(0-1) \bmod 4);
  • Person 1 is happy because Dish 1 is in front of Person 1\ (=1);
  • Person 2 is happy because Dish 2 is in front of Person 2\ (=2);
  • Person 3 is happy because Dish 3 is in front of Person 0\ (=(3+1) \bmod 4).

There cannot be five or more happy people, so the answer is 4.


Sample Input 2

3
0 1 2

Sample Output 2

3

Sample Input 3

10
3 9 6 1 7 2 8 0 5 4

Sample Output 3

5
G - Circumferences

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

xy -平面上の N 個の円が与えられます。 i = 1, 2, \ldots, N について、i 番目の円は点 (x_i, y_i) を中心とする半径 r_i の円です。

N 個の円のうち少なくとも 1 つ以上の円の円周上にある点のみを通って、点 (s_x, s_y) から点 (t_x, t_y) に行くことができるかどうかを判定してください。

制約

  • 1 \leq N \leq 3000
  • -10^9 \leq x_i, y_i \leq 10^9
  • 1 \leq r_i \leq 10^9
  • (s_x, s_y)N 個の円のうち少なくとも 1 つ以上の円の円周上にある
  • (t_x, t_y)N 個の円のうち少なくとも 1 つ以上の円の円周上にある
  • 入力はすべて整数

入力

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

N
s_x s_y t_x t_y
x_1 y_1 r_1
x_2 y_2 r_2
\vdots
x_N y_N r_N

出力

(s_x, s_y) から点 (t_x, t_y) に行くことができる場合は Yes を、そうでない場合は No を出力せよ。 ジャッジは英小文字と英大文字を厳密に区別することに注意せよ。


入力例 1

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

出力例 1

Yes

例えば、下記の経路で点 (0, -2) から点 (3, 3) へ行くことができます。

  • (0, -2) から 1 つ目の円の円周上を反時計回りに通って点 (1, -\sqrt{3}) へ行く。
  • (1, -\sqrt{3}) から 2 つ目の円の円周上を時計回りに通って点 (2, 2) へ行く。
  • (2, 2) から 3 つ目の円の円周上を反時計回りに通って点 (3, 3) へ行く。

よって、Yes を出力します。


入力例 2

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

出力例 2

No

少なくとも 1 つ以上の円の円周上にある点のみを通って点 (0, 1) から点 (0, 3) に行くことはできないので No を出力します。

Score : 400 points

Problem Statement

You are given N circles on the xy-coordinate plane. For each i = 1, 2, \ldots, N, the i-th circle is centered at (x_i, y_i) and has a radius of r_i.

Determine whether it is possible to get from (s_x, s_y) to (t_x, t_y) by only passing through points that lie on the circumference of at least one of the N circles.

Constraints

  • 1 \leq N \leq 3000
  • -10^9 \leq x_i, y_i \leq 10^9
  • 1 \leq r_i \leq 10^9
  • (s_x, s_y) lies on the circumference of at least one of the N circles.
  • (t_x, t_y) lies on the circumference of at least one of the N circles.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
s_x s_y t_x t_y
x_1 y_1 r_1
x_2 y_2 r_2
\vdots
x_N y_N r_N

Output

If it is possible to get from (s_x, s_y) to (t_x, t_y), print Yes; otherwise, print No. Note that the judge is case-sensitive.


Sample Input 1

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

Sample Output 1

Yes

Here is one way to get from (0, -2) to (3, 3).

  • From (0, -2), pass through the circumference of the 1-st circle counterclockwise to reach (1, -\sqrt{3}).
  • From (1, -\sqrt{3}), pass through the circumference of the 2-nd circle clockwise to reach (2, 2).
  • From (2, 2), pass through the circumference of the 3-rd circle counterclockwise to reach (3, 3).

Thus, Yes should be printed.


Sample Input 2

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

Sample Output 2

No

It is impossible to get from (0, 1) to (0, 3) by only passing through points on the circumference of at least one of the circles, so No should be printed.

H - Manhattan Multifocal Ellipse

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

2 次元平面上の N 個の点 (x_1,y_1),(x_2,y_2),\dots,(x_N,y_N) と、非負整数 D が与えられます。

整数の組 (x,y) であって、 \displaystyle \sum_{i=1}^N (|x-x_i|+|y-y_i|) \leq D を満たすものの個数を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq D \leq 10^6
  • -10^6 \leq x_i, y_i \leq 10^6
  • i\neq j ならば (x_i,y_i) \neq (x_j,y_j)
  • 入力はすべて整数

入力

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

N D
x_1 y_1
x_2 y_2
\vdots
x_N y_N

出力

答えを出力せよ。


入力例 1

2 3
0 0
1 0

出力例 1

8

下図は、サンプル 1 の入力と答えを可視化したものです。青い点が入力を表します。青い点と赤い点の合計 8 点が、問題文中の条件を満たす点です。


入力例 2

2 0
0 0
2 0

出力例 2

0

入力例 3

6 100
9 -6
10 -1
2 10
-1 7
-7 5
-1 -4

出力例 3

419

Score : 475 points

Problem Statement

You are given N points (x_1, y_1), (x_2, y_2), \dots, (x_N, y_N) on a two-dimensional plane, and a non-negative integer D.

Find the number of integer pairs (x, y) such that \displaystyle \sum_{i=1}^N (|x-x_i|+|y-y_i|) \leq D.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq D \leq 10^6
  • -10^6 \leq x_i, y_i \leq 10^6
  • (x_i, y_i) \neq (x_j, y_j) for i \neq j.
  • All input values are integers.

Input

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

N D
x_1 y_1
x_2 y_2
\vdots
x_N y_N

Output

Print the answer.


Sample Input 1

2 3
0 0
1 0

Sample Output 1

8

The following figure visualizes the input and the answer for Sample 1. The blue points represent the input. The blue and red points, eight in total, satisfy the condition in the statement.


Sample Input 2

2 0
0 0
2 0

Sample Output 2

0

Sample Input 3

6 100
9 -6
10 -1
2 10
-1 7
-7 5
-1 -4

Sample Output 3

419
I - Diameter set

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

N 頂点からなる木が与えられます。 頂点は 1 , 2 , \ldots , N と番号付けられており、 1\leq i\leq N-1 について、i 本目の辺は頂点 U_i と頂点 V_i を結んでいます。 木の直径を D とするとき、木の頂点のうち 2 点以上を選んで赤く塗る方法であって、 赤く塗られたどの頂点の間の距離も D であるようなものの数を 998244353 で割った余りを求めてください。

ただし、木の 2 頂点の間の距離は一方から他方へ移動するときに用いる辺の本数の最小値であり、 木の直径は任意の 2 頂点の間の距離の最大値として定められます。

制約

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq U_i,V_i \leq N
  • U_i \neq V_i
  • 入力は全て整数である。
  • 与えられるグラフは木である。

入力

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

N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}

出力

答えを出力せよ。


入力例 1

5
1 2
1 3
1 4
4 5

出力例 1

2

与えられた木は 5 頂点からなり、直径は 3 です。
2 頂点の組であって、その間の距離が 3 であるようなものは (2,5) , (3,5) しか存在しないため、 条件をみたす塗り方は \lbrace 2,5\rbrace \lbrace 3,5\rbrace 2 通りとなります。
\lbrace 2,3,5\rbrace は頂点 2 と頂点 3 の間の距離が 2 であるため条件をみたさないことに注意してください。


入力例 2

4
1 2
1 3
1 4

出力例 2

4

直径は 2 であり、条件をみたす塗り方は \lbrace 2,3\rbrace , \lbrace 2,4\rbrace , \lbrace 3,4\rbrace , \lbrace 2,3,4\rbrace 4 通りとなります。

Score : 500 points

Problem Statement

Given is a tree with N vertices. The vertices are numbered 1, 2, \ldots, N, and for each 1\leq i\leq N-1, the i-th edge connects Vertex U_i and Vertex V_i. Let D be the diameter of the tree. Find the number, modulo 998244353, of ways to choose two or more of the vertices and paint them red so that all distances between two red vertices are D.

Here, the distance between two vertices is the minimum number of edges that must be traversed to travel from one vertex to the other, and the diameter of the tree is the maximum distance between two vertices.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq U_i,V_i \leq N
  • U_i \neq V_i
  • All values in input are integers.
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}

Output

Print the answer.


Sample Input 1

5
1 2
1 3
1 4
4 5

Sample Output 1

2

The given tree has five vertices and a diameter of 3.
There are just two pairs of vertices whose distance is 3: (2,5) and (3,5), so there are two ways to paint the tree to satisfy the condition: \lbrace 2,5\rbrace and \lbrace 3,5\rbrace .
Note that painting 2,3,5 does not satisfy the condition since the distance between Vertex 2 and Vertex 3 is 2.


Sample Input 2

4
1 2
1 3
1 4

Sample Output 2

4

The diameter is 2, and the four ways to paint the tree to satisfy the condition are: \lbrace 2,3\rbrace , \lbrace 2,4\rbrace , \lbrace 3,4\rbrace , \lbrace 2,3,4\rbrace .