#P11358. [eJOI 2023] Tree Infection
[eJOI 2023] Tree Infection
Problem Description
You are given a rooted tree with nodes, where the root is guaranteed to be node .
A node on the tree will be chosen. All nodes whose distance to is not greater than 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 .
For , compute the number of reachable pairs of nodes when choosing .
Input Format
The first line contains three integers .
The second line contains integers , representing the parent of each node.
Output Format
Output lines, each containing one integer. The integer on line represents the answer when choosing .
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 is shown in the figure.
All reachable pairs are , , , and .
Note that is not reachable because is already infected; is not reachable because there are three infected nodes on the path.
[Constraints]
This problem uses bundled tests.
- Subtask 1 (20 pts): .
- Subtask 2 (14 pts): .
- Subtask 3 (15 pts): .
- Subtask 4 (10 pts): .
- Subtask 5 (16 pts): .
- Subtask 6 (25 pts): No special constraints.
For of the testdata, it is guaranteed that , , , .
Translated by ChatGPT 5