#Lutece3003. 又是树上距离
又是树上距离
Migrated from Lutece 3003 又是树上距离
All parts of this problem, including description, images, samples, data and checker, might be broken. If you find bugs in this problem, please contact the admins.
Description
给一棵 个点的树,每个点 有点权 。定义两个点 之间的花费 为 ,其中 表示节点 在树上的距离。现在你需要对每个点 求 的值。
Input
第一行一个正整数 ,表示树的节点个数。 第二行 个正整数 ,表示每个点的点权。 接下来 行,每行两个整数,表示点之间有一条边。
Output
输出 行,第 行表示第 个数的答案。
Samples
5
1 2 3 4 5
1 2
1 5
2 3
2 4
35
56
201
252
360
Constraints
,
Note
对于样例中的3号节点,其距离1,2,3,4,5号节点的距离分别为 ,因此对应的答案为 $3\times 1\times 2^2+3\times 2\times 1^2+3\times 3\times 0^2+3\times 4\times 2^2+3\times 5\times 3^2=12+6+0+48+135=201$.
Resources
2023 UESTC ICPC Training for Search and Dynamic Programming