#P10421. [蓝桥杯 2023 国 A] 树上的路径
[蓝桥杯 2023 国 A] 树上的路径
Problem Description
Given a tree with nodes, where the length of every edge is , find the sum of lengths of all paths in this tree whose lengths are between . Two paths are considered the same path if the sets of edges they pass through are exactly the same.
That is, compute $\sum\limits_{i=1}^n{\sum\limits_{j=i+1}^{n}{dis(i,j)\cdot[L \le dis(i,j) \le R]}}$, where denotes the distance between node and node , and equals if condition holds, otherwise .
Input Format
The first line contains three integers , separated by one space.
The next lines each contain one integer. On the -th line, the integer indicates the parent node of node in the tree. Node is the root and has no parent.
Output Format
Output one line containing one integer, which is the answer.
4 2 3
1
1
3
7
Hint
[Test Case Size and Conventions]
For of the testdata, .
For all testdata, , .
Translated by ChatGPT 5