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 ...
随机推荐
- 仿联想商城laravel实战---2、后端页面搭建(验证码如何在页面中使用)
仿联想商城laravel实战---2.后端页面搭建(验证码如何在页面中使用) 一.总结 一句话总结: 放在img里面,img的src就是生产验证码的控制器路径: img src="/admi ...
- php: 0跟字符串做比较永远是true。 php大bug。
php: 0跟字符串做比较永远是true. php大bug. 如: $a = 0; if( $a == 'excel') { echo "yes"; }else{ echo &qu ...
- python基础-循环语句for\嵌套循环
for循环格式: for index in range(0,3):#等同于range(3),取0\1\2 print(index) index = 0 starnames = ['xr1','xr2' ...
- 重拾安卓_01_安卓开发环境搭建(eclipse)
一.下载安装Android SDK 1.下载地址 (1)官网(可FQ选择):http://developer.android.com/sdk/index.html (2)不可FQ选择:http://w ...
- jQuery插件--图片文字向上向左循环滚动
需要引用jquery 调用非常简单: 一. 向上滚动 $(".scroll_two").jScroll({vertical: true}); <div class=" ...
- [转]sscanf函数具体用法
大学生程序代写 sscanf 名称: sscanf() - 从一个字符串中读进与指定格式相符的数据. 函数原型: Int sscanf( string str, string fmt, mixed v ...
- Agc003_E Sequential operations on Sequence
传送门 题目大意 $1,2...n,n$个数从小到大排列,有$m$此操作,每次操作给定一个参数$x$,将当且数列作为循环节无限地展开下去,再取前$x$个作为新的数列,求最终的数列每个数出现的次数. $ ...
- bzoj 3796: Mushroom追妹纸 AC自动机+后缀自动机+dp
题目大意: 给定三个字符串s1,s2,s3,求一个字符串w满足: w是s1的子串 w是s2的子串 s3不是w的子串 w的长度应尽可能大 题解: 首先我们可以用AC自动机找出s3在s1,s2中出现的位置 ...
- 转 Django根据现有数据库,自动生成models模型文件
Django引入外部数据库还是比较方便的,步骤如下 : 创建一个项目,修改seting文件,在setting里面设置你要连接的数据库类型和连接名称,地址之类,和创建新项目的时候一致 运行下面代码可以自 ...
- [转]各种开源协议介绍 BSD、Apache Licence、GPL V2 、GPL V3 、LGPL、MIT
现今存在的开源协议很多,而经过Open Source Initiative组织通过批准的开源协议目前有58种(http://www.opensource.org/licenses /alphabeti ...