Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 233 点
問題文
高橋君は美術館の管理人をしています。美術館の壁には H 行 W 列のグリッド状にタイルが並んでおり、各タイルは一辺の長さが 1 の正方形です。上から i 行目、左から j 列目のタイルを (i, j) と表します。
各タイルは絵画の一部であるタイル(# で表す)か、何も描かれていない空白のタイル(. で表す)のいずれかです。
高橋君は、すべての絵画タイルを含むような、グリッドの行・列に沿った長方形の額縁を設置したいと考えています。具体的には、ある整数 r_1, r_2, c_1, c_2(1 \leq r_1 \leq r_2 \leq H, 1 \leq c_1 \leq c_2 \leq W)を選び、(r_1, c_1) を左上隅、(r_2, c_2) を右下隅とする長方形のタイル領域を定めます。すべての絵画タイルはこの長方形領域に含まれていなければなりません。すなわち、絵画タイル (i, j) が存在するならば、r_1 \leq i \leq r_2 かつ c_1 \leq j \leq c_2 を満たす必要があります。
この長方形領域は、縦に h = r_2 - r_1 + 1 タイル、横に w = c_2 - c_1 + 1 タイルを含みます。各タイルの一辺の長さは 1 なので、この長方形領域の縦の長さは h、横の長さは w であり、その周の長さは 2 \times (h + w) です。
高橋君は、すべての絵画タイルを含む長方形領域のうち、周の長さが最小となるものを求めたいと考えています。その最小の周の長さを出力してください。
ただし、絵画タイルが 1 つも存在しない場合は、額縁を設置する必要がないため 0 を出力してください。
制約
- 1 \leq H \leq 1000
- 1 \leq W \leq 1000
- H, W は整数
- S_i(1 \leq i \leq H)は
#と.からなる長さ W の文字列である
入力
H W S_1 S_2 \vdots S_H
- 1 行目には、グリッドの行数を表す整数 H と列数を表す整数 W が、スペース区切りで与えられる。
- 2 行目から H 行にわたって、グリッドの各行の情報を表す文字列 S_i が与えられる。
- 1 + i 行目(1 \leq i \leq H)には、i 行目のグリッド情報である長さ W の文字列 S_i が与えられる。S_i の j 文字目はタイル (i, j) の状態を表し、
#は絵画タイル、.は空白タイルを意味する。
出力
すべての絵画タイルを含む長方形領域のうち、周の長さの最小値を 1 行で出力せよ。絵画タイルが存在しない場合は 0 を出力せよ。
入力例 1
5 5 ..... .#.#. ..... .#.#. .....
出力例 1
12
入力例 2
3 4 .... .... ....
出力例 2
0
入力例 3
8 10 .......... #........# .......... ..#..#.... .......... ....##.... .......#.. ..........
出力例 3
32
Score : 233 pts
Problem Statement
Takahashi is a curator at an art museum. The museum wall has tiles arranged in a grid of H rows and W columns, where each tile is a square with side length 1. The tile at the i-th row from the top and the j-th column from the left is denoted as (i, j).
Each tile is either a painting tile (represented by #), which is part of a painting, or a blank tile (represented by .), on which nothing is drawn.
Takahashi wants to install a rectangular frame aligned with the grid's rows and columns that contains all painting tiles. Specifically, he chooses integers r_1, r_2, c_1, c_2 (1 \leq r_1 \leq r_2 \leq H, 1 \leq c_1 \leq c_2 \leq W) and defines a rectangular tile region with (r_1, c_1) as the top-left corner and (r_2, c_2) as the bottom-right corner. All painting tiles must be contained within this rectangular region. That is, if a painting tile (i, j) exists, then r_1 \leq i \leq r_2 and c_1 \leq j \leq c_2 must hold.
This rectangular region contains h = r_2 - r_1 + 1 tiles vertically and w = c_2 - c_1 + 1 tiles horizontally. Since each tile has side length 1, the vertical length of this rectangular region is h, the horizontal length is w, and its perimeter is 2 \times (h + w).
Takahashi wants to find, among all rectangular regions that contain all painting tiles, the one with the minimum perimeter. Output this minimum perimeter.
However, if there are no painting tiles, there is no need to install a frame, so output 0.
Constraints
- 1 \leq H \leq 1000
- 1 \leq W \leq 1000
- H, W are integers
- S_i (1 \leq i \leq H) is a string of length W consisting of
#and.
Input
H W S_1 S_2 \vdots S_H
- The first line contains integers H and W, representing the number of rows and columns of the grid, separated by a space.
- Over the next H lines, the string S_i representing each row of the grid is given.
- The (1 + i)-th line (1 \leq i \leq H) contains the string S_i of length W representing the grid information of the i-th row. The j-th character of S_i represents the state of tile (i, j), where
#denotes a painting tile and.denotes a blank tile.
Output
Output in a single line the minimum perimeter among all rectangular regions that contain all painting tiles. If there are no painting tiles, output 0.
Sample Input 1
5 5 ..... .#.#. ..... .#.#. .....
Sample Output 1
12
Sample Input 2
3 4 .... .... ....
Sample Output 2
0
Sample Input 3
8 10 .......... #........# .......... ..#..#.... .......... ....##.... .......#.. ..........
Sample Output 3
32
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君は、あるオンラインショッピングサイトで開催されている「ポイント獲得キャンペーン」に参加することになりました。
このキャンペーンでは、N 個の商品が用意されており、各商品 i(1 \leq i \leq N)を購入するとポイント P_i を獲得できます。
高橋君はこれらの商品の中から好きな商品を選んで購入することができますが、同じ商品を複数回購入することはできず、購入できる商品の個数は最大 K 個までです。K \geq N の場合はすべての商品を購入することもでき、また、1 つも購入しないことも許されます。
高橋君が獲得できる合計ポイントの最大値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 2 \times 10^5
- 1 \leq P_i \leq 10^9
- 入力はすべて整数
入力
N K P_1 P_2 \ldots P_N
- 1 行目には、商品の個数を表す整数 N と、購入できる商品の最大個数を表す整数 K が、スペース区切りで与えられる。
- 2 行目には、各商品で獲得できるポイントを表す整数 P_1, P_2, \ldots, P_N が、スペース区切りで与えられる。P_i は商品 i を購入したときに獲得できるポイントを表す。
出力
高橋君が獲得できる合計ポイントの最大値を整数として 1 行で出力せよ。
入力例 1
5 3 10 30 20 50 40
出力例 1
120
入力例 2
4 6 100 200 300 400
出力例 2
1000
入力例 3
10 5 1000000000 999999999 1 500000000 750000000 250000000 800000000 600000000 900000000 100000000
出力例 3
4449999999
Score : 300 pts
Problem Statement
Takahashi is going to participate in a "Point Earning Campaign" held on an online shopping site.
In this campaign, N products are available, and purchasing product i (1 \leq i \leq N) earns P_i points.
Takahashi can choose and purchase any products he likes from among these products, but he cannot purchase the same product more than once, and he can purchase at most K products. If K \geq N, he may purchase all products, and it is also allowed to purchase none at all.
Find the maximum total points Takahashi can earn.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 2 \times 10^5
- 1 \leq P_i \leq 10^9
- All inputs are integers
Input
N K P_1 P_2 \ldots P_N
- The first line contains an integer N representing the number of products and an integer K representing the maximum number of products that can be purchased, separated by a space.
- The second line contains integers P_1, P_2, \ldots, P_N representing the points earned from each product, separated by spaces. P_i represents the points earned when purchasing product i.
Output
Output the maximum total points Takahashi can earn as an integer on a single line.
Sample Input 1
5 3 10 30 20 50 40
Sample Output 1
120
Sample Input 2
4 6 100 200 300 400
Sample Output 2
1000
Sample Input 3
10 5 1000000000 999999999 1 500000000 750000000 250000000 800000000 600000000 900000000 100000000
Sample Output 3
4449999999
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
高橋君は N 個の部屋からなる洞窟を探検します。部屋には 1 から N の番号が付いています。部屋と部屋の間には M 本の一方通行の通路があります。各通路は、ある部屋から別の部屋へ向かう向きが決まっています。
各部屋から出る通路には優先順位が定められています。この優先順位は入力で与えられる順番に対応しており、先に与えられた通路ほど優先順位が高くなります。
また、各部屋には照明が設置されており、その照明の色は赤(0)または青(1)のいずれかです。
高橋君は部屋 1 から探検を開始します。部屋 1 は探検開始時に訪問済みとなります。探検は以下のルールに従って行われます。
- 現在いる部屋から出ているすべての通路を、優先順位が高い順に 1 つずつ確認していきます。各通路について、その通路の先の部屋がすでに訪問済みであれば、その通路を飛ばして次の通路に進みます。通路の先の部屋が未訪問である場合、手順 2 に進みます。
- 確認した未訪問の部屋の照明の色が現在いる部屋の照明の色と同じ場合、その部屋には移動せず、その通路を飛ばして次の通路の確認に進みます。この部屋は訪問済みにはならず、今後別の機会に再び確認対象となりえます。
- 確認した未訪問の部屋の照明の色が現在いる部屋の照明の色と異なる場合、その部屋に移動し、訪問済みとします。移動先の部屋を新たに「現在の部屋」として、手順 1 からやり直します。
- 現在の部屋から出るすべての通路の確認が完了し、移動先が見つからなかった場合(すべて訪問済みまたは手順 2 で飛ばされた場合)、あるいはそもそも通路が出ていない場合は、バックトラックを行います。すなわち、通路の向きに関係なく、現在の部屋に移動してくる直前にいた部屋に戻ります。戻った先の部屋を「現在の部屋」として、その部屋で中断していた通路の確認を、中断した箇所の次の通路から再開し、手順 1〜3 の処理を続けます。
- 部屋 1 でバックトラックが発生した場合(すなわち、部屋 1 で移動先が見つからず、かつ戻れる部屋がない場合)、探検は終了します。
ここで、「中断していた通路の確認を再開する」とは、具体的には次のことを意味します。ある部屋 X で通路を優先順位順に確認している途中、手順 3 により別の部屋 Y に移動したとします。その後(部屋 Y 以降の探索やバックトラックを経て)部屋 X に戻ってきたとき、部屋 X では、部屋 Y への通路の次の優先順位の通路から確認を再開します。再開後も、各通路の先の部屋が訪問済みかどうかはその時点での状態で判定します。
探検終了時に、一度も訪れなかった部屋の番号を小さい順にすべて出力してください。すべての部屋を訪れた場合は COMPLETE と出力してください。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- C_i \in \{0, 1\} (1 \leq i \leq N)
- 1 \leq U_j, V_j \leq N (1 \leq j \leq M)
- U_j \neq V_j (1 \leq j \leq M)
- 同じ (U_j, V_j) の組は複数回与えられない
- グラフは連結とは限らない
- 入力はすべて整数である
入力
N M C_1 C_2 \cdots C_N U_1 V_1 U_2 V_2 : U_M V_M
- 1 行目には、部屋の数を表す整数 N と、通路の本数を表す整数 M が、スペース区切りで与えられる。
- 2 行目には、各部屋の照明の色を表す整数 C_i が N 個、スペース区切りで与えられる。C_i = 0 のとき赤、C_i = 1 のとき青を表す。
- 3 行目から M 行にわたって、通路の情報が与えられる。各行の U_j V_j は「部屋 U_j から部屋 V_j への一方通行の通路がある」ことを意味する。同じ部屋 U_j から出る通路の優先順位は、入力で先に与えられたもの(上の行)ほど高い。
出力
一度も訪れなかった部屋の番号を小さい順にスペース区切りで 1 行で出力せよ。すべての部屋を訪れた場合は COMPLETE と出力せよ。
入力例 1
5 5 0 1 0 1 0 1 2 2 3 3 4 4 5 1 5
出力例 1
COMPLETE
入力例 2
6 7 0 0 1 1 0 1 1 2 1 3 3 4 3 5 5 6 2 6 4 5
出力例 2
2 4
入力例 3
12 17 0 1 1 0 1 0 1 0 1 0 0 1 1 3 1 2 1 4 3 5 3 6 6 7 7 8 8 9 9 10 10 11 10 12 12 4 4 5 5 2 2 11 11 3 6 10
出力例 3
COMPLETE
入力例 4
30 45 0 1 0 1 0 1 0 1 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 3 1 2 1 4 2 5 2 6 5 7 5 8 8 9 8 10 10 11 11 12 12 13 12 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 20 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 1 4 9 4 10 6 13 6 14 7 15 9 16 13 21 21 22 3 6 3 7 11 4 18 2 23 5 25 9 27 13
出力例 4
3 6 7 9
入力例 5
1 0 0
出力例 5
COMPLETE
Score : 366 pts
Problem Statement
Takahashi explores a cave consisting of N rooms. The rooms are numbered from 1 to N. There are M one-way passages between rooms. Each passage has a fixed direction from one room to another.
For each room, the passages leading out of it have a defined priority order. This priority corresponds to the order given in the input, with passages given earlier having higher priority.
Additionally, each room has a light installed, and the color of the light is either red (0) or blue (1).
Takahashi starts exploring from room 1. Room 1 is marked as visited at the start of the exploration. The exploration follows these rules:
- Check all passages leading out of the current room one by one in order of decreasing priority. For each passage, if the room at the end of the passage has already been visited, skip that passage and move on to the next one. If the room at the end of the passage is unvisited, proceed to step 2.
- If the light color of the confirmed unvisited room is the same as the light color of the current room, do not move to that room, skip that passage, and continue checking the next passage. This room does not become visited, and it may be checked again on a different occasion in the future.
- If the light color of the confirmed unvisited room is different from the light color of the current room, move to that room and mark it as visited. Set the destination room as the new "current room" and restart from step 1.
- If all passages from the current room have been checked and no destination was found (all were visited or skipped in step 2), or if there are no passages leading out of the current room, perform a backtrack. That is, regardless of passage directions, return to the room you were in immediately before moving to the current room. Set that room as the "current room" and resume checking passages from where it was interrupted, starting from the next passage after the interruption point, continuing the process of steps 1–3.
- If a backtrack occurs at room 1 (that is, no destination is found from room 1 and there is no room to return to), the exploration ends.
Here, "resuming the interrupted passage check" specifically means the following: Suppose while checking passages in priority order from some room X, you moved to another room Y via step 3. After that (following exploration from room Y onward and backtracks), when you return to room X, you resume checking from the passage with the next priority after the passage to room Y. After resuming, whether each passage's destination room has been visited is judged based on the state at that point in time.
When the exploration ends, output all room numbers that were never visited in ascending order. If all rooms were visited, output COMPLETE.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- C_i \in \{0, 1\} (1 \leq i \leq N)
- 1 \leq U_j, V_j \leq N (1 \leq j \leq M)
- U_j \neq V_j (1 \leq j \leq M)
- The same pair (U_j, V_j) is not given more than once
- The graph is not necessarily connected
- All input values are integers
Input
N M C_1 C_2 \cdots C_N U_1 V_1 U_2 V_2 : U_M V_M
- The first line contains an integer N representing the number of rooms and an integer M representing the number of passages, separated by a space.
- The second line contains N integers C_i separated by spaces, representing the light color of each room. C_i = 0 means red, and C_i = 1 means blue.
- From the third line onward, M lines of passage information are given. Each line U_j V_j means "there is a one-way passage from room U_j to room V_j." For passages leading out of the same room U_j, those given earlier in the input (upper lines) have higher priority.
Output
Output the room numbers that were never visited in ascending order, separated by spaces, on a single line. If all rooms were visited, output COMPLETE.
Sample Input 1
5 5 0 1 0 1 0 1 2 2 3 3 4 4 5 1 5
Sample Output 1
COMPLETE
Sample Input 2
6 7 0 0 1 1 0 1 1 2 1 3 3 4 3 5 5 6 2 6 4 5
Sample Output 2
2 4
Sample Input 3
12 17 0 1 1 0 1 0 1 0 1 0 0 1 1 3 1 2 1 4 3 5 3 6 6 7 7 8 8 9 9 10 10 11 10 12 12 4 4 5 5 2 2 11 11 3 6 10
Sample Output 3
COMPLETE
Sample Input 4
30 45 0 1 0 1 0 1 0 1 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 3 1 2 1 4 2 5 2 6 5 7 5 8 8 9 8 10 10 11 11 12 12 13 12 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 20 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 1 4 9 4 10 6 13 6 14 7 15 9 16 13 21 21 22 3 6 3 7 11 4 18 2 23 5 25 9 27 13
Sample Output 4
3 6 7 9
Sample Input 5
1 0 0
Sample Output 5
COMPLETE
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君の学校では、 N 人の生徒が期末テストを受けました。各生徒には 1 から N までの出席番号が付けられています。
テストの結果、生徒 i の得点は S_i でした。
ここで基準値を次のように定めます。 N 人の得点を小さい順に並べた列を A_1 \le A_2 \le \cdots \le A_N とします。同じ得点の生徒が複数いる場合も、それぞれを別の要素として列に含めます(したがって列の長さは常に N です)。このとき、先頭から \lceil N/2 \rceil 番目の値 A_{\lceil N/2 \rceil} を基準値とします。ここで \lceil \cdot \rceil は天井関数(切り上げ)を表します。たとえば N = 5 のときは \lceil 5/2 \rceil = 3 番目、 N = 4 のときは \lceil 4/2 \rceil = 2 番目の値が基準値です。なお、同じ得点の要素が複数あっても、それらの間の並び順によらず A_{\lceil N/2 \rceil} の値は一意に定まります。
このテストでは、得点が厳密に基準値未満の生徒が補習の対象となります。基準値と同じ得点の生徒は補習の対象にはなりません。
その後、先生は採点の見直しにより、生徒の得点を Q 回にわたって更新します。得点が増える場合も減る場合もあり、同じ生徒の得点が複数回更新されることもあります。
j 回目の更新( 1 \le j \le Q )では、生徒 T_j の得点を V_j に置き換えます。
各更新の直後に、補習の対象となる生徒の人数を求めてください。すなわち、その時点での N 人の得点から定まる基準値に対して、得点が厳密に基準値未満である生徒が何人いるかを出力してください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 0 \leq S_i \leq 10^9 ( 1 \leq i \leq N )
- 1 \leq T_j \leq N ( 1 \leq j \leq Q )
- 0 \leq V_j \leq 10^9 ( 1 \leq j \leq Q )
- 入力はすべて整数である。
入力
N Q S_1 S_2 \ldots S_N T_1 V_1 T_2 V_2 \vdots T_Q V_Q
- 1 行目には、生徒の人数を表す整数 N と、更新の回数を表す整数 Q が、スペース区切りで与えられる。
- 2 行目には、各生徒の初期得点 S_1, S_2, \ldots, S_N が、スペース区切りで与えられる。
- 続く Q 行にわたって、各更新の内容が与えられる。
- 2 + j 行目( 1 \le j \le Q )には、 j 回目の更新で得点を変更する生徒の出席番号 T_j と、変更後の得点 V_j が、スペース区切りで与えられる。
出力
Q 行出力せよ。 j 行目( 1 \le j \le Q )には、 j 回目の更新の直後における補習対象の生徒の人数を整数で出力せよ。
入力例 1
5 3 50 30 70 20 60 2 55 4 80 1 10
出力例 1
2 2 2
入力例 2
4 2 40 40 40 40 1 30 3 30
出力例 2
1 0
入力例 3
10 5 15 92 35 78 54 12 67 43 89 26 3 80 7 10 1 100 5 20 10 50
出力例 3
4 4 4 4 4
入力例 4
15 8 100 200 300 400 500 600 700 800 900 1000 50 150 250 350 450 1 999 11 999 5 0 8 0 15 1000 3 500 12 500 7 0
出力例 4
7 7 7 7 7 7 6 7
入力例 5
1 1 100 1 0
出力例 5
0
Score : 400 pts
Problem Statement
At Takahashi's school, N students took a final exam. Each student is assigned a student ID number from 1 to N.
As a result of the exam, student i scored S_i points.
The threshold value is defined as follows. Sort the scores of all N students in non-decreasing order to form a sequence A_1 \le A_2 \le \cdots \le A_N. If multiple students have the same score, each is included as a separate element in the sequence (so the sequence always has length N). The threshold value is the \lceil N/2 \rceil-th value A_{\lceil N/2 \rceil}, where \lceil \cdot \rceil denotes the ceiling function (rounding up). For example, when N = 5, the threshold is the \lceil 5/2 \rceil = 3-rd value, and when N = 4, it is the \lceil 4/2 \rceil = 2-nd value. Note that even if there are multiple elements with the same score, the value of A_{\lceil N/2 \rceil} is uniquely determined regardless of how ties are ordered.
In this exam, students whose scores are strictly less than the threshold value are required to attend supplementary lessons. Students whose scores are equal to the threshold value are not required to attend.
Afterwards, the teacher updates the students' scores Q times due to regrading. Scores may increase or decrease, and the same student's score may be updated multiple times.
In the j-th update (1 \le j \le Q), the score of student T_j is replaced with V_j.
Immediately after each update, determine the number of students who are required to attend supplementary lessons. That is, output the number of students whose scores are strictly less than the threshold value determined from the current scores of all N students.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 0 \leq S_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq T_j \leq N (1 \leq j \leq Q)
- 0 \leq V_j \leq 10^9 (1 \leq j \leq Q)
- All input values are integers.
Input
N Q S_1 S_2 \ldots S_N T_1 V_1 T_2 V_2 \vdots T_Q V_Q
- The first line contains an integer N representing the number of students and an integer Q representing the number of updates, separated by a space.
- The second line contains the initial scores S_1, S_2, \ldots, S_N of each student, separated by spaces.
- The following Q lines describe each update.
- The (2 + j)-th line (1 \le j \le Q) contains the student ID T_j of the student whose score is changed in the j-th update and the new score V_j, separated by a space.
Output
Print Q lines. The j-th line (1 \le j \le Q) should contain an integer representing the number of students required to attend supplementary lessons immediately after the j-th update.
Sample Input 1
5 3 50 30 70 20 60 2 55 4 80 1 10
Sample Output 1
2 2 2
Sample Input 2
4 2 40 40 40 40 1 30 3 30
Sample Output 2
1 0
Sample Input 3
10 5 15 92 35 78 54 12 67 43 89 26 3 80 7 10 1 100 5 20 10 50
Sample Output 3
4 4 4 4 4
Sample Input 4
15 8 100 200 300 400 500 600 700 800 900 1000 50 150 250 350 450 1 999 11 999 5 0 8 0 15 1000 3 500 12 500 7 0
Sample Output 4
7 7 7 7 7 7 6 7
Sample Input 5
1 1 100 1 0
Sample Output 5
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 433 点
問題文
高橋君は、 N 人の社員からなる会社の管理システムを担当しています。社員には 1 から N までの番号が付けられています。
この会社には明確な上下関係があります。社員 1 が社長であり、社員 1 以外の各社員 i ( 2 \leq i \leq N )にはちょうど 1 人の直属の上司 P_i がいます。この上司関係は、社員 1 を根とする根付き木をなしています。
社員 u と社員 v の距離 \mathrm{dist}(u, v) とは、根付き木上で u から v へ至る唯一の単純パスに含まれる辺の数のことをいいます。特に、 \mathrm{dist}(u, u) = 0 です。
社員 v の深さ \mathrm{depth}(v) とは、根からの距離 \mathrm{dist}(1, v) のことをいいます。
社員 v が社員 u の祖先であるとは、 u から根(社員 1 )に向かって直属の上司を 0 回以上繰り返したどっていく過程で v に到達できることをいいます。この定義から、社員 u 自身( 0 回たどった場合)と社員 1 (根までたどった場合)は必ず u の祖先となります。
空でない社員の集合 S の最小共通祖先(LCA) \mathrm{LCA}(S) とは、 S に含まれるすべての社員の共通の祖先のうち、深さが最も大きい(根から最も遠い)社員のことをいいます。根付き木の性質から、このような社員はちょうど 1 人に定まります。集合の要素が 1 人だけの場合、 \mathrm{LCA}(\{u\}) = u です。
高橋君は Q 回の「会議招集クエリ」を処理しなければなりません。
j 番目のクエリ( 1 \leq j \leq Q )では、 K_j 人の相異なる社員 V_{j,1}, V_{j,2}, \ldots, V_{j,K_j} が会議に招集されます。会議の進行役は、招集された K_j 人の LCA が務めます。なお、進行役自身が招集メンバーに含まれる場合もあれば、含まれない場合もあります。
各クエリについて、以下の値 1 と値 2 を求めてください。
値 1: 進行役 r = \mathrm{LCA}(\{V_{j,1}, V_{j,2}, \ldots, V_{j,K_j}\}) から、招集された K_j 人の各社員への距離の合計を求めてください。すなわち、
\sum_{i=1}^{K_j} \mathrm{dist}(r, V_{j,i})
を求めてください。( K_j = 1 のときは r = V_{j,1} となるので、値 1 は 0 です。)
値 2: 招集された K_j 人のうちちょうど 1 人を欠席させ、残りの K_j - 1 人で会議を行うことを考えます。残った K_j - 1 人の LCA を新たな進行役 r' とし、 r' から残った K_j - 1 人の各社員への距離の合計を求めます。欠席させる 1 人のすべての選び方( K_j 通り)の中での、この距離の合計の最小値を求めてください。
S_j = \{V_{j,1}, V_{j,2}, \ldots, V_{j,K_j}\} とおくと、求める値は
\min_{1 \leq t \leq K_j} \sum_{\substack{1 \leq i \leq K_j \\ i \neq t}} \mathrm{dist}\!\left(\mathrm{LCA}\!\left(S_j \setminus \{V_{j,t}\}\right),\, V_{j,i}\right)
です。なお、残った K_j - 1 人の進行役 r' もまた、残ったメンバーに含まれる場合と含まれない場合があります。
ただし K_j = 1 の場合、 1 人を欠席させると集合が空になり LCA が定義できないため、値 2 として -1 を出力してください。 K_j \geq 2 のときは、誰を欠席させても残りの集合は空にならないため、LCA は必ず定まります。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq P_i \leq i - 1 ( 2 \leq i \leq N )
- 1 \leq K_j \leq N ( 1 \leq j \leq Q )
- 1 \leq V_{j,k} \leq N ( 1 \leq k \leq K_j )
- 各クエリにおいて V_{j,1}, V_{j,2}, \ldots, V_{j,K_j} はすべて相異なる
- \displaystyle \sum_{j=1}^{Q} K_j \leq 2 \times 10^5
- 入力はすべて整数である
入力
N Q
P_2 P_3 \ldots P_N
K_1 V_{1,1} V_{1,2} \ldots V_{1,K_1}
K_2 V_{2,1} V_{2,2} \ldots V_{2,K_2}
\vdots
K_Q V_{Q,1} V_{Q,2} \ldots V_{Q,K_Q}
- 1 行目には、社員の人数を表す整数 N とクエリの個数を表す整数 Q が、スペース区切りで与えられる。
- 2 行目には、社員 2 から社員 N までの直属の上司を表す N - 1 個の整数 P_2, P_3, \ldots, P_N が、スペース区切りで与えられる。 P_i は社員 i の直属の上司の番号である。ただし N=1 のときは空行が与えられる。
- 続く Q 行にわたって、各クエリが与えられる。 j 番目のクエリ( 1 \leq j \leq Q )は第 j + 2 行目に記述され、先頭に招集人数を表す整数 K_j 、続いて招集された社員の番号を表す K_j 個の整数 V_{j,1}, V_{j,2}, \ldots, V_{j,K_j} がスペース区切りで与えられる。
出力
Q 行出力せよ。 j 行目( 1 \leq j \leq Q )には、 j 番目のクエリに対する値 1 と値 2 をスペース区切りで出力せよ。
入力例 1
7 4 1 1 2 2 3 3 3 4 5 2 2 4 6 1 7 4 4 5 6 7
出力例 1
2 1 4 0 0 -1 8 6
入力例 2
5 5 1 2 3 4 1 3 2 2 5 3 1 4 5 1 1 4 2 3 4 5
出力例 2
0 -1 3 0 7 1 0 -1 6 3
入力例 3
15 8 1 1 2 2 3 3 4 4 5 6 6 7 10 10 5 8 9 14 15 10 4 11 12 13 7 6 4 5 6 7 14 15 3 2 8 9 7 1 3 6 11 12 13 15 2 14 15 1 5 8 8 9 10 11 12 13 14 15
出力例 3
12 9 7 5 16 12 4 2 16 12 2 0 0 -1 26 22
入力例 4
30 12 1 1 1 2 2 3 3 3 4 4 5 5 6 7 7 8 9 9 10 11 11 12 12 14 17 17 19 22 22 10 23 24 13 25 14 6 20 29 30 22 8 15 16 26 27 17 18 28 19 5 2 5 12 23 24 6 10 20 4 11 21 30 3 23 25 30 12 1 2 3 4 5 7 10 12 15 20 23 29 2 26 27 1 30 7 13 14 15 16 17 18 19 9 6 8 9 11 24 25 26 28 29 4 3 7 15 16 5 21 22 29 30 11
出力例 4
34 30 19 16 9 5 9 6 12 6 26 22 2 0 0 -1 21 18 28 24 5 2 6 4
入力例 5
1 3 1 1 1 1 1 1
出力例 5
0 -1 0 -1 0 -1
Score : 433 pts
Problem Statement
Takahashi is in charge of the management system for a company consisting of N employees. The employees are numbered 1 through N.
The company has a clear hierarchy. Employee 1 is the president, and each employee i (2 \leq i \leq N) other than employee 1 has exactly one direct superior P_i. This hierarchy forms a rooted tree with employee 1 as the root.
The distance \mathrm{dist}(u, v) between employee u and employee v is defined as the number of edges on the unique simple path between u and v on the rooted tree. In particular, \mathrm{dist}(u, u) = 0.
The depth \mathrm{depth}(v) of employee v is defined as the distance from the root, \mathrm{dist}(1, v).
Employee v is said to be an ancestor of employee u if v can be reached by starting from u and repeatedly moving to the direct superior 0 or more times toward the root (employee 1). By this definition, employee u themself (0 steps) and employee 1 (tracing all the way to the root) are always ancestors of u.
The lowest common ancestor (LCA) \mathrm{LCA}(S) of a non-empty set of employees S is defined as the employee with the maximum depth (farthest from the root) among all common ancestors of the employees in S. By the properties of rooted trees, such an employee is uniquely determined. If the set contains only one employee, \mathrm{LCA}(\{u\}) = u.
Takahashi needs to process Q "meeting summons queries".
In the j-th query (1 \leq j \leq Q), K_j distinct employees V_{j,1}, V_{j,2}, \ldots, V_{j,K_j} are summoned to a meeting. The facilitator of the meeting is the LCA of the summoned K_j employees. Note that the facilitator may or may not be among the summoned members.
For each query, find Value 1 and Value 2 described below.
Value 1: Find the sum of distances from the facilitator r = \mathrm{LCA}(\{V_{j,1}, V_{j,2}, \ldots, V_{j,K_j}\}) to each of the K_j summoned employees. That is, compute
\sum_{i=1}^{K_j} \mathrm{dist}(r, V_{j,i})
(When K_j = 1, r = V_{j,1}, so Value 1 is 0.)
Value 2: Consider having exactly 1 of the K_j summoned employees be absent and holding the meeting with the remaining K_j - 1 employees. Let r' be the LCA of the remaining K_j - 1 employees as the new facilitator, and compute the sum of distances from r' to each of the remaining K_j - 1 employees. Find the minimum possible sum of distances over all choices of the absent employee (K_j ways).
Letting S_j = \{V_{j,1}, V_{j,2}, \ldots, V_{j,K_j}\}, the value to find is
\min_{1 \leq t \leq K_j} \sum_{\substack{1 \leq i \leq K_j \\ i \neq t}} \mathrm{dist}\!\left(\mathrm{LCA}\!\left(S_j \setminus \{V_{j,t}\}\right),\, V_{j,i}\right)
Note that the facilitator r' of the remaining K_j - 1 employees may or may not be included in the remaining members.
However, if K_j = 1, leaving 1 person out results in an empty set and the LCA cannot be defined, so output -1 as Value 2. When K_j \geq 2, no matter who is absent, the remaining set is non-empty, so the LCA is always uniquely determined.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq P_i \leq i - 1 (2 \leq i \leq N)
- 1 \leq K_j \leq N (1 \leq j \leq Q)
- 1 \leq V_{j,k} \leq N (1 \leq k \leq K_j)
- V_{j,1}, V_{j,2}, \ldots, V_{j,K_j} are all distinct in each query
- \displaystyle \sum_{j=1}^{Q} K_j \leq 2 \times 10^5
- All input values are integers.
Input
N Q
P_2 P_3 \ldots P_N
K_1 V_{1,1} V_{1,2} \ldots V_{1,K_1}
K_2 V_{2,1} V_{2,2} \ldots V_{2,K_2}
\vdots
K_Q V_{Q,1} V_{Q,2} \ldots V_{Q,K_Q}
- The first line contains an integer N representing the number of employees and an integer Q representing the number of queries, separated by a space.
- The second line contains N - 1 integers P_2, P_3, \ldots, P_N separated by spaces, representing the direct superiors of employee 2 through employee N. P_i is the number of the direct superior of employee i. However, if N=1, an empty line is provided.
- The following Q lines describe the queries. The j-th query (1 \leq j \leq Q) is given on the (j + 2)-th line, starting with an integer K_j representing the number of summoned employees, followed by K_j integers V_{j,1}, V_{j,2}, \ldots, V_{j,K_j} representing the numbers of the summoned employees, separated by spaces.
Output
Print Q lines. The j-th line (1 \leq j \leq Q) should contain Value 1 and Value 2 for the j-th query, separated by a space.
Sample Input 1
7 4 1 1 2 2 3 3 3 4 5 2 2 4 6 1 7 4 4 5 6 7
Sample Output 1
2 1 4 0 0 -1 8 6
Sample Input 2
5 5 1 2 3 4 1 3 2 2 5 3 1 4 5 1 1 4 2 3 4 5
Sample Output 2
0 -1 3 0 7 1 0 -1 6 3
Sample Input 3
15 8 1 1 2 2 3 3 4 4 5 6 6 7 10 10 5 8 9 14 15 10 4 11 12 13 7 6 4 5 6 7 14 15 3 2 8 9 7 1 3 6 11 12 13 15 2 14 15 1 5 8 8 9 10 11 12 13 14 15
Sample Output 3
12 9 7 5 16 12 4 2 16 12 2 0 0 -1 26 22
Sample Input 4
30 12 1 1 1 2 2 3 3 3 4 4 5 5 6 7 7 8 9 9 10 11 11 12 12 14 17 17 19 22 22 10 23 24 13 25 14 6 20 29 30 22 8 15 16 26 27 17 18 28 19 5 2 5 12 23 24 6 10 20 4 11 21 30 3 23 25 30 12 1 2 3 4 5 7 10 12 15 20 23 29 2 26 27 1 30 7 13 14 15 16 17 18 19 9 6 8 9 11 24 25 26 28 29 4 3 7 15 16 5 21 22 29 30 11
Sample Output 4
34 30 19 16 9 5 9 6 12 6 26 22 2 0 0 -1 21 18 28 24 5 2 6 4
Sample Input 5
1 3 1 1 1 1 1 1
Sample Output 5
0 -1 0 -1 0 -1