#P11358. [eJOI 2023] Tree Infection

[eJOI 2023] Tree Infection

Problem Description

You are given a rooted tree TT with NN nodes, where the root is guaranteed to be node 11.

A node ss on the tree will be chosen. All nodes whose distance to ss is not greater than RR will be infected, where the distance between two nodes is defined as the number of edges on the path between them in the tree.

A pair of nodes is reachable if and only if both nodes are not infected, and the number of infected nodes on the path between them is not greater than MM.

For s=1,2,,Ns=1,2,\cdots,N, compute the number of reachable pairs of nodes when choosing ss.

Input Format

The first line contains three integers N,R,MN,R,M.

The second line contains N1N-1 integers p2,p3,,pNp_2,p_3,\cdots,p_N, representing the parent of each node.

Output Format

Output NN lines, each containing one integer. The integer on line ss represents the answer when choosing ss.

13 2 2
1 2 3 4 3 6 6 8 2 10 11 1
16
4
15
55
66
36
66
55
66
45
55
66
66
3 0 1
1 2
1
1
1

Hint

[Sample Explanation]

The tree for s=2s=2 is shown in the figure.

All reachable pairs are (1,13)(1,13), (7,8)(7,8), (7,9)(7,9), and (8,9)(8,9).

Note that (1,2)(1,2) is not reachable because 22 is already infected; (1,5)(1,5) is not reachable because there are three infected nodes 2,3,42,3,4 on the path.

[Constraints]

This problem uses bundled tests.

  • Subtask 1 (20 pts): N300N\leq 300.
  • Subtask 2 (14 pts): R=0R=0.
  • Subtask 3 (15 pts): M=2R+1M=2R+1.
  • Subtask 4 (10 pts): M=2R1M=2R-1.
  • Subtask 5 (16 pts): N5×103N\leq 5\times 10^3.
  • Subtask 6 (25 pts): No special constraints.

For 100%100\% of the testdata, it is guaranteed that 2N5×1052\leq N\leq 5\times 10^5, 1pi<i1\leq p_i<i, 0RN10\leq R\leq N-1, 0M2R+10\leq M\leq 2R+1.

Translated by ChatGPT 5