#P8334. [ZJOI2022] 深搜

    ID: 9401 远端评测题 5000ms 1024MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>动态规划 DP线段树各省省选2022浙江O2优化树链剖分矩阵乘法

[ZJOI2022] 深搜

Problem Description

Kanjou Karen is a girl who likes algorithms. Among many algorithms, she especially likes depth-first search (DFS).

One day, Karen obtained a rooted tree with root root\mathit{root}. Each node xx on the tree has a weight axa_x.

On a tree, starting from xx to search for node yy, if we use depth-first search, it can be described by the following procedure:

  1. Set the recursion stack to be empty.
  2. First, push node xx into the recursion stack.
  3. Pop the top node from the recursion stack. If this node is yy, then stop the procedure. Otherwise, if there exists an unvisited direct child, then uniformly at random choose one child and push it into the recursion stack.
  4. Repeat step 3 until there is no unvisited direct child.
  5. Push the parent node into the recursion stack and repeat step 3.
  6. Repeat step 5 until the current node is xx, and the procedure ends.

We define f(x,y)f(x, y) to be valid if and only if yy is in the subtree of xx. Its value is the expected minimum weight among all nodes visited (including xx and yy) during the DFS on the subtree of xx starting from xx while searching for yy.

Karen wants to know, for all valid pairs (x,y)(x, y), the value of f(x,y)\sum f(x, y). You only need to output the result modulo 998244353998244353. Specifically, if the answer in lowest terms is ab\frac{a}{b}, output a×b1mod998244353a \times b^{-1} \bmod 998244353.

Input Format

The input contains multiple test cases. The first line contains an integer TT, the number of test cases.

For each test case, the first line contains two integers n,rootn, \mathit{root}, representing the size of the tree and the index of the root.

The next line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n, representing the weight of each node.

The next n1n - 1 lines each contain two integers u,vu, v, indicating that there is an edge between uu and vv.

Output Format

For each test case, output one line containing one integer, representing f(x,y)\sum f(x, y) over all valid pairs (x,y)(x, y) modulo 998244353998244353.

4
1 1
1
3 3
3 3 4
3 1
3 2
6 1
5 2 4 1 3 6
1 2
1 6
2 3
2 4
4 5
5 1
5 4 3 2 1
1 2
1 3
3 4
3 5

1
16
34
499122202

见附件中的 dfs/dfs_ex2.in
见附件中的 dfs/dfs_ex2.ans

Hint

For all test points, it holds that 1T1001 \le T \le 100, n8×105\sum n \le 8 \times {10}^5, 1n4×1051 \le n \le 4 \times {10}^5, 1root,u,vn1 \le \mathit{root}, u, v \le n, 1ai1091 \le a_i \le {10}^9.

The detailed limits for each test point are shown in the table below:

Test Point ID n\sum n \le nn \le Special Constraints
11 5050 1010 None
242 \sim 4 4000040000 50005000
5105 \sim 10 4×1054 \times {10}^5 105{10}^5
1111 8×1058 \times {10}^5 4×1054 \times {10}^5 The tree generation is random
1212 The tree is a chain
1313 The degree of the root is n1n - 1
142014 \sim 20 None

For test point 1111, the tree is generated as follows: take 11 as the root. For each node i[2,n]i \in [2, n], uniformly at random choose a node from [1,i1][1, i - 1] as its parent. Then the labels are randomly permuted.

Translated by ChatGPT 5