luogu#P8009. 「Wdsr-3」船往低处流

「Wdsr-3」船往低处流

Background

Murasa Minamitsu is the captain who controls the Hijiri ship. As a ship ghost who has spent her whole life with ships, she is very interested in them. Because of this hobby, Murasa has a lot of ship models.

Due to the eruption of a geyser, a large water pit appeared around it, gathering many streams of water. Different streams interweave and form waterways of various sizes. If you just place a ship model at some position, it will flow along the water. According to physics, a ship naturally flows from high places to low places. Since the pit is formed by water gathering from all directions, the center of the pit is the lowest; as the distance to the center increases, the terrain keeps getting higher.

Murasa found that when she chooses two positions to place ship models, they will collide at some junction of the waterways. Murasa cares about where the collision happens. It is easy to see that the first possible collision position is the lowest common ancestor of the two chosen points in the tree structure.

Of course, since the geyser is unstable, the position of the center of the pool may keep changing, and the terrain also changes, but the waterways do not change at all. Murasa labels each junction with a value called "danger level", representing how dangerous it might be if two ship models collide there. The positions where Murasa places ship models are also random.

However, the pit is too large, and the center keeps changing, so Murasa, who only cares about ship models, gets confused. She urgently wants to know the threat caused by playing with ship models in the pit, so she hopes you can help her compute it.

Problem Description

These waterways form a rooted tree structure TT with nn nodes, rooted at 11. Each node has a node weight wiw_i, representing its danger level. Define the following:

  • Lowest common ancestor: In a rooted tree with root rr, the lowest common ancestor of two nodes u,vu,v is the common ancestor of these two nodes that is farthest from the root, denoted as lca(r,u,v)\operatorname{lca}(r,u,v).
  • Subtree: In tree TT, after deleting the edge between node uu and its parent, the connected component containing uu is called the subtree TuT_u. In particular, TT itself can be seen as the subtree T1T_1 rooted at 11.
  • Danger value: For TuT_u, its danger value is defined as:
$$\mathrm{LCAS}(u)=\sum_{i\in T_u}\sum_{j\in T_u}\sum_{k\in T_u,k<j} w_{\operatorname{lca}(i,j,k)}$$

Now given TT, for i=1,2,ni=1,2,\cdots n, compute LCAS(i)\mathrm{LCAS}(i).

Input Format

  • The first line contains a positive integer nn, the number of nodes.
  • The second line contains nn integers wiw_i, the danger level of each node.
  • The next n1n-1 lines each contain two positive integers u,vu,v, describing an edge in TT.

Output Format

  • Output nn lines, each containing one integer. The ii-th integer is LCAS(i)mod998,244,353\mathrm{LCAS}(i)\bmod 998,244,353 (a large prime).
5
3 1 2 1 3
1 2
1 3
3 4
3 5

109
0
18
0
0
10
1 1 4 5 1 4 1 9 1 9
1 2
1 3
1 4
2 5
2 6
5 7
3 8
3 9
9 10
972
33
99
0
2
0
0
0
10
0

Hint

Explanation for Sample 1

The tree in sample 1 is as follows. Red nodes are the nodes, and blue ones are the node weights.

It is easy to see that $\mathrm{LCAS}(2)=\mathrm{LCAS}(4)=\mathrm{LCAS}(5)=0$. Here we explain how to compute LCAS(1)\mathrm{LCAS}(1) and LCAS(3)\mathrm{LCAS}(3). First, we explain LCAS(3)\mathrm{LCAS}(3):

  • With 33 as the root, we have $\mathrm{lca}(3,3,4)=\mathrm{lca}(3,3,5)=\mathrm{lca}(3,4,5)=3$, and the contribution of this part is 3×w3=63\times w_3=6.
  • With 44 as the root, we have $\mathrm{lca}(4,3,4)=\mathrm{lca}(4,4,5)=4,\mathrm{lca}(4,3,5)=3$, and the contribution of this part is 2×w4+1×w3=42\times w_4+1\times w_3=4.
  • With 55 as the root, we have $\mathrm{lca}(5,3,5)=\mathrm{lca}(5,4,5)=5,\mathrm{lca}(5,3,4)=3$, and the contribution of this part is 2×w5+1×w3=82\times w_5+1\times w_3=8.

Therefore, LCAS(3)=6+4+8=18\mathrm{LCAS}(3)=6+4+8=18. Next we compute LCAS(1)\mathrm{LCAS}(1).

$$\def\arraystretch{1.2} \begin{matrix} \textbf{Root is 1 }\bm{\mathbf{lca}(1,i,j)} & \textbf{Root is 2 }\bm{\mathbf{lca}(2,i,j)}\cr \begin{array}{c||c|c|c|c|c}\hline & 1 & 2 & 3 & 4 & 5 \cr\hline\hline 1 & - & - & - & - &- \cr\hline 2 & 1 & - & - & - &- \cr\hline 3 & 1 & 1 & - & - &- \cr\hline 4 & 1 & 1 & 3 & - &- \cr\hline 5 & 1 & 1 & 3 & 3 &- \cr\hline \end{array} & \begin{array}{c||c|c|c|c|c}\hline & 1 & 2 & 3 & 4 & 5 \cr\hline\hline 1 & - & - & - & - &- \cr\hline 2 & 2 & - & - & - &- \cr\hline 3 & 1 & 2 & - & - &- \cr\hline 4 & 1 & 2 & 3 & - &- \cr\hline 5 & 1 & 2 & 3 & 3 &- \cr\hline \end{array} \cr[50pt] \textbf{Root is 3 }\bm{\mathbf{lca}(3,i,j)} & \textbf{Root is 4 }\bm{\mathbf{lca}(4,i,j)}\cr \begin{array}{c||c|c|c|c|c}\hline & 1 & 2 & 3 & 4 & 5 \cr\hline\hline 1 & - & - & - & - &- \cr\hline 2 & 1 & - & - & - &- \cr\hline 3 & 3 & 3 & - & - &- \cr\hline 4 & 3 & 3 & 3 & - &- \cr\hline 5 & 3 & 3 & 3 & 3 &- \cr\hline \end{array} & \begin{array}{c||c|c|c|c|c}\hline & 1 & 2 & 3 & 4 & 5 \cr\hline\hline 1 & - & - & - & - &- \cr\hline 2 & 1 & - & - & - &- \cr\hline 3 & 3 & 3 & - & - &- \cr\hline 4 & 4 & 4 & 4 & - &- \cr\hline 5 & 3 & 3 & 3 & 4 &- \cr\hline \end{array} \end{matrix}\\[10pt] \textbf{Root is 5 }\bm{\mathbf{lca}(5,i,j)}\\ \begin{array}{c||c|c|c|c|c}\hline & 1 & 2 & 3 & 4 & 5 \cr\hline\hline 1 & - & - & - & - &- \cr\hline 2 & 1 & - & - & - &- \cr\hline 3 & 3 & 3 & - & - &- \cr\hline 4 & 3 & 3 & 3 & - &- \cr\hline 5 & 5 & 5 & 5 & 5 &- \cr\hline \end{array}$$

It is easy to see that in the table above, 11 appears 1313 times, 22 appears 44 times, 33 appears 2525 times, 44 appears 44 times, and 55 appears 44 times. Therefore, $\mathrm{LCAS}(1)=3\times 13+1\times 4+2\times 25+1\times 4+3\times 4=109$.

Explanation for Sample 2

I have an ingenious method to explain sample 22, but unfortunately this blank area is too small to write it down.

The input size of this problem is large. Please use a fast input method.

Constraints and Notes

$$\def\arraystretch{1.5} \begin{array}{|c|c|c|c|}\hline \textbf{Subtask} & \bm{n\le} & \textbf{Special Property} & \textbf{Score}\cr\hline 1 & 100 & - & 20 \cr\hline 2 & 10^3 & - & 25 \cr\hline 3 & 10^5 & \text{A} & 10\cr\hline 4 & 10^5 & \text{B} & 10\cr\hline 5 & 10^6 & - & 35\cr\hline \end{array}$$

Special Property A\textbf{A}: It is guaranteed that the ii-th edge is u=iu=i, v=i+1v=i+1.
Special Property B\textbf{B}: It is guaranteed that the ii-th edge is u=1u=1, v=i+1v=i+1.

For all testdata, it is guaranteed that 1n1061\le n\le 10^6 and 0wi<998,244,3530\le w_i<998,244,353.

Translated by ChatGPT 5