#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. PHP中include路径的解决方法汇总

    这几天整理一份很乱的代码,这才意识到php对include处理不是一般的贱:别的编程语言在处理include中的相对目录时,都是以当前处理的文件作为基准.也就是说,如果A包含B,B包含C时,C再包含一 ...

  2. 分享知识-快乐自己:虚拟机 NET 网络配置

    第一步: 第二步: 第三步:   第四步:   第五步: 第六步: 第七步: 第九步: 第十步: 第十一步: 第十二步:   第十三步: 成功. 第十四步:开启开机自启动网路连接 cd /etc/sy ...

  3. javascript笔记(一)

    使用function关键字来定义函数,分为两种形式: 声明式函数定义: function add(m,n) { alert(m+n); } 这种方式等同于构造一个Function类的实例的方式: va ...

  4. python生成excel格式座位表

    脚本分两个文件: 1.生成二维随机列表:GenerateLocaltion.py 2.将列表导入excel文件:CreateExcel.py 先上GenerateLocaltion.py: impor ...

  5. BEC listen and translation exercise 46

    录音文件 https://pan.baidu.com/s/1qYYZGWO The process of learning and exploring a subject can lead to a ...

  6. vc6++Release和Debug

    1. 如何快速地规范代码缩进格式 选中所需要规范的代码,按shift+F8 2. 如何在Release状态下进行调试 Project->Setting=>ProjectSetting对话框 ...

  7. codeforces 598E E. Chocolate Bar(区间dp)

    题目链接: E. Chocolate Bar time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  8. Java 时间和日期类型的 Hibernate 映射

    以下情况下必须显式指定 Hibernate 映射类型 一个 Java 类型可能对应多个 Hibernate 映射类型. 例如: 如果持久化类的属性为 java.util.Date 类型, 对应的 Hi ...

  9. Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

    使用hibernate的时候,报出这个错误Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvir ...

  10. android sqlite,大数据处理、同时读写

    1. 批量写入,采用事物方式,先缓存数据,再批量写入数据,极大提高了速度 288条,直接inset  into  耗时7秒 8640条,     批量写入  耗时5-7秒 try { this.myD ...