#P15649. [省选联考 2026] 找寻者

    ID: 17712 远端评测题 1500ms 1024MiB 尝试: 0 已通过: 0 显示难度提高+/省选− 上传者: 标签>各省省选多项式O2优化背包 DP树形 DP期望逆元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 nn nodes, numbered from 11 to nn.

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 11.

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 11 as the root.
  • For each non-leaf node, choose its heavy child bottom-up: for a non-leaf node uu (1≤u≤n1 \le u \le n), suppose it has kk children v1,v2,…,vkv_1, v_2, \dots, v_k. After the heavy child choices inside all child subtrees have been determined recursively, let the lengths of the heavy chains that contain v1,v2,…,vkv_1, v_2, \dots, v_k be l1,l2,…,lkl_1, l_2, \dots, l_k, respectively. Then uu chooses proportionally to these lengths: the probability that uu chooses viv_i (1≤i≤k1 \le i \le k) as the heavy child is li∑j=1klj\frac{l_i}{\sum_{j=1}^{k} l_j}.

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 xx (1≤x≤n1 \le x \le n), the sum of expectations of the number of light edges on the simple path from node xx to the root node 11. Since the answer may be large, you only need to output it modulo 998244353998244353.

The expectation is defined as follows: suppose a random variable XX can take values x1,…,xmx_1, \dots, x_m, where Pr⁡[X=xi]=pi∈[0,1]\Pr[X = x_i] = p_i \in [0,1] and ∑i=1mpi=1\sum_{i=1}^{m} p_i = 1. Then the expectation of XX is

E[X]=∑i=1mpixi.\mathbb{E}[X] = \sum_{i=1}^{m} p_i x_i.

Input Format

This problem contains multiple test cases.

The first line contains two non-negative integers c,tc, t, representing the test point ID and the number of test cases. c=0c = 0 means this test point is the sample.

Then each test case is given as follows:

  • The first line contains a positive integer nn, the number of nodes.
  • Line i+1i+1 (1≤i≤n−11 \le i \le n-1) contains two positive integers ui,viu_i, v_i, denoting an edge between nodes uiu_i and viv_i.

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 998244353998244353.

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 22 chooses node 44 or node 55 as its heavy child with equal probability.
  • Node 11 chooses node 22 or node 33 as its heavy child with probabilities 2/32/3 and 1/31/3, respectively.

Therefore:

  • The expected number of light edges on the simple path from node 11 to the root is 00.
  • The expected number of light edges on the simple path from node 22 to the root is (2/3)⋅0+(1/3)⋅1=1/3(2/3) \cdot 0 + (1/3) \cdot 1 = 1/3.
  • The expected number of light edges on the simple path from node 33 to the root is (2/3)⋅1+(1/3)⋅0=2/3(2/3) \cdot 1 + (1/3) \cdot 0 = 2/3.
  • The expected number of light edges on the simple path from node 44 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 55 to the root is 5/65/6.

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 3∼53 \sim 5.

Sample 3.

See recollector/recollector3.in and recollector/recollector3.ans in the contestant directory.

This sample satisfies the constraints of test points 6,76, 7.

Sample 4.

See recollector/recollector4.in and recollector/recollector4.ans in the contestant directory.

This sample satisfies the constraints of test points 8∼108 \sim 10.

Sample 5.

See recollector/recollector5.in and recollector/recollector5.ans in the contestant directory.

This sample satisfies the constraints of test points 11,1211, 12.

Sample 6.

See recollector/recollector6.in and recollector/recollector6.ans in the contestant directory.

This sample satisfies the constraints of test points 13∼1613 \sim 16.

Sample 7.

See recollector/recollector7.in and recollector/recollector7.ans in the contestant directory.

This sample satisfies the constraints of test points 17∼2517 \sim 25.

Constraints

For all testdata:

  • 1≤t≤51 \le t \le 5;
  • 1≤n≤5,0001 \le n \le 5,000;
  • For all 1≤i≤n−11 \le i \le n-1, 1≤ui,vi≤n1 \le u_i, v_i \le n, and (u1,v1),…,(un−1,vn−1)(u_1, v_1), \dots, (u_{n-1}, v_{n-1}) form a tree.

::cute-table{tuack}

Test point ID n≤n \le Special property
1,21, 2 88 None
3∼53 \sim 5 2020 ^
6,76, 7 500500 A
8∼108 \sim 10 ^ None
11,1211, 12 1,5001,500 B
13∼1613 \sim 16 ^ None
17∼2517 \sim 25 5,0005,000 ^
  • Special property A: for all 1≤i≤n−11 \le i \le n-1, ui=iu_i = i and vi=i+1v_i = i+1.
  • Special property B: for all 1≤x≤n1 \le x \le n, the number of nodes on the simple path from node 11 to node xx is at most 100100.

Translated by ChatGPT 5