QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 512 MB Total points: 100 Difficulty: [show] Hackable ✓

#15459. 树的价值

الإحصائيات

Given a rooted tree with $n$ nodes, where node 1 is the root and the parent of node $i$ ($2 \le i \le n$) is node $p_i$.

For $1 \le i \le n$, define the depth $d_i$ of node $i$ as the number of edges on the simple path from node 1 to node $i$. That is, $d_1 = 0$ and $d_i = d_{p_i} + 1$ ($2 \le i \le n$). Define the height $h$ of the rooted tree as the maximum depth of all nodes, i.e., $h = \max_{i=1}^n d_i$.

Given an upper bound $m$ on the height. In this problem, the height of the given rooted tree does not exceed $m$.

You need to assign a non-negative integer as the weight to each node. For $1 \le i \le n$, if the weight of node $i$ is $a_i$, let $S_i$ denote the set of weights of all nodes in the subtree of node $i$. For each weight assignment scheme, define the value of the tree as $\sum_{i=1}^n \text{mex}(S_i)$, where $\text{mex}(S)$ denotes the smallest non-negative integer not present in the set $S$. For example, in the figure below, if we set $a_1 = 3$, $a_2 = 2$, $a_3 = a_4 = 0$, $a_5 = 1$, then $S_1 = \{0, 1, 2, 3\}$, $S_2 = \{0, 1, 2\}$, $S_3 = \{0\}$, $S_4 = \{0\}$, $S_5 = \{1\}$, and the value of the tree is $4 + 3 + 1 + 1 + 0 = 9$.

You need to find the maximum value of the tree among all possible weight assignment schemes.

Input

The first line contains a positive integer $t$, representing the number of test cases.

For each test case: The first line contains two positive integers $n, m$, representing the number of nodes and the upper bound of the height, respectively. The second line contains $n - 1$ positive integers $p_2, p_3, \dots, p_n$, representing the parent of each node.

Output

For each test case, output a single line containing a non-negative integer, representing the maximum value of the tree.

Examples

Input 1

2
5 2
1 1 2 2
7 2
1 1 2 2 2 3

Output 1

9
13

Note 1

This example contains two test cases. For the first test case, one can set $a_1 = 3, a_2 = 2, a_3 = a_4 = 0, a_5 = 1$, then the value of the tree is $4 + 3 + 1 + 1 + 0 = 9$. For the second test case, one can set $a_1 = 4, a_2 = 3, a_4 = 2, a_3 = a_6 = 1, a_5 = a_7 = 0$, then the value of the tree is $5 + 4 + 2 + 0 + 1 + 0 + 1 = 13$.

Examples 2

See tree/tree2.in and tree/tree2.ans in the contestant's directory. This example satisfies the constraints of test cases 3 and 4.

Examples 3

See tree/tree3.in and tree/tree3.ans in the contestant's directory. This example satisfies the constraints of test cases 7 and 8.

Examples 4

See tree/tree4.in and tree/tree4.ans in the contestant's directory. This example satisfies the constraints of test cases 13 and 14.

Examples 5

See tree/tree5.in and tree/tree5.ans in the contestant's directory. This example satisfies the constraints of test cases 18 and 19.

Constraints

For all test cases: $1 \le t \le 5$; $2 \le n \le 8,000$, $1 \le m \le \min(n - 1, 800)$; For all $2 \le i \le n$, $1 \le p_i \le i - 1$; The height of the given rooted tree does not exceed $m$.

Test Case ID $n \le$ $m \le$
1, 2 7 $n - 1$
3, 4 13 $n - 1$
5, 6 18 $n - 1$
7, 8 40 $n - 1$
9, 10 120 $n - 1$
11, 12 360 $n - 1$
13, 14 4,000 2
15 $\sim$ 17 4,000 10
18, 19 4,000 50
20 $\sim$ 25 8,000 800

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.