#P1612. [yLOI2018] 树上的链
[yLOI2018] 树上的链
Description
Given a tree with nodes. Each node has a weight and a parameter. For node , its weight is and its parameter is . Node is the root.
Now, for each node (), find the longest chain on the tree that satisfies the following conditions:
- .
- For , is the parent of .
- The sum of weights on the chain does not exceed , i.e., .
Input Format
The first line contains an integer , the number of nodes in the tree.
The second line contains integers , where denotes the parent of node .
The third line contains integers; the -th integer is the weight of node .
The fourth line contains integers; the -th integer is the parameter of node .
Output Format
Output one line with space-separated integers. The -th integer denotes the maximum length of the chain corresponding to node .
5
1 1 2 2
1 2 3 4 5
1 3 3 6 8
1 2 1 2 3
Hint
Constraints
For all testdata, it is guaranteed that ,,.
Translated by ChatGPT 5
京公网安备 11011102002149号