#P1351. [NOIP 2014 提高组] 联合权值

    ID: 348 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>动态规划,dp2014NOIp 提高组最近公共祖先,LCA

[NOIP 2014 提高组] 联合权值

Description

An undirected connected graph GG has nn vertices and n1n-1 edges. The vertices are numbered from 11 to nn, and the weight of vertex ii is WiW_i. Each edge has length 11. For any two vertices uu and vv, their distance is defined as the length of the shortest path from uu to vv. For a pair of vertices (u,v)(u, v) in GG, if their distance is 22, then they generate a joint weight of Wv×WuW_v \times W_u.

Among all ordered pairs in GG that can generate a joint weight, what is the maximum joint weight? What is the sum of all joint weights?

Input Format

The first line contains 11 integer nn.

The next n1n-1 lines each contain 22 positive integers u,vu, v separated by a space, indicating that there is an edge between vertex uu and vertex vv.

The last line contains nn positive integers separated by single spaces, where the ii-th integer indicates that the weight of vertex ii in GG is WiW_i.

Output Format

Output a single line containing 22 integers separated by a space: the maximum joint weight on graph GG and the sum of all joint weights. Since the sum may be large, output it modulo 1000710007.

5  
1 2  
2 3
3 4  
4 5  
1 5 2 3 10 
20 74

Hint

Sample Explanation.

For the input in this example, the graph is as shown above. The ordered pairs at distance 22 are (1,3),(2,4),(3,1),(3,5),(4,2),(5,3)(1, 3), (2, 4), (3, 1), (3, 5), (4, 2), (5, 3).

Their joint weights are 2,15,2,20,15,202, 15, 2, 20, 15, 20. The maximum is 2020, and the total sum is 7474.

Constraints

  • For 30%30\% of the testdata, 2<n1002 < n \leq 100.
  • For 60%60\% of the testdata, 2<n20002 < n \leq 2000.
  • For 100%100\% of the testdata, 2<n2×1052 < n \leq 2 \times 10^5, 0<Wi100000 < W_i \leq 10000.

It is guaranteed that there exists at least one ordered pair that can generate a joint weight.

Translated by ChatGPT 5