Codeforces Round #527 (Div. 3)F(DFS,DP)
#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)的更多相关文章
- Codeforces Round #551 (Div. 2)D(树形DP)
#define HAVE_STRUCT_TIMESPEC#include <bits/stdc++.h>using namespace std;int val[300007],num[30 ...
- 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 ...
- 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 ...
- Codeforces Round #113 (Div. 2) Tetrahedron(滚动DP)
Tetrahedron time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- 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 ...
- 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 ...
- Codeforces Round #249 (Div. 2)B(贪心法)
B. Pasha Maximizes time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 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 ...
- [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 ...
随机推荐
- JavaScript常用函数以及语法
$("#dwid").val(checkedVal.join(',')); .字符串(String) trim() //去掉空格 1.声明 var myString ...
- 分享知识-快乐自己:idea的断点调试
1:Step Over ,进入下一步,如果是方法,那就直接跳过(F8) 2:Step Into,进入下一步,如果是方法,就进入方法内部,但是不会进入jdk封装的方法.(F7) 3:Force Step ...
- Javascript-- jQuery动画篇(1)
jQuery中隐藏元素的hide方法 让页面上的元素不可见,一般可以通过设置css的display为none属性.但是通过css直接修改是静态的布局,如果在代码执行的时候,一般是通过js控制元素的st ...
- L106 Three things we learned from day one at the World Cup
Hosts Russia got the World Cup off to a flying start by hammering Saudi Arabia 5-0 in the opening ga ...
- perl: warning: Falling back to the standard locale ("C").
/********************************************************************************** * perl: warning: ...
- FEC之我见三
继续上文讲解: 3) 标准的RTP头结构如下所示: 其中第一个字节中的x标志位是否扩展了RTP头,RTP协议允许用户自定义的扩展,扩展的字段紧挨上述RTP固定头.RTP扩展投中承载如下信息: 1).当 ...
- mysql调优参考笔记
之前一位童鞋发的: 5版邮件,在用户量很大的情况下,如果做了分布式,如果在后端mysql上执行: mysql> show global status like 'Thread%'; Th ...
- Java Modifiers
Private means this could only be seen within this class. Protected means "package private" ...
- ACM学习历程—FZU2191完美的数字(数学)
Description Bob是个很喜欢数字的孩子,现在他正在研究一个与数字相关的题目,我们知道一个数字的完美度是 把这个数字分解成三个整数相乘A*A*B(0<A<=B)的方法数,例如数字 ...
- 【LeetCode】001. Two Sum
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...