#P7574. 「PMOI-3」子树

「PMOI-3」子树

Description

b6e0 has a tree. The ii-th node has value aia_i. Each edge has length 11.

b6e0 will choose a node as the root, denoted by rr. Then, b6e0 will select the entire subtree of some node as his territory, and let the root of this subtree be uu. At this time, every node in the tree will bring b6e0 some profit. When the root of the territory subtree is uu, the profit brought by node xx, denoted by f(x,u)f(x,u), is defined as follows:

  1. If xx is in the subtree of uu, then its profit equals the profit of its parent node plus its own value axa_x.
  2. If xx is not in the subtree of uu, then its profit is: among the nodes adjacent to it, take those whose distance to uu (the length of the simple path to uu) is greater than the distance from xx to uu; take the maximum of these nodes' profits modulo 998244353998244353, and then multiply it by axa_x.

With root rr, define the profit of taking uu as the subtree, W(u)W(u), as the sum of ff over all nodes.

Of course, b6e0 has many ways to choose the root. Define the profit when choosing rr as the root, C(r)C(r), as the sum over all uu of the subtree profit W(u)W(u). For each node rr, compute C(r)C(r).


Formal statement:

You are given a tree with nn nodes. The ii-th node has weight aia_i, and each edge has length 11. When the root is rr:

Let F(x)F(x) denote the parent of xx. In particular, F(r)=0F(r)=0. Let D(x,y)D(x,y) denote the length of the simple path from xx to yy. In particular, for all xx, D(x,x)=0D(x,x)=0. Let AxA_x denote the set of nodes in the subtree of xx (including xx itself), i.e. Ax={yD(x,y)=D(F(x),y)1}A_x=\{y\mid D(x,y)=D(F(x),y)-1\}. In particular, Ar={1,2,,n}A_r=\{1,2,\cdots,n\}. Let BxB_x denote the set of nodes adjacent to xx, i.e. Bx={yD(x,y)=1}B_x=\{y\mid D(x,y)=1\}.

Define f(x,u)f(x,u):

$$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 xx, f(0,x)=0f(0,x)=0. In the case x∉Aux\not\in A_u, if for all yBxy\in B_x we have D(y,u)D(x,u)D(y,u)\le D(x,u), then f(x,u)=axf(x,u)=a_x.

Define the score of node uu as W(u)=v=1nf(v,u)W(u)=\sum_{v=1}^nf(v,u).

Define the profit of node rr, C(r)C(r), as the value of i=1nW(i)\sum_{i=1}^nW(i) when the root is rr.

For each node rr, compute C(r)C(r).

All C(r)C(r) are taken modulo 998244353998244353.

Input Format

The first line contains a positive integer nn, the number of nodes in the tree.

The second line contains nn integers. The ii-th integer denotes the node weight aia_i of node ii.

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

Output Format

Output nn lines. The ii-th line outputs a positive integer, the value of node ii's profit C(i)C(i) modulo 998244353998244353.

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 r=1r=1 and u=5u=5, f(2,u)=a2=2f(2,u)=a_2=2, f(1,u)=a1f(2,u)=14f(1,u)=a_1\cdot f(2,u)=14, f(3,u)=a3f(1,u)=70f(3,u)=a_3\cdot f(1,u)=70, f(6,u)=a6=5f(6,u)=a_6=5, f(4,u)=a4max{f(3,u),f(6,u)}=7000f(4,u)=a_4\cdot\max\{f(3,u),f(6,u)\}=7000, and f(5,u)=f(4,u)+a5=7001f(5,u)=f(4,u)+a_5=7001.

[Constraints]

  • Subtask 1 (10 pts): n200n\le200, ai103a_i\le 10^3.
  • Subtask 2 (20 pts): n103n\le10^3.
  • Subtask 3 (20 pts): the tree is a chain.
  • Subtask 4 (20 pts): there exists a node whose degree is n1n-1.
  • Subtask 5 (30 pts): no special restrictions.

For 100%100\% of the testdata, 1n5×1051\le n\le5\times10^5, 1ai1091\le a_i\le10^9.

Translated by ChatGPT 5