#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)F(DFS,DP)的更多相关文章

  1. Codeforces Round #551 (Div. 2)D(树形DP)

    #define HAVE_STRUCT_TIMESPEC#include <bits/stdc++.h>using namespace std;int val[300007],num[30 ...

  2. Codeforces Round #272 (Div. 1)D(字符串DP)

    D. Dreamoon and Binary time limit per test 2 seconds memory limit per test 512 megabytes input stand ...

  3. Codeforces Round #272 (Div. 1)C(字符串DP)

    C. Dreamoon and Strings time limit per test 1 second memory limit per test 256 megabytes input stand ...

  4. Codeforces Round #113 (Div. 2) Tetrahedron(滚动DP)

    Tetrahedron time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  5. Codeforces Round #518 (Div. 2) D(计数DP)

    #include<bits/stdc++.h>using namespace std;const long long mod=998244353;int n;int a[100007];l ...

  6. Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost 【DFS换根 || 树形dp】

    传送门:http://codeforces.com/contest/1092/problem/F F. Tree with Maximum Cost time limit per test 2 sec ...

  7. Codeforces Round #249 (Div. 2)B(贪心法)

    B. Pasha Maximizes time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  8. Codeforces Round #277 (Div. 2)D(树形DP计数类)

    D. Valid Sets time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  9. [Educational Codeforces Round 63 ] D. Beautiful Array (思维+DP)

    Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds ...

随机推荐

  1. Array对象(一)

    Array是JavaScript中的一个事先定义好的对象(也可以称作一个类),可以直接使用. 创建Array对象: var array = new Array(); 创建指定元素个数的Array对象: ...

  2. 分享知识-快乐自己:关于 String 小案例

    单个字符出现的次数: /*** * 验证是否符合拆分条件 * * @param text * 原字符串 * @param sub * 判断条件 * @return */ public static i ...

  3. 山东省第七届ACM省赛

    ID Title Hint A Julyed 无 B Fibonacci 打表 C Proxy 最短路径 D Swiss-system tournament 归并排序 E The Binding of ...

  4. Geoserver端口冲突解决方案(二)

    转载:https://blog.csdn.net/sx341125/article/details/52091903 上一篇文章说了GeoServer的安装问题,其中一个问题就是GeoServer端口 ...

  5. jenkins-小知识点

    如果想停止jenkins运行 控制面板-服务-查看本地服务-选中jenkins 1.启动类型改为手动 2.改为禁止 使用的时候,每次都改一下状态

  6. 201621123014《Java程序设计》第四周学习总结

    1.本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 答:继承.多态.子类.父类.final.static.类型判断与类型转换.抽象类. 1.2 尝试使用思维导图将这些关键词组织起来. ...

  7. I.MX6 Python3 OpenCV

    /************************************************************************* * I.MX6 Python3 OpenCV * ...

  8. ubuntu 上采用nginx做rtmp 直播 服务器

    首先安装必要的依赖库   sudo apt-get install autoconf automake sudo apt-get install libpcre3 libpcre3-dev   安装 ...

  9. mysql调优参考笔记

    之前一位童鞋发的: 5版邮件,在用户量很大的情况下,如果做了分布式,如果在后端mysql上执行:   mysql> show global status like 'Thread%';   Th ...

  10. Sentry的要点

    1.Apache的Build 在研究Sentry的时候,发现没有bin.jar,只能手工编辑工程,但是编辑发现很多jar包有问题:在访问官网的时候发现左侧菜单中有一项是builds,点开后(https ...