#P5521. [yLOI2019] 梅深不见冬

    ID: 4460 远端评测题 1000ms 500MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>贪心2019O2优化排序深度优先搜索,DFS

[yLOI2019] 梅深不见冬

Description

Fu Su walks out of the deep winter Meiling and arrives at a rooted tree with nn nodes.

If you do not know what a tree is, you may think of it as a simple undirected connected graph whose number of edges is exactly one less than the number of nodes.

If we specify that xx is the root of a tree TT, then for any node yy, the path from yy to the root is defined as the set of nodes passed through when starting from yy and reaching xx without visiting any node twice. It can be proven that such a set is unique.

A node uu is a child of node vv if and only if uu is connected to vv and uu is not on the path from vv to the root. If uu is a child of vv, then vv is the parent node of uu.

If I were a toxic genius like @_rqy, I might ask you how many labeled unrooted trees with nn nodes exist such that every node has at most kk children. Unfortunately I cannot solve that either, so I will not ask such a painful question.

Fu Su starts from node 11 of this nn-node tree and walks along the edges. We define node 11 as the root of the tree. His movement rule is: when Fu Su is at node uu, he either chooses a node vv among the children of uu that he has not visited and moves to vv, or he chooses to return to the parent node of uu.

Now each node has a weight ww, where the weight of node ii is wiw_i. He wants to place plum blossoms brought from Meiling onto some node of the tree. We say Fu Su can place plum blossoms on node uu if and only if the following conditions are satisfied:

Fu Su is currently at node uu.

For every child vv of uu, node vv has been placed with wvw_v plum blossoms.

Meanwhile, Fu Su can take back the plum blossoms on any node at any time, and he does not need to walk to that node when taking them back.

Now Fu Su asks you: for each node, if he wants to place wiw_i plum blossoms on node ii, what is the minimum number of plum blossoms he needs to bring out from Meiling?

Input Format

Each input file contains one and only one set of testdata.

The first line contains an integer nn, the number of nodes in the tree.

The second line contains n1n - 1 integers separated by spaces. The ii-th integer pip_i denotes the index of the parent of node i+1i + 1.

The third line contains nn integers separated by spaces. The ii-th integer denotes wiw_i.

Output Format

Output one line with nn integers separated by spaces. The ii-th integer denotes the number of plum blossoms that need to be prepared to place wiw_i plum blossoms on node ii.

3 
1 2 
1 1 1
2 2 1
3
1 1
1 1 1
3 1 1
6
1 1 2 3 4
3 14 1 5 12 15
21 20 13 20 12 15

Hint

Explanation for Sample Input/Output 1

qwq

The input of Sample 1 is shown in the figure above. Every node needs to be placed with one plum blossom.

If we place plum blossoms on node 11, we move from node 11 to node 22, then to node 33, place one plum blossom on node 33, return to node 22, place one plum blossom on node 22, and at the same time take back the plum blossom on node 33. Then return to node 11, and place the plum blossom taken back from node 33 onto node 11. In total, we need two plum blossoms.

The plans for placing plum blossoms on nodes 22 and 33 are similar.

Explanation for Sample Input/Output 3

qwq

The input of Sample 3 is shown in the left figure.

First move from node 11 to node 33, then to node 55, place 1212 plum blossoms on node 55, then return to node 33, place 11 plum blossom on node 33, take back the 1212 plum blossoms on node 55, and return to node 11.

Then move to node 22, and move through node 44 to node 66, place 1515 plum blossoms, return to node 44 and place 55 plum blossoms. At this moment, the number of plum blossoms on the tree is 5+15+1=215 + 15 + 1 = 21, located at nodes 44, 66, and 33. Then take back the plum blossoms on node 66, return to node 22, place 1414 plum blossoms, take back the blossoms on node 44, return to node 11, and place 33 plum blossoms on node 11, thus achieving the goal of placing plum blossoms on node 11.

It can be verified that the minimum cost is 2121. The answers for other nodes are similar.

Please note that the answers for other nodes are not necessarily obtained by walking along the movement path of that node.


Constraints and Agreements

Test Point ID n=n = Test Point ID n=n =
1 11 11 100003100003
2 88 12
3 13
4 14
5 15 100004100004
6 100000100000 16
7 17
8 100002100002 18
9 19
10 20
  • For test points 5 and 6, a special property holds: each node has at most 22 child nodes.
  • For test points 8 to 10, a special property holds: each node has at most 55 child nodes.
  • For test points 11 to 14, a special property holds: the number of nodes on the path from any node to the root is at most 33, i.e. the height of the tree is at most 33.
  • For 100%100\% of the data, it is guaranteed that $1 \leq n \leq 10^5 + 4,~1 \leq p_i \leq i,~1 \leq w_i \leq 1000$.

Hint

  • The last digit of nn can help you quickly determine the special property of the test point.

Translated by ChatGPT 5