#P5521. [yLOI2019] 梅深不见冬
[yLOI2019] 梅深不见冬
Description
Fu Su walks out of the deep winter Meiling and arrives at a rooted tree with 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 is the root of a tree , then for any node , the path from to the root is defined as the set of nodes passed through when starting from and reaching without visiting any node twice. It can be proven that such a set is unique.
A node is a child of node if and only if is connected to and is not on the path from to the root. If is a child of , then is the parent node of .
If I were a toxic genius like @_rqy, I might ask you how many labeled unrooted trees with nodes exist such that every node has at most children. Unfortunately I cannot solve that either, so I will not ask such a painful question.
Fu Su starts from node of this -node tree and walks along the edges. We define node as the root of the tree. His movement rule is: when Fu Su is at node , he either chooses a node among the children of that he has not visited and moves to , or he chooses to return to the parent node of .
Now each node has a weight , where the weight of node is . 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 if and only if the following conditions are satisfied:
Fu Su is currently at node .
For every child of , node has been placed with 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 plum blossoms on node , 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 , the number of nodes in the tree.
The second line contains integers separated by spaces. The -th integer denotes the index of the parent of node .
The third line contains integers separated by spaces. The -th integer denotes .
Output Format
Output one line with integers separated by spaces. The -th integer denotes the number of plum blossoms that need to be prepared to place plum blossoms on node .
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

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 , we move from node to node , then to node , place one plum blossom on node , return to node , place one plum blossom on node , and at the same time take back the plum blossom on node . Then return to node , and place the plum blossom taken back from node onto node . In total, we need two plum blossoms.
The plans for placing plum blossoms on nodes and are similar.
Explanation for Sample Input/Output 3

The input of Sample 3 is shown in the left figure.
First move from node to node , then to node , place plum blossoms on node , then return to node , place plum blossom on node , take back the plum blossoms on node , and return to node .
Then move to node , and move through node to node , place plum blossoms, return to node and place plum blossoms. At this moment, the number of plum blossoms on the tree is , located at nodes , , and . Then take back the plum blossoms on node , return to node , place plum blossoms, take back the blossoms on node , return to node , and place plum blossoms on node , thus achieving the goal of placing plum blossoms on node .
It can be verified that the minimum cost is . 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 | Test Point ID | ||
|---|---|---|---|
| 1 | 11 | ||
| 2 | 12 | ||
| 3 | 13 | ||
| 4 | 14 | ||
| 5 | 15 | ||
| 6 | 16 | ||
| 7 | 17 | ||
| 8 | 18 | ||
| 9 | 19 | ||
| 10 | 20 |
- For test points 5 and 6, a special property holds: each node has at most child nodes.
- For test points 8 to 10, a special property holds: each node has at most 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 , i.e. the height of the tree is at most .
- For 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 can help you quickly determine the special property of the test point.
Translated by ChatGPT 5
京公网安备 11011102002149号