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 with nodes, rooted at . Each node has a node weight , representing its danger level. Define the following:
- Lowest common ancestor: In a rooted tree with root , the lowest common ancestor of two nodes is the common ancestor of these two nodes that is farthest from the root, denoted as .
- Subtree: In tree , after deleting the edge between node and its parent, the connected component containing is called the subtree . In particular, itself can be seen as the subtree rooted at .
- Danger value: For , its danger value is defined as:
Now given , for , compute .
Input Format
- The first line contains a positive integer , the number of nodes.
- The second line contains integers , the danger level of each node.
- The next lines each contain two positive integers , describing an edge in .
Output Format
- Output lines, each containing one integer. The -th integer is (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 and . First, we explain :
- With 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 .
- With 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 .
- With 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 .
Therefore, . Next we compute .
$$\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, appears times, appears times, appears times, appears times, and appears 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 , 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 : It is guaranteed that the -th edge is , .
Special Property : It is guaranteed that the -th edge is , .
For all testdata, it is guaranteed that and .
Translated by ChatGPT 5