Educational Codeforces Round 67 E.Tree Painting (树形dp)
题意:给你一棵无根树,每次你可以选择一个点从白点变成黑点(除第一个点外别的点都要和黑点相邻),变成黑点后可以获得一个权值(白点组成连通块的大小) 问怎么使权值最大
思路:首先,一但根确定了,整棵树的权值就只需要模拟即可,所以思路就转换为求哪一个点为根的权值最大。
这题需要用到一个二次扫描换根的思想,我们可以先从任意一个点去进行树形dp 并且得到从这个点开始去逐渐更新他的儿子节点
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1};
int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1};
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
int n;
vector<int> G[200007];
ll dp[200007]; //表示i节点以下的所有贡献
ll f[200007]; //以i为根的权值
ll nump[200007]; //儿子节点数(包含自己)
void dfs(int u,int fa){
nump[u]=1;
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(v==fa) continue;
dfs(v,u);
nump[u]+=nump[v];
}
}
void dfss(int u,int fa){
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(v==fa) continue;
dfss(v,u);
dp[u]+=dp[v];
}
dp[u]+=nump[u];
}
void change(int u,int fa){
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(v==fa) continue;
f[v]=f[u]-nump[v]-dp[v]+n-nump[v]+dp[v]; //核心代码
change(v,u);
}
}
int main(){
ios::sync_with_stdio(false);
cin>>n;
for(int i=1;i<n;i++){
int u,v; cin>>u>>v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1,0);
dfss(1,0);
f[1]=dp[1];
change(1,0);
ll ans=0;
for(int i=1;i<=n;i++){
ans=max(ans,f[i]);
}
cout<<ans<<"\n";
return 0;
}
Educational Codeforces Round 67 E.Tree Painting (树形dp)的更多相关文章
- Educational Codeforces Round 67
Educational Codeforces Round 67 CF1187B Letters Shop 二分 https://codeforces.com/contest/1187/submissi ...
- Educational Codeforces Round 67 D. Subarray Sorting
Educational Codeforces Round 67 D. Subarray Sorting 传送门 题意: 给出两个数组\(a,b\),现在可以对\(a\)数组进行任意次排序,问最后能否得 ...
- Educational Codeforces Round 53 E. Segment Sum(数位DP)
Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...
- Codeforces Educational Codeforces Round 67
目录 Contest Info Solutions A. Stickers and Toys B. Letters Shop C. Vasya And Array D. Subarray Sortin ...
- Educational Codeforces Round 67 (Rated for Div. 2)
A 考虑之前选中没有一个的,那么结果就是\(min(n-s,n-t)\) 那么能选中的第一次就是这个结果\(+1\),但需要拥有两个 \((s>t)\)考虑一开始选不中\(t\),则但选中\(t ...
- Educational Codeforces Round 67 (Rated for Div. 2) B题【前缀+二分】【补题ING系列】
题意:给出一个字符串s, 可以从左往右拿走s的字符, 至少要到s的第几个位置才能拼成t 思路:用二维数组记录前缀,然后二分即可. #include<bits/stdc++.h> using ...
- Codeforces Round #530 (Div. 2) F (树形dp+线段树)
F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...
- Educational Codeforces Round 16 E. Generate a String dp
题目链接: http://codeforces.com/problemset/problem/710/E E. Generate a String time limit per test 2 seco ...
- Educational Codeforces Round 8 D. Magic Numbers 数位DP
D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...
随机推荐
- Tomca7t服务器 配置HTTP和HTTPS 同时访问
(首先你要有 ssl 证书 ,我是阿里的 ) 查看申请ssl证书(https://www.cnblogs.com/lxf-mw/p/14261303.html) 一.下载 tomcat证书(两个文件) ...
- Label_strange_labels
空格符号 特殊符号名 注释语句 , align = "left right center" 粗体 斜体 也可能是控制标签 上标 下标 大字号 小字号 下划线 删除线 等宽 键盘输 ...
- MongoDB备份(mongoexport)与恢复(mongoimport)
1.备份恢复工具介绍: mongoexport/mongoimport mongodump/mongorestore(本文未涉及) 2.备份工具区别在哪里? 2.1 mongoexport/mongo ...
- Mac M1原生(ARM64)Golang dev&debug
前言 通过本文最终实现了在M1芯片的Mac mini上的Goland的开发,并通过编译源码解决了无法DEBUG的问题. Go 1.16版将正式支持Apple Silicon M1芯片,即arm64架构 ...
- 剑指offer 面试题5:替换空格
题目描述 请实现一个函数,将一个字符串中的每个空格替换成"%20".例如,当字符串为We Are Happy. 则经过替换之后的字符串为We%20Are%20Happy. 编程思想 ...
- 关于使用th:text获取不到值
今天在使用thymeleaf模板引擎整合SpringBoot时,对于从controller层传递过来的参数"message",无法获取. 控制层代码如下: @PostMapping ...
- Desired_Capabilities配置
appium服务器初始化参数 最全: https://github.com/appium/appium/blob/master/docs/cn/writing-running-appium/caps. ...
- 【ORACLE】11g rac+dg
首先感谢群友分享的文档,在这里先感谢哆啦B梦,非常感谢 该文档主要指导如何利用现有的RAC环境搭建一套RAC与单实例的DG的环境 ============================主机配置信息 ...
- 【Linux】服务器识别ntfs移动磁盘方法
Linux服务器无法识别ntfs磁盘 如果想识别的话,需要安装一个包ntfs-3g 安装好后,将移动磁盘插入到服务器的usb口中 新建一个目录,将磁盘挂载在新建的目录上 挂载命令如下: mount - ...
- 利用iptables防火墙保护web服务器
实例:利用iptables防火墙保护web服务器 防火墙--->路由器-->交换机-->pc机 配置之前,清空下已有的规则,放在规则冲突不生效 工作中,先放行端口写完规则,再DROP ...