#P7574. 「PMOI-3」子树
「PMOI-3」子树
Description
b6e0 has a tree. The -th node has value . Each edge has length .
b6e0 will choose a node as the root, denoted by . Then, b6e0 will select the entire subtree of some node as his territory, and let the root of this subtree be . At this time, every node in the tree will bring b6e0 some profit. When the root of the territory subtree is , the profit brought by node , denoted by , is defined as follows:
- If is in the subtree of , then its profit equals the profit of its parent node plus its own value .
- If is not in the subtree of , then its profit is: among the nodes adjacent to it, take those whose distance to (the length of the simple path to ) is greater than the distance from to ; take the maximum of these nodes' profits modulo , and then multiply it by .
With root , define the profit of taking as the subtree, , as the sum of over all nodes.
Of course, b6e0 has many ways to choose the root. Define the profit when choosing as the root, , as the sum over all of the subtree profit . For each node , compute .
Formal statement:
You are given a tree with nodes. The -th node has weight , and each edge has length . When the root is :
Let denote the parent of . In particular, . Let denote the length of the simple path from to . In particular, for all , . Let denote the set of nodes in the subtree of (including itself), i.e. . In particular, . Let denote the set of nodes adjacent to , i.e. .
Define :
$$f(x,u)=\begin{cases}f(F(x),u)+a_x&x\in A_u\\a_x\cdot\max_{y\in B_x,D(y,u)>D(x,u)}\{f(y,u)\bmod 998244353\}&x\not\in A_u\end{cases}$$In particular, for all , . In the case , if for all we have , then .
Define the score of node as .
Define the profit of node , , as the value of when the root is .
For each node , compute .
All are taken modulo .
Input Format
The first line contains a positive integer , the number of nodes in the tree.
The second line contains integers. The -th integer denotes the node weight of node .
The next lines each contain two positive integers , indicating that there is an edge between node and node .
Output Format
Output lines. The -th line outputs a positive integer, the value of node 's profit modulo .
6
7 2 5 100 1 5
1 3
3 4
1 2
4 5
4 6
67562
29930
75168
76888
63243
63283
Hint
[Sample Explanation]
The tree in the sample is shown in the figure:

For example, when and , , , , , , and .
[Constraints]
- Subtask 1 (10 pts): , .
- Subtask 2 (20 pts): .
- Subtask 3 (20 pts): the tree is a chain.
- Subtask 4 (20 pts): there exists a node whose degree is .
- Subtask 5 (30 pts): no special restrictions.
For of the testdata, , .
Translated by ChatGPT 5
京公网安备 11011102002149号