#P5689. [CSP-S 2019 江西] 多叉堆
[CSP-S 2019 江西] 多叉堆
Description
A d-ary heap is a tree-structured data structure. In this problem, we only consider the min-heap, which satisfies that for every node except the root, the weight of the node is not less than the weight of its parent. Except for leaf nodes, every node has at least one child.
Initially, there are nodes, numbered . Each node is a single-node tree rooted at itself. Then there are operations in order, and each operation is one of the following two types:
-
1 x y: Choose nodes and that are not in the same tree. Attach the root of the tree containing directly under the root of the tree containing . Then the trees containing and merge into one tree. -
2 x: Choose a node , and let the number of nodes in the tree containing be . You need to compute how many different d-ary heaps can be produced by filling the numbers into the nodes of the tree containing . Two heaps are different if and only if there exists a node whose filled value is different. Since the answer may be very large, you only need to output the result modulo .
Input Format
The first line contains two integers , whose meanings are described above.
In the next lines, each line is in one of the following formats:
-
1 x' y': You need to compute to get the actual and . The input guarantees that the trees containing and are different. -
2 x': You need to compute to get the actual .
Here, denotes the output of the previous type operation, and initially .
Output Format
For each type operation, output one line with one integer representing the answer.
5 6
1 1 0
1 2 0
2 1
1 3 1
1 2 0
2 4
2
8
Hint
Sample 1 Explanation
In the 1st operation, attach the root of the tree containing under the root of the tree containing .
In the 2nd operation, attach the root of the tree containing under the root of the tree containing .
In the 3rd operation, the tree containing is shown in Figure 1. Filling with and can produce different heaps.
In the 4th operation, , . Attach the root of the tree containing under the root of the tree containing .
In the 5th operation, , . Attach the root of the tree containing under the root of the tree containing .
In the 6th operation, . The tree containing is shown in Figure 2. Filling with , , , , , , , can produce different heaps.

Constraints and Conventions
For of the data, , .
For different test points, we define:
::cute-table{tuack}
| Test Point ID | Special Property | Special Property | ||
|---|---|---|---|---|
| Yes | No | |||
| ^ | No | Yes | ||
| ^ | No | |||
| ^ | ||||
| Yes | ||||
| ^ | No | Yes | ||
| ^ | No | |||
| Yes | ^ | |||
| ^ | No | Yes | ||
| ^ | No | |||
| Yes | ^ | |||
| ^ | No | Yes | ||
| ^ | No | |||
| ^ | ||||
| Yes | ||||
| ^ | No | Yes | ||
| ^ | No | |||
| ^ | ||||
Special Property : There exists , such that the first operations are all type operations, and all the remaining operations are type operations.
Special Property : For all inputs, and themselves are the roots of their respective trees.
Thanks to @Fairicle for providing the testdata.
Translated by ChatGPT 5
京公网安备 11011102002149号