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 ...
随机推荐
- WPF 调试时拖拽不生效
WPF窗体代码 <Window x:Class="SerialLabelDemo.Test.Window10" xmlns="http://schemas.micr ...
- TCP/IP协议栈在Linux内核中的运行时序分析
网络程序设计调研报告 TCP/IP协议栈在Linux内核中的运行时序分析 姓名:柴浩宇 学号:SA20225105 班级:软设1班 2021年1月 调研要求 在深入理解Linux内核任务调度(中断处理 ...
- 关于使用jq跨域请求的实现
今天算是把js跨域请求搞定了,想实现跨越,首先想到的是JSONP,但是具体去做的时候,发现有很多坑.在本地测试好之后又发现目标网站做了https证书认证,也就是实用的jsonp请求地址必须是https ...
- 克隆slave
在日常生活中,我们做的比较多的操作就是在线添加从库,比如线上有一主一丛两个数据库,由于业务的需要一台从库的读取量无法满足现在的需求,这样就需要我们在线添加从库,出于安全考虑,我们通常需要在从库上进行在 ...
- 【EXPDP】导出全部表的时候,选择不导出某个表
导出的时候指定某一张表不导出的话,一般都用的是数据泵的expdp来操作 具体方法是: expdp test/test dumpfile=test.dmp directory=test_dir excl ...
- ctfhub技能树—文件上传—无验证
打开靶机 查看页面信息 编写一句话木马 <?php echo "123"; @eval(@$_POST['a']); ?> 上传木马 上传成功,并拿到相对路径地址 查看 ...
- 关于请求接口报4XX错误,给广大前端同胞进行伸冤澄清,请相信它不一定都是前端的错
关于请求接口报4XX错误,给广大前端同胞进行伸冤澄清,请相信它不一定都是前端的错 首先确保接口没有写错,参数按照后台要的写,确保自己也没有写错,若页面还是报4xx错误,请站出来大胆的质疑后端,干什么吃 ...
- 使用Swagger2
一.Swagger2是什么? Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务. 优点: 及时性 (接口变更后,能够及时准确地通知相关前后端开 ...
- 华为路由配置IPSec
用该方法配置后用抓包工具抓取的就看不到两个通讯点的IP,而显示的是加密点的IP. 原文:https://www.cnblogs.com/yangyang1988/p/11559819.html
- navicat premium 11.0.17 破解版
下载地址: 链接:https://pan.baidu.com/s/1zBoKRAaQZb2p2weelJpKMQ 提取码:b8dd 一款功能强大的数据库管理工具Navicat Premiu ...