#P9634. [ICPC2020 Nanjing R] Monster Hunter

[ICPC2020 Nanjing R] Monster Hunter

题目描述

There is a rooted tree with nn vertices and the root vertex is 11. In each vertex, there is a monster. The hit points of the monster in the ii-th vertex is hpihp_i.

Kotori would like to kill all the monsters. The monster in the ii-th vertex could be killed if the monster in the direct parent of the ii-th vertex has been killed. The power needed to kill the ii-th monster is the sum of hpihp_i and the hit points of all other living monsters who lives in a vertex jj whose direct parent is ii. 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 00 power without any restriction. That is, she can choose a monster even if the monster in the direct parent is alive.

For each m=0,1,2,,nm=0,1,2,\cdots,n, Kotori would like to know, respectively, the minimum total power needed to kill all the monsters if she can use mm magic spells.

输入格式

There are multiple test cases. The first line of input contains an integer TT indicating the number of test cases. For each test case:

The first line contains an integer nn (2n2×1032 \le n \le 2 \times 10^3), indicating the number of vertices.

The second line contains (n1)(n-1) integers p2,p3,,pnp_2,p_3,\cdots,p_n (1pi<i1 \le p_i < i), where pip_i means the direct parent of vertex ii.

The third line contains nn integers hp1,hp2,,hpnhp_1,hp_2,\cdots,hp_n (1hpi1091 \le hp_i \le 10^9) indicating the hit points of each monster.

It's guaranteed that the sum of nn of all test cases will not exceed 2×1032 \times 10^3.

输出格式

For each test case output one line containing (n+1)(n+1) integers a0,a1,,ana_0, a_1, \cdots, a_n separated by a space, where ama_m indicates the minimum total power needed to kill all the monsters if Kotori can use mm magic spells.

Please, DO NOT output extra spaces at the end of each line, otherwise your answer may be considered incorrect!

题目大意

给定一棵树和点权 hpihp_i,如果需要标记一个点 ii 的话,你会付出 hpihp_i 再加上所有 ii 的直接子节点的权值的代价,必须先标记 ii 的父节点才能标记 ii(根节点除外)。你可以使用魔法,每使用一次魔法,可以选择一个未被标记的点 xx 进行无代价标记(即在 xx 的父节点未被标记的时候)。求所有点都被标记掉的最少代价。

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