#P6048. 最优性剪枝
最优性剪枝
Description
Nauuo decides to break a brute-force search program, so she constructs a set of testdata. To simplify the problem, you are given the search tree generated by this testdata. contains nodes, numbered in order, where node is the root of . The depth of a node is the number of nodes on the simple path from it to node .
The pseudocode of this program is as follows.
answer := inf
procedure dfs(node,depth)
if (node is leaf)
answer := min(answer,depth)
return
if (depth < answer)
for i in children of node
dfs(i,depth+1)
dfs(1,1)
Here, := denotes assignment.
In plain words, this brute-force search program traverses the search tree in depth-first order. When it reaches a leaf node, it updates the answer using the depth of that leaf node.
Meanwhile, this program has an optimality pruning. That is, when the program reaches any node whose depth equals the current answer, it will not visit this node’s children.
However, poor Nauuo does not know the order in which the program visits a node’s children. Therefore, she assumes that for each node, the order of visiting its children is uniformly random among all possible orders. Clearly, there are possible cases in total, where is the number of children of node .
Now she wants to know the expected number of nodes visited by this program, to determine whether the program will be broken by her testdata.
To avoid floating-point errors, take the answer modulo . The answer is guaranteed to be representable as an irreducible fraction . You only need to output an such that .
Input Format
The first line contains an integer .
The second line contains integers , where is the index of the parent of node .
Output Format
One line with an integer, the required .
4
1 1 3
499122180
3
1 2
3
13
1 1 1 3 5 4 2 3 7 4 4 6
776412285
Hint
Sample Explanation
For the first sample, the true answer is .
There are only two cases in total. If node traverses node first, then the program will visit all nodes in the search tree. If node traverses node first, then node will not be visited.
In the second sample, each non-leaf node has only one child, so there is only one possible case, and all nodes will definitely be visited.
For the third sample, the true answer is .
Constraints
This problem uses bundled test cases.
For all test points, it is guaranteed that , .
.
.
.
.
.
No special constraints.
Hint
If you do not know how to take a fraction modulo a number, you can refer to here.
Translated by ChatGPT 5
京公网安备 11011102002149号