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 ...
随机推荐
- 从HTTP请求中获取客户IP地址
/** * 从HTTP请求中获取客户IP地址 * * @param request http请求 * @return 客户IP地址 */ public s ...
- C#--父子页面传值、刷新(showModalDialog)
父页面: var obj = new Object(); obj.name="name"; var rtnValue=window.showModalDialog("ch ...
- PHP+MySQL百万级数据插入的优化
插入分析 MySQL中插入一个记录需要的时间由下列因素组成,其中的数字表示大约比例: 连接:(3) 发送查询给服务器:(2) 分析查询:(2) 插入记录:(1x记录大小) 插入索引:(1x索引) 关闭 ...
- C#winform拖拽实现获得文件路径
1.关键知识点说明: 通过DragEnter事件获得被拖入窗口的“信息”(可以是若干文件,一些文字等等),在DragDrop事件中对“信息”进行解析.窗体的AllowDrop属性必须设置成true;且 ...
- Selenium-使用firepath获取元素的xpath
- Eclipse_常用技巧_02_使用Eclipse进行源码分析
1.分析java类和接口的继承关系 具体做法: 在代码区中选择需要的类和接口定义,然后右击,选择“Open Type Hiberarchy”,可以在“Hiberarchy View”中看到继承关系 快 ...
- 关于linux 安装 python pymssql模块
需要先安装freetds是一个开源的C程序库,它可以实现在Linux系统下访问操作微软的SQL数据库.可以用在Sybase的db-lib或者ct-lib库,在里面也包含了一个ODBC的库.允许许多应用 ...
- 1022 Digital Library (30)(30 分)
A Digital Library contains millions of books, stored according to their titles, authors, key words o ...
- BZOJ3064:CPU监控
浅谈区间最值操作和历史最值问题:https://www.cnblogs.com/AKMer/p/10225100.html 题目传送门:https://lydsy.com/JudgeOnline/pr ...
- 块级&行内元素总结
一.块级元素与行内元素的区别 块级元素与行内元素有几个关键区别: 格式 默认情况下: 块级元素会新起一行: 行内元素不会以新行开始. 内容模型 一般块级元素可以包含行内元素和其他块级元素.这种结构上的 ...