Codeforces Round #627 (Div. 3) F - Maximum White Subtree(深度优先搜索)
题意:
n 个点 n - 1 条边的树,问每个点所在所有子树中白黑点数目的最大差。
思路:
白点先由下至上汇集,后由上至下分并。
#include <bits/stdc++.h>
using namespace std; const int M=220000; vector<vector<int>> e(M); int n,a[M]; void dfs1(int u,int pre){
for(int v:e[u]){
if(v!=pre){
dfs1(v,u);
a[u]+=max(0,a[v]);
}
}
} void dfs2(int u,int pre){
for(int v:e[u]){
if(v!=pre){
a[v]+=max(0,a[u]-max(0,a[v]));
dfs2(v,u);
}
}
} int main(){
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
if(a[i]==0) a[i]=-1;
}
for(int i=0;i<n-1;i++){
int u,v;cin>>u>>v;
--u,--v;
e[u].push_back(v);
e[v].push_back(u);
}
dfs1(0,-1);
dfs2(0,-1);
for(int i=0;i<n;i++) cout<<a[i]<<' ';
return 0;
}
Codeforces Round #627 (Div. 3) F - Maximum White Subtree(深度优先搜索)的更多相关文章
- Codeforces Round #555 (Div. 3) F. Maximum Balanced Circle
F. Maximum Balanced Circle 题目链接 题意 给出\(n\)个数,现在要从中选出最多的数\(b_i,b_{i+1},\cdots,b_k\),将这些数连成一个环,要求两两相邻的 ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索
Codeforces Round #297 (Div. 2)D. Arthur and Walls Time Limit: 2 Sec Memory Limit: 512 MBSubmit: xxx ...
- Codeforces Round #297 (Div. 2)E. Anya and Cubes 折半搜索
Codeforces Round #297 (Div. 2)E. Anya and Cubes Time Limit: 2 Sec Memory Limit: 512 MBSubmit: xxx ...
- 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 #376 (Div. 2) F. Video Cards —— 前缀和 & 后缀和
题目链接:http://codeforces.com/contest/731/problem/F F. Video Cards time limit per test 1 second memory ...
随机推荐
- ASP.NET Core 3.1 中间件
参考微软官方文档 : https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/middleware/?view=aspnetcore-3.1 ...
- Docker 介绍和安装(一)
# 下载阿里云的 Centos7 的docker.repo # step 1: 安装必要的一些系统工具 sudo yum install -y yum-utils device-mapper-pers ...
- 目录遍历 - Pikachu
概述: 在web功能设计中,很多时候我们会要将需要访问的文件定义成变量,从而让前端的功能便的更加灵活. 当用户发起一个前端的请求时,便会将请求的这个文件的值(比如文件名称)传递到后台,后台再执行其对应 ...
- 微信小程序腾讯地图SDK使用方法
一.本篇文章主要知识点有以下几种: 1.授权当前位置 2.map组件的使用 3.腾讯地图逆地址解析 4.坐标系的转化 二.效果如下: 三.WXML代码 <map id="map&quo ...
- Ajax编程基础
目录 Ajax编程基础 传统网站中存在的问题 Ajax概述 Ajax的应用场景 Ajax的运行环境 Ajax运行原理及实现 Ajax运行原理 Ajax的实现步骤 1.创建Ajax对象 2.告诉Ajax ...
- JavaScript中的迭代器和生成器[未排版]
JavaScript中的迭代器 在软件开发领域,"迭代"的意思是按照顺序反复多次执行一段程序,通常会有明确的终止条件. ECMAScript 6规范新增了两个高级特性:迭代器和生成 ...
- 树莓派做私有云盘-极简版(owncloud)
这里直接给出配置好私有云的镜像,只需烧录镜像后微改配置后即可使用 链接:https://pan.baidu.com/s/1EOQaSQso-0wmnuWgZKknZg提取码:q26h 1.直接将此镜像 ...
- Property or method "previewUrl" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components,
Property or method "previewUrl" is not defined on the instance but referenced during rende ...
- PB 级大规模 Elasticsearch 集群运维与调优实践
PB 级大规模 Elasticsearch 集群运维与调优实践 https://mp.weixin.qq.com/s/PDyHT9IuRij20JBgbPTjFA | 导语 腾讯云 Elasticse ...
- 语言反射规则 - The Laws of Reflection
[译]Go反射的三个原则(官方博客) | seven的分享 https://sevenyu.top/2019/12/21/laws-of-reflection.html wilhg/The-Laws- ...