#P1612. [yLOI2018] 树上的链

[yLOI2018] 树上的链

Description

Given a tree with nn nodes. Each node has a weight and a parameter. For node ii, its weight is wiw_i and its parameter is cic_i. Node 11 is the root.

Now, for each node uu (1un1 \leq u \leq n), find the longest chain v1,v2,vmv_1, v_2, \dots v_m on the tree that satisfies the following conditions:

  1. v1=uv_1 = u.
  2. For 2im2 \leq i \leq m, viv_i is the parent of vi1v_{i - 1}.
  3. The sum of weights on the chain does not exceed cuc_u, i.e., j=1mwvjcu\sum_{j = 1}^m w_{v_j} \leq c_u.

Input Format

The first line contains an integer nn, the number of nodes in the tree.
The second line contains n1n - 1 integers p2,p3,pnp_2, p_3, \dots p_n, where pip_i denotes the parent of node ii.
The third line contains nn integers; the ii-th integer is the weight wiw_i of node ii.
The fourth line contains nn integers; the ii-th integer is the parameter cic_i of node ii.

Output Format

Output one line with nn space-separated integers. The ii-th integer denotes the maximum length of the chain corresponding to node ii.

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 1u,vn1051 \leq u, v \leq n \leq 10^51pi<i1 \leq p_i \lt i1wici1091 \leq w_i \leq c_i \leq 10^9.

Translated by ChatGPT 5