#P15649. [省选联考 2026] 找寻者
[省选联考 2026] 找寻者
Background
As time goes by, Xiao B returned to the NOI Qualifier arena that he had long dreamed of, yet once failed at. But how much does he still remember about competitive programming? And among those memories, which ones are the most precious and worth cherishing?
Xiao B is passionate about competitive programming and loves exploring. To him, the most precious memories are probably the days when he learned algorithms by making all kinds of changes, running experiments, and trying to achieve something new.
Xiao B wants you to accompany him to search for these precious memories.
2026/3/13: Four groups of hack testdata were added.
Problem Description
You are given an undirected tree with nodes, numbered from to .
Define a heavy-light decomposition scheme as follows:
- First, choose a node as the root, obtaining a rooted tree.
- For every non-leaf node in the tree, choose exactly one child as its heavy child, and classify the edge connecting the node and its heavy child as a heavy edge; edges to its other children are light edges.
- Then all heavy edges and their endpoints form several maximal simple paths. The nodes on each such path form a heavy chain. The length of a heavy chain is the number of nodes it contains. In particular, a node that is not incident to any heavy edge alone forms a heavy chain of length .
Xiao B recalls that when he learned heavy-light decomposition years ago, he proposed a random chain decomposition algorithm, with the following process:
- First, set node as the root.
- For each non-leaf node, choose its heavy child bottom-up: for a non-leaf node (), suppose it has children . After the heavy child choices inside all child subtrees have been determined recursively, let the lengths of the heavy chains that contain be , respectively. Then chooses proportionally to these lengths: the probability that chooses () as the heavy child is .
Xiao B knows that the time complexity of heavy-light decomposition is closely related to the number of light edges on the simple path from each node to the root. You need to help him compute, under the random chain decomposition algorithm above, for each node (), the sum of expectations of the number of light edges on the simple path from node to the root node . Since the answer may be large, you only need to output it modulo .
The expectation is defined as follows: suppose a random variable can take values , where and . Then the expectation of is
Input Format
This problem contains multiple test cases.
The first line contains two non-negative integers , representing the test point ID and the number of test cases. means this test point is the sample.
Then each test case is given as follows:
- The first line contains a positive integer , the number of nodes.
- Line () contains two positive integers , denoting an edge between nodes and .
Output Format
For each test case, output one line with a non-negative integer: the sum, over all nodes, of the expected number of light edges on the simple path to the root, taken modulo .
0 2
5
1 2
1 3
2 4
2 5
8
1 2
1 3
2 4
2 5
2 6
5 7
3 8
665496238
549034400
Hint
Sample 1 Explanation.
This sample contains two test cases. For the first test case:
- Node chooses node or node as its heavy child with equal probability.
- Node chooses node or node as its heavy child with probabilities and , respectively.
Therefore:
- The expected number of light edges on the simple path from node to the root is .
- The expected number of light edges on the simple path from node to the root is .
- The expected number of light edges on the simple path from node to the root is .
- The expected number of light edges on the simple path from node to the root is $(2/3) \cdot (1/2) \cdot 0 + (2/3) \cdot (1/2) \cdot 1 + (1/3) \cdot (1/2) \cdot 1 + (1/3) \cdot (1/2) \cdot 2 = 5/6$.
- The expected number of light edges on the simple path from node to the root is .
So the answer is $0 + 1/3 + 2/3 + 5/6 + 5/6 = 8/3 \equiv 665496238 \pmod{998244353}$.
Sample 2.
See recollector/recollector2.in and recollector/recollector2.ans in the contestant directory.
This sample satisfies the constraints of test points .
Sample 3.
See recollector/recollector3.in and recollector/recollector3.ans in the contestant directory.
This sample satisfies the constraints of test points .
Sample 4.
See recollector/recollector4.in and recollector/recollector4.ans in the contestant directory.
This sample satisfies the constraints of test points .
Sample 5.
See recollector/recollector5.in and recollector/recollector5.ans in the contestant directory.
This sample satisfies the constraints of test points .
Sample 6.
See recollector/recollector6.in and recollector/recollector6.ans in the contestant directory.
This sample satisfies the constraints of test points .
Sample 7.
See recollector/recollector7.in and recollector/recollector7.ans in the contestant directory.
This sample satisfies the constraints of test points .
Constraints
For all testdata:
- ;
- ;
- For all , , and form a tree.
::cute-table{tuack}
| Test point ID | Special property | |
|---|---|---|
| None | ||
| ^ | ||
| A | ||
| ^ | None | |
| B | ||
| ^ | None | |
| ^ |
- Special property A: for all , and .
- Special property B: for all , the number of nodes on the simple path from node to node is at most .
Translated by ChatGPT 5