#HK4015. 「eJOI2023」Tree Infection

「eJOI2023」Tree Infection

Description

You are given a rooted tree consisting of NN vertices, along with integers RR and MM. The vertices are numbered from 11 to NN, with vertex 11 as a root. Each of the other vertices has a single parent in the tree.

If a vertex ss is chosen, it becomes infected along with all its descendants (i.e. vertices that can be reached by following edges downward from ss) at a distance of RR or less, where distance is calculated as the number of edges between vertices. A vertex uu is considered reachable from vertex vv if and only if neither of them is infected, and the number of infected vertices on the path between them does not exceed MM.

For each possible chosen vertex ss (1sN1 \leq s \leq N), you must calculate the number of vertex pairs (u,v)(u, v) such that 1u<vN1 \leq u < v \leq N and uu is reachable from vv (and vice versa).

Input Format

The first line contains three integers: NN, RR and MM.

The second line contains N1N - 1 integers: p[2],p[3],,p[N]p[2], p[3], \ldots ,p[N], the parents of the vertices 2,3,,N2, 3, \ldots ,N, respectively.

Output Format

Print NN lines with single integer each: ss-th line should contain required number of pairs when the chosen vertex is ss.

It's not recommended to use std::endl for outputting newline symbols. Instead, consider using '\n' for better performance.

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

The image above corresponds to s=2s = 2.

The reachable pairs are: (1,13),(7,8),(7,9),(8,9)(1,13), (7,8), (7,9), (8,9).

This list doesn't include the pair (1,2)(1,2) since vertex 22 is infected. Similarly, the pair (1,5)(1,5) is absent since the path between 11 and 55 has three infected vertices (22, 33 and 44).

3 0 1
1 2
1
1
1

Constraints

  • 2N5000002 \leq N \leq 500\,000
  • 1p[i]<i1 \leq p[i] < i (for each 2iN2 \leq i \leq N)
  • 0RN10 \leq R \leq N - 1
  • 0M2×R+10 \leq M \leq 2 \times R + 1

Subtasks

  1. (20 points) N300N \leq 300
  2. (14 points) R=0R = 0
  3. (15 points) M=2×R+1M = 2 \times R + 1
  4. (10 points) M=2×R1M = 2 \times R - 1
  5. (16 points) N5000N \leq 5\,000
  6. (25 points) No additional constraints.