CF1746D.Paths on the Tree
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a rooted tree consisting of n vertices. The vertices are numbered from 1 to n , and the root is the vertex 1 . You are also given a score array s1,s2,…,sn .
A multiset of k simple paths is called valid if the following two conditions are both true.
- Each path starts from 1 .
- Let ci be the number of paths covering vertex i . For each pair of vertices (u,v) ( 2≤u,v≤n ) that have the same parent, ∣cu−cv∣≤1 holds.
The value of the path multiset is defined as i=1∑ncisi .It can be shown that it is always possible to find at least one valid multiset. Find the maximum value among all valid multisets.
输入格式
Each test contains multiple test cases. The first line contains a single integer t ( 1≤t≤104 ) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two space-separated integers n ( 2≤n≤2⋅105 ) and k ( 1≤k≤109 ) — the size of the tree and the required number of paths.
The second line contains n−1 space-separated integers p2,p3,…,pn ( 1≤pi≤n ), where pi is the parent of the i -th vertex. It is guaranteed that this value describe a valid tree with root 1 .
The third line contains n space-separated integers s1,s2,…,sn ( 0≤si≤104 ) — the scores of the vertices.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 .
输出格式
For each test case, print a single integer — the maximum value of a path multiset.
输入输出样例
输入#1
2 5 4 1 2 1 3 6 2 1 5 7 5 3 1 2 1 3 6 6 1 4 10
输出#1
54 56
说明/提示
In the first test case, one of optimal solutions is four paths 1→2→3→5 , 1→2→3→5 , 1→4 , 1→4 , here c=[4,2,2,2,2] . The value equals to 4⋅6+2⋅2+2⋅1+2⋅5+2⋅7=54 .
In the second test case, one of optimal solution is three paths 1→2→3→5 , 1→2→3→5 , 1→4 , here c=[3,2,2,1,2] . The value equals to 3⋅6+2⋅6+2⋅1+1⋅4+2⋅10=56 .