#P9634. [ICPC2020 Nanjing R] Monster Hunter
[ICPC2020 Nanjing R] Monster Hunter
题目描述
There is a rooted tree with vertices and the root vertex is . In each vertex, there is a monster. The hit points of the monster in the -th vertex is .
Kotori would like to kill all the monsters. The monster in the -th vertex could be killed if the monster in the direct parent of the -th vertex has been killed. The power needed to kill the -th monster is the sum of and the hit points of all other living monsters who lives in a vertex whose direct parent is . Formally, the power equals to
$$hp_i + \sum_{\begin{array}{c}\text{the monster in vertex } j \text{ is \bf{alive}} \\ \text{and } i \text{ is the direct parent of } j \end{array}} hp_j $$In addition, Kotori can use some magic spells. If she uses one magic spell, she can kill any monster using power without any restriction. That is, she can choose a monster even if the monster in the direct parent is alive.
For each , Kotori would like to know, respectively, the minimum total power needed to kill all the monsters if she can use magic spells.
输入格式
There are multiple test cases. The first line of input contains an integer indicating the number of test cases. For each test case:
The first line contains an integer (), indicating the number of vertices.
The second line contains integers (), where means the direct parent of vertex .
The third line contains integers () indicating the hit points of each monster.
It's guaranteed that the sum of of all test cases will not exceed .
输出格式
For each test case output one line containing integers separated by a space, where indicates the minimum total power needed to kill all the monsters if Kotori can use magic spells.
Please, DO NOT output extra spaces at the end of each line, otherwise your answer may be considered incorrect!
题目大意
给定一棵树和点权 ,如果需要标记一个点 的话,你会付出 再加上所有 的直接子节点的权值的代价,必须先标记 的父节点才能标记 (根节点除外)。你可以使用魔法,每使用一次魔法,可以选择一个未被标记的点 进行无代价标记(即在 的父节点未被标记的时候)。求所有点都被标记掉的最少代价。
3
5
1 2 3 4
1 2 3 4 5
9
1 2 3 4 3 4 6 6
8 4 9 4 4 5 2 4 1
12
1 2 2 4 5 3 4 3 8 10 11
9 1 3 5 10 10 7 3 7 9 4 9
29 16 9 4 1 0
74 47 35 25 15 11 7 3 1 0
145 115 93 73 55 42 32 22 14 8 4 1 0