You are given a tree consisting exactly of ?n vertices. Tree is a connected undirected graph with ?−1n−1 edges. Each vertex ?v of this tree has a value ??av assigned to it.
Let ????(?,?)dist(x,y) be the distance between the vertices ?x and ?y. The distance between the vertices is the number of edges on the simple path between them.
Let's define the cost of the tree as the following value: firstly, let's fix some vertex of the tree. Let it be ?v. Then the cost of the tree is ∑?=1?????(?,?)⋅??∑i=1ndist(i,v)⋅ai.
Your task is to calculate the maximum possible cost of the tree if you can choose ?v arbitrarily.
Input
The first line contains one integer ?n, the number of vertices in the tree (1≤?≤2⋅1051≤n≤2⋅105).
The second line of the input contains ?n integers ?1,?2,…,??a1,a2,…,an (1≤??≤2⋅1051≤ai≤2⋅105), where ??ai is the value of the vertex ?i.
Each of the next ?−1n−1 lines describes an edge of the tree. Edge ?i is denoted by two integers ??ui and ??vi, the labels of vertices it connects (1≤??,??≤?1≤ui,vi≤n, ??≠??ui≠vi).
It is guaranteed that the given edges form a tree.
Output
Print one integer — the maximum possible cost of the tree if you can choose any vertex as ?v.
Examples
input
Copy
8 9 4 1 7 10 1 6 5 1 2 2 3 1 4 1 5 5 6 5 7 5 8
output
Copy
121
input
Copy
1 1337
output
Copy
0
Note
Picture corresponding to the first example:
You can choose the vertex 33 as a root, then the answer will be 2⋅9+1⋅4+0⋅1+3⋅7+3⋅10+4⋅1+4⋅6+4⋅5=18+4+0+21+30+4+24+20=1212⋅9+1⋅4+0⋅1+3⋅7+3⋅10+4⋅1+4⋅6+4⋅5=18+4+0+21+30+4+24+20=121.
In the second example tree consists only of one vertex so the answer is always 00.
题目意思:
给出你一颗带点权的树,dist(i, j)的值为节点i到j的距离乘上节点j的权值,让你任意找一个节点v,使得dist(v, i) (1 < i < n)的和最大。输出最大的值。
解题思路:
我们以dp[u]代表我们找的节点为u时,整棵树的权值, size[i]代表以i为根节点的子树的点权之和。那么dp[u]和dp[v](v是u的儿子节点)的关系就为:dp[v] = dp[u] + size[1] - 2 * size[v];也就是也就是除了v这颗子树之外,其他所有节点到
v的距离比到u的距离要大1,也就是把这些节点的权值加上一遍,同理,v这颗子树的所有节点距离减1,所以原来的式子就是dp[v] = dp[u] + (size[1] - size[v]) - size[v];
相比于暴力枚举所有节点找最终的答案,这种方法其实时用了递推的方法,利用相邻两节点的关系,在线性时间内求出了所有的情况。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn = 2 * 1e5 + 100;
ll dp[maxn], size[maxn], head[maxn];
int n, top;
ll ans;
struct node { //链式前向星存树,可以更换为其他的存储方式
int v, next;
}edge[maxn * 2];
inline void add (int u, int v) //建边
{
edge[top].v = v;
edge[top].next = head[u];
head[u] = top++;
}
void dfs(int u, int father) //求出根为1的时候的dp值
{
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].v;
if(v != father)
{
dfs(v, u);
size[u] += size[v];
dp[u] += size[v] + dp[v];
}
}
}
void dfsx(int u, int father)
{
if(u != 1)
dp[u] = dp[father] + size[1] - 2 * size[u]; //递推解决其他的情况
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].v;
if(v != father)
{
dfsx(v, u);
}
}
ans = max(ans, dp[u]); //求最大值
}
int main()
{
//freopen("in.txt", "r", stdin);
cin >> n;
top = 0;
ans = 0;
memset(head, -1, sizeof(head));
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= n; ++ i)
{
scanf("%I64d", &size[i]);
}
int u, v;
for(int i = 1; i <= n - 1; ++ i)
{
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
dfs(1, 0);
dfsx(1, 0);
cout << ans << endl;
return 0;
}