Codeforces Round 949 (Div. 2)
F
Turtle and Paths on a Tree
Codeforces
3000
原题
← E→
翻译中…

Note the unusual definition of $\text{MEX}$ in this problem.

翻译中…

Piggy gave Turtle a binary tree$^{\dagger}$ with $n$ vertices and a sequence $a_1, a_2, \ldots, a_n$ on his birthday. The binary tree is rooted at vertex $1$.

翻译中…

If a set of paths $P = \{(x_i, y_i)\}$ in the tree covers each edge exactly once, then Turtle will think that the set of paths is good. Note that a good set of paths can cover a vertex twice or more.

翻译中…

Turtle defines the value of a set of paths as $\sum\limits_{(x, y) \in P} f(x, y)$, where $f(x, y)$ denotes the $\text{MEX}^{\ddagger}$ of all $a_u$ such that vertex $u$ is on the simple path from $x$ to $y$ in the tree (including the starting vertex $x$ and the ending vertex $y$).

翻译中…

Turtle wonders the minimum value over all good sets of paths. Please help him calculate the answer!

翻译中…

$^{\dagger}$A binary tree is a tree where every non-leaf vertex has at most $2$ sons.

翻译中…

$^{\ddagger}\text{MEX}$ of a collection of integers $c_1, c_2, \ldots, c_k$ is defined as the smallest positive integer $x$ which does not occur in the collection $c$. For example, $\text{MEX}$ of $[3, 3, 1, 4]$ is $2$, $\text{MEX}$ of $[2, 3]$ is $1$.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains a single integer $n$ ($2 \le n \le 2.5 \cdot 10^4$) — the number of vertices in the tree.

翻译中…

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of the sequence $a$.

翻译中…

The third line of each test case contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \le p_i \lt i$) — the parent of each vertex in the tree.

翻译中…

Additional constraint on the input: the given tree is a binary tree, that is, every non-leaf vertex has at most $2$ sons.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

翻译中…

Output

翻译中…

For each test case, output a single integer — the minimum value over all good sets of paths.

翻译中…

Note

翻译中…

In the first test case, the tree is as follows. The number in brackets denotes the weight of the vertex:

翻译中…

image

翻译中…

The good set of paths with the minimum value is $\{(2, 3), (4, 5)\}$.

翻译中…

Note that in this test case $\{(4, 5)\}$ and $\{(3, 4), (4, 5)\}$ are not good sets of paths because each edge should be covered exactly once.

翻译中…

In the second test case, the tree is as follows:

翻译中…

image

翻译中…

The set of good paths with the minimum value is $\{(1, 2), (1, 3), (4, 5)\}$.

翻译中…

In the third test case, the tree is as follows:

翻译中…

image

翻译中…

The set of good paths with the minimum value is $\{(1, 6), (3, 5)\}$.

翻译中…

样例

Input #1
5
5
3 2 2 1 1
1 1 2 2
5
3 2 1 1 1
1 1 2 2
6
1 2 1 2 1 3
1 2 3 3 4
7
2 1 2 3 1 2 1
1 1 2 2 3 3
10
1 2 2 1 4 2 3 1 2 1
1 1 2 2 3 3 4 5 5
Output #1
4
6
6
6
7
InputCopy OutputCopy
题目信息
题号
1981F
时间限制
4 s
内存限制
1024 MB
标签
数据结构
动态规划
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。