luogu#P5206. [WC2019] 数树
[WC2019] 数树
Background
The white rabbit likes trees.
The white cloud likes counting.
There are mice. The white rabbit uses blue ropes to connect them into a tree; each blue rope connects two mice. The white cloud uses red ropes to connect them into a tree; each red rope connects two mice.
The white cloud wants to assign a number to each mouse. This number can be any integer in .
The white rabbit gives the white cloud a requirement: for two mice , if there exists a path connecting these two mice that belongs to both trees, then and must be assigned the same integer.
“Having a path that belongs to both trees” means: there exists a sequence such that for all , and are directly connected by both a red rope and a blue rope.
The white cloud wants to know: how many different assignment schemes are there?
The mice keep struggling, trying to break free from the ropes. Before the white cloud can figure it out, the mice bite off all the red ropes.
The white rabbit is somewhat annoyed, but still wants to know the answer. So he asks the white cloud: over all possible connection schemes of the red ropes, what is the total sum of the answers (that is, sum the number of assignment schemes over all red-rope connection schemes)?
The mice keep struggling, trying to break free from the ropes. Before the white cloud can figure it out, the mice bite off the blue ropes as well.
The white rabbit is somewhat annoyed, but still wants to know the answer. So he asks the white cloud: over all possible connection schemes of both the red ropes and the blue ropes, what is the total sum of the answers (that is, sum the number of assignment schemes over all red-rope and blue-rope connection schemes)? Two schemes are different if and only if there exists at least one pair of mice such that the ropes directly connecting these two mice are different in the two schemes (there are 4 possibilities for ropes between two mice: no rope directly connects them, only a red rope directly connects them, only a blue rope directly connects them, and both colors of ropes directly connect them).
The white cloud cries.
Problem Description
This problem contains three questions:
- Question 0: The shapes of two trees with nodes are given (both trees have nodes labeled from to ). The first tree is the red tree, and the second tree is the blue tree. You need to assign each node an integer in such that for any two nodes , if there exists a path that belongs to both trees, then must be assigned the same number. Find the number of assignment schemes.
- The definition of “there exists a path that belongs to both trees” is the same as in the “Background”.
- Question 1: The blue tree is given. For all choices of the red tree, compute the sum of the answers to Question 0.
- Question 2: For all choices of the blue tree, compute the sum of the answers to Question 1.
Hint: There are different trees on nodes.
In different test points, you may need to answer different questions. We use to denote the index of the question you need to answer (corresponding to 0, 1, 2 above).
Since the answer may be very large, you only need to output it modulo .
Input Format
The first line contains three integers separated by spaces.
If , then the next lines follow: the first lines each describe a blue rope, and the next lines each describe a red rope.
If , then the next lines follow, each describing a blue rope.
If , then there is no further input.
Each rope description line contains two integers separated by a space, representing the indices of the two mice connected by this rope. The mice are numbered starting from .
Output Format
Output one integer, representing the answer modulo .
3 2 0
1 2
2 3
1 2
2 3
2
3 2 1
1 2
2 3
10
3 2 2
30
Hint
Sample Explanation 1
The two trees are the same, so any two nodes must be assigned the same number. The number of schemes is .
Sample Explanation 2
There are three possible red trees:
- Contains ropes (meaning the rope connecting mice and , similarly below), . Then any two mice must be assigned the same number, so the answer to Question 0 is .
- Contains ropes , . Then mice and must be assigned the same number, so the answer to Question 0 is .
- Contains ropes , . Then node and node must be assigned the same number, so the answer to Question 0 is .
Therefore, the answer to Question 1 is .
Sample Explanation 3
There are three possible blue trees. It is not hard to see that for each blue tree, the computed answer to Question 1 is . So the answer is .
::cute-table{tuack}
| Question Type | Test Point ID | Points per Test (CTT) | Points per Test (Non-CTT) | ||
|---|---|---|---|---|---|
| 0 | 1 | No special restriction | 2 | 18 | |
| ^ | 2 | ^ | ^ | 5 | |
| 3 | ^ | ^ | |||
| 1 | 4 | 1 | 4 | ||
| ^ | 5 | ^ | ^ | ||
| 6 | 6 | ||||
| 7 | ^ | ^ | |||
| 8 | |||||
| 9 | ^ | ||||
| 10 | 1 | ||||
| 11 | ^ | No special restriction | 5 | 2 | |
| 12 | ^ | ^ | |||
| 13 | |||||
| 14 | |||||
| 2 | 15 | 1 | 4 | ||
| ^ | 16 | ^ | ^ | ||
| 17 | 6 | ||||
| 18 | ^ | ^ | |||
| 19 | |||||
| 20 | ^ | ||||
| 21 | 1 | ||||
| 22 | ^ | No special restriction | 5 | 2 | |
| 23 | ^ | ||||
| 24 | |||||
| 25 | |||||
To optimize your reading experience, we placed the Test Point ID in the middle of the table; please pay attention to this.
All test points satisfy $3 \leq n \leq 10^{5}, 1 \leq y<998244353, o p \in\{0,1,2\}$.
Luogu scores according to CTT points
Translated by ChatGPT 5