time limit per test : 2 seconds
memory limit per test : 256 megabytes
You are given a tree consisting exactly of n n n vertices. Tree is a connected undirected graph with n − 1 n−1 n−1 edges. Each vertex v v v of this tree has a value a v a_v av assigned to it.
Let d i s t ( x , y ) dist(x,y) dist(x,y) be the distance between the vertices x x x and y y 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 v v . Then the cost of the tree is ∑ i = 1 n d i s t ( i , v ) ⋅ a i \sum^{n}_{i=1} dist(i,v)⋅a_i ∑i=1ndist(i,v)⋅ai.
Your task is to calculate the maximum possible cost of the tree if you can choose
v
v
v arbitrarily.
Input
The first line contains one integer n, the number of vertices in the tree ( 1 ≤ n ≤ 2 ⋅ 1 0 5 ) (1≤n≤2⋅10^5) (1≤n≤2⋅105).
The second line of the input contains n
integers
a
1
,
a
2
,
…
,
a
n
(
1
≤
a
i
≤
2
⋅
1
0
5
)
a1,a2,…,an (1≤a_i≤2⋅10^5)
a1,a2,…,an(1≤ai≤2⋅105), where
a
i
a_i
ai is the value of the vertex
i
i
i.
Each of the next n − 1 n−1 n−1 lines describes an edge of the tree. Edge i i i is denoted by two integers ui and vi, the labels of vertices it connects ( 1 ≤ u i , v i ≤ n , u i ≠ v i ) . (1≤u_i,v_i≤n, u_i≠v_i). (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
v
v
Input
8
9 4 1 7 10 1 6 5
1 2
2 3
1 4
1 5
5 6
5 7
5 8
Output
121
Input
1
1337
Output
0
Note
Picture corresponding to the first example:
You can choose the vertex
3
3
3 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
=
121
2⋅9+1⋅4+0⋅1+3⋅7+3⋅10+4⋅1+4⋅6+4⋅5=18+4+0+21+30+4+24+20=121
2⋅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 0 0 0.
题意:
给一个
n
n
n个点的树,每个点有一个点权
a
i
a_i
ai,,
d
i
s
t
(
x
,
y
)
dist(x,y)
dist(x,y)表示
x
x
x到
y
y
y的简单路径长度,求一点
v
v
v使得
∑
i
=
1
n
d
i
s
t
(
i
,
v
)
⋅
a
i
\sum^{n}_{i=1} dist(i,v)⋅a_i
∑i=1ndist(i,v)⋅ai最大,答案输出最大值。
题解:
考虑已知以一个点为
v
v
v点的答案,求其移动到一个相邻的点对答案造成的影响。假设当前
v
v
v点为
x
x
x点,当前的答案为
n
o
w
now
now,要将
v
v
v设为与
x
x
x相邻的
y
y
y点,则答案变化为
n
o
w
−
v
a
l
[
y
]
+
s
u
m
a
−
v
a
l
[
y
]
now-val[y]+suma-val[y]
now−val[y]+suma−val[y],
v
a
l
[
y
]
val[y]
val[y]是以
y
y
y为根的子树的所有点点权之和。
#include<bits/stdc++.h>
#define LiangJiaJun main
#define ll long long
using namespace std;
int n,cnt,ne,h[200004],a[200004];
struct edge{
int to,nt;
}e[400004];
ll suma,ans;
bool vis[200004];
ll dis[200004],val[200004];
void add(int u,int v){
e[++ne].to=v;e[ne].nt=h[u];
h[u]=ne;
}
void dfs(int x){
ans+=a[x]*(dis[x]-1);
for(int i=h[x];i;i=e[i].nt){
if(dis[e[i].to])continue;
dis[e[i].to]=dis[x]+1;
dfs(e[i].to);
val[x]+=val[e[i].to];
}
val[x]+=a[x];
}
void calc(int x,ll now){
ans=max(ans,now);
vis[x]=1;
for(int i=h[x];i;i=e[i].nt){
if(vis[e[i].to])continue;
calc(e[i].to,now+suma-(val[e[i].to]<<1));
}
}
int w33ha(){
ne=0;suma=0;
memset(val,0,sizeof(val));
memset(dis,0,sizeof(dis));
memset(h,0,sizeof(h));
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
suma+=a[i];
}
for(int i=1;i<n;i++){
int u,v;scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
ans=0;
dis[1]=1;
dfs(1);
calc(1,ans);
printf("%lld\n",ans);
return 0;
}
int LiangJiaJun(){
while(scanf("%d",&n)!=EOF)w33ha();
return 0;
}