#P8882. 成熟时追随原神
成熟时追随原神
Background
Klee likes living in trees.
![]()
Artist pid: 4787895.
Problem Description
Klee lives on a rooted tree, where the initial nodes are numbered from to . To make it easier for Klee to travel, the people of Mondstadt decide to build one new road starting from every non-leaf node. Specifically, for each non-leaf node , Mondstadt will uniformly randomly choose one node among its children, and build a new road between and . Obviously, these newly built roads form many connected components. To help with their construction, you need to tell Mondstadt what the expected number of connected components is.
After hearing about this task, Klee thinks it is too easy for you. Therefore, she decides to add some modification operations on the tree:
- : Add a child node under node , numbered , where is the operation index. It is guaranteed that node exists before the operation.
- : Delete node . It is guaranteed that node exists before the operation and is a leaf node.
- : Change the root of the tree to . It is guaranteed that node exists before the operation.
Meanwhile, at any time, it is guaranteed that the tree will not be deleted until empty.
For the initial tree and the tree obtained after each modification, you need to answer the above question once. Note that the modifications are not independent, but the new roads built by Mondstadt each time are not affected by the results of the previous time.
Input Format
The first line contains an integer , representing the size of the initial tree.
Lines to each contain an integer. The -th of these lines gives , the parent of node in the initial tree. Initially, node is the root of the tree.
Line contains an integer , representing the number of modification operations.
Lines to each contain one modification operation, in the form described in Description.
Output Format
Output a total of lines.
Line is the answer for the initial tree.
Lines to : line should output the answer after the -th modification operation.
All outputs are taken modulo . Specifically, if the answer is , you should output an such that .
2
1
4
Add 1
Upd 2
Del 3
Del 1
1
2
1
1
1
Hint
$$\begin{array}{|c|c|c|c|}\hline \textbf{Test Point ID}& { n\le} & {m\le} & \textbf{Special Property} \cr\hline 1\sim 3 & 5 & 5 & - \cr\hline 4\sim 7 & 1000 & 1000 &- \cr\hline 8\sim 10 & 10^5 & 0 & - \cr\hline 11\sim 13 & 10^5 & 2\times 10^5 & \textbf{AB}\cr\hline 14\sim 16 & 2\times 10^5 & 5\times 10^4 & \textbf{A} \cr\hline 17\sim 20 & 2\times 10^5 & 2\times 10^5 & - \cr\hline \end{array}$$- Special property : It is guaranteed that there is no operation.
- Special property : It is guaranteed that there is no operation.
Constraints: for of the testdata, . It is guaranteed that .
Translated by ChatGPT 5