#P17438. 「LWLV Round 1」天气先生

    ID: 19948 远端评测题 1000ms 250MiB 尝试: 0 已通过: 0 显示难度提高 上传者: 标签>数学树形 DP排列组合期望

「LWLV Round 1」天气先生

Background

Maybe the sun won't shine
Maybe the rain won't fall
But maybe our love will find a way
And rise above it all
Maybe there comes a day
When all of the skies turn grey
Maybe today our love can fly high
You and I will soar

Problem Description

There is a tree TT with nn nodes, where the root is 11. Each node has a node weight wiw_i which is 0/10/1.

We define the weather value of some DFS order ss of this tree TT as:

$$\left|w_{s_1}-w_{s_2}\right|+\left|w_{s_2}-w_{s_3}\right|+\cdots+\left|w_{s_{n-1}}-w_{s_n}\right|$$

Please find the sum of the weather values over all DFS orders of the tree TT.

:::info[DFS order] For a tree rooted at 11, its DFS order refers to a sequence of length nn formed by the order in which nodes are visited (entered) when traversing the whole tree starting from the root, following the rules of depth-first search (DFS).

In a standard DFS process, the following principles are followed:

  • Start from the root node. Each time a node is visited, immediately recursively visit one of its unvisited children.
  • Only after all children of the current node have been visited will the traversal backtrack to its parent.
  • During the whole traversal, each node is visited (recorded into the sequence) exactly once. :::

:::info[All DFS orders] In a typical DFS algorithm, when a node has multiple children, the algorithm may choose any child as the next one to visit. This freedom in choosing the visiting order of children means that the same tree may generate many different DFS orders.

“All DFS orders” means: during the traversal of this tree, enumerate all permutations of the children of each node as the visiting order, and take the set of all valid DFS traversal sequences generated in this way. :::

Output the answer modulo 998,244,353998,244,353.

Input Format

The first line contains a positive integer nn, the number of nodes.
The second line contains nn integers wiw_i, the node weights.
The next n−1n-1 lines each contain two positive integers u,vu,v, indicating that there is an edge connecting uu and vv.

Output Format

One line, the result of the answer modulo 998,244,353998,244,353.

4
0 1 0 1
1 2
1 3
3 4
4

Hint

Sample Explanation

There are two DFS orders: [1,2,3,4],[1,3,4,2][1,2,3,4],[1,3,4,2]. They correspond to node-weight sequences [0,1,0,1],[0,0,1,1][0,1,0,1],[0,0,1,1], and the answer is 44.

Constraints

Subtask ID n≤n\le Special Property Score
00 1010 None 1515
11 10510^5 w1=1,wi=0(i≠1)w_1=1,w_i=0(i\neq 1) 1010
22 ^ All leaves have the same weight 3030
33 None 4545

For 100%100\% of the testdata, it is guaranteed that 1≤u,v≤n≤105,wi∈{0,1}1\le u,v\le n\le 10^5,w_i\in\{0,1\}。

Translated by ChatGPT 5