#include<bits/stdc++.h>
using namespace std;
const int N=200005;
int n,A[N];
long long Mx,tot,S[N];
vector<int>Adj[N];
void DFS(int v,int p){
S[v]=A[v];
for(int &u:Adj[v])
if(u!=p)
DFS(u,v),S[v]+=S[u];
if(p)//0号结点是不存在的
tot+=S[v];
}
void DFS2(int v,int p){
Mx=max(Mx,tot);
for(int &u:Adj[v])
if(u != p){
tot-=S[u];//换根后新根子树的权重会减小一段,即减为一半
tot+=S[1]-S[u];//换根后新根子树的父结点与新根结点子树权重的差值会增大一段,即增加一倍
DFS2(u,v);//对新根继续深度优先搜索
tot-=S[1]-S[u];//还原为原值
tot+=S[u];//同上
}
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&A[i]);
for(int i=1,v,u;i<n;i++)
scanf("%d%d",&v,&u),Adj[v].push_back(u),Adj[u].push_back(v);
DFS(1,0);//深度优先搜索每个结点子树(包含当前结点)的权重
DFS2(1,0);//深度优先搜索换根后的WPL值
return !printf("%lld",Mx);
}
相关文章
- Codeforces Round #527 (Div. 3) 总结 A B C D1 D2 F
- Codeforces Round #527 (Div. 3) C. Prefixes and Suffixes (思维,字符串)
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
- Codeforces Round #501 (Div. 3) F. Bracket Substring
- Codeforces Round #554 (Div. 2) F2. Neko Rules the Catniverse (Large Version) (矩阵快速幂 状压DP)
- Codeforces Round #527 (Div. 3)
- Codeforces Round #527 (Div. 3)D2(栈,思维)
- Codeforces Round #527 (Div. 3) ABCDEF题解
- Codeforces Round #527 (Div. 3) . F Tree with Maximum Cost
- Codeforces Round #527 (Div. 3) D1. Great Vova Wall (Version 1) 【思维】