/
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