#HK4015. 「eJOI2023」Tree Infection
「eJOI2023」Tree Infection
Description
You are given a rooted tree consisting of vertices, along with integers and . The vertices are numbered from to , with vertex as a root. Each of the other vertices has a single parent in the tree.
If a vertex is chosen, it becomes infected along with all its descendants (i.e. vertices that can be reached by following edges downward from ) at a distance of or less, where distance is calculated as the number of edges between vertices. A vertex is considered reachable from vertex if and only if neither of them is infected, and the number of infected vertices on the path between them does not exceed .
For each possible chosen vertex (), you must calculate the number of vertex pairs such that and is reachable from (and vice versa).
Input Format
The first line contains three integers: , and .
The second line contains integers: , the parents of the vertices , respectively.
Output Format
Print lines with single integer each: -th line should contain required number of pairs when the chosen vertex is .
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 .
The reachable pairs are: .
This list doesn't include the pair since vertex is infected. Similarly, the pair is absent since the path between and has three infected vertices (, and ).
3 0 1
1 2
1
1
1
Constraints
- (for each )
Subtasks
- (20 points)
- (14 points)
- (15 points)
- (10 points)
- (16 points)
- (25 points) No additional constraints.