Codeforces Round #551 (Div. 2) D. Serval and Rooted Tree (树形dp)
题目链接
题意:给你一个有根树,假设有k个叶子节点,你可以给每个叶子节点编个号,要求编号不重复且在1-k以内。然后根据节点的max,minmax,minmax,min信息更新节点的值,要求根节点的值最大。
解法:考虑树形dp,若当前节点是nownownow,我们可以假设编号最大的一些数分布在当前节点的子节点中,显然,若当前节点为叶子节点,那么这个这个编号就是最大的。然后往上回溯,假设当前节点取得是maxmaxmax,那么就从子节点中取最大的数为当前节点的答案,如果取得是minminmin的话,就要把子节点需要的叶子节点加起来,比如,一共有k个叶子,当前节点为nownownow,子节点的答案分别为x1,x2...xm{x_1,x_2...x_m}x1,x2...xm,那么当前节点的答案就应该是sum总的叶子数−∑i=1mxi+1sum_{总的叶子数}-\sum_{i=1}^mx_i+1sum总的叶子数−∑i=1mxi+1,答案直接输入根节点的答案即可
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
const int N = 3e5 +11;
int n,a[N],f[N],cnt,dp[N];
vector<int>v[N];
int su[N];
void get(int now){
if(!v[now].size()){su[now]=1;return ;}
int sum=0;
for(int k:v[now]){
get(k);
sum+=su[k];
}
su[now]=sum;
return ;
}
void dfs(int now){
if(!v[now].size()){dp[now]=cnt;return ;}
if(a[now]){
int ans=0;
for(int k:v[now]){
dfs(k);
ans=max(ans,dp[k]);
}
dp[now]=ans;return;
}else{
int ans=0;
for(int k:v[now]){
dfs(k);
ans+=cnt-dp[k]+1;
}
dp[now]=cnt-ans+1;return ;
}
}
int main(){
ios::sync_with_stdio(false);
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=2;i<=n;i++){cin>>f[i],v[f[i]].pb(i);}
for(int i=1;i<=n;i++)cnt+=!v[i].size();
dfs(1);
cout<<dp[1];
return 0;
}
Codeforces Round #551 (Div. 2) D. Serval and Rooted Tree (树形dp)的更多相关文章
- Codeforces Round #551 (Div. 2) D. Serval and Rooted Tree (树形dp)
题目:http://codeforces.com/contest/1153/problem/D 题意:给你一棵树,每个节点有一个操作,0代表取子节点中最小的那个值,1代表取子节点中最大的值,叶子节点的 ...
- Codeforces Round #551 (Div. 2) F. Serval and Bonus Problem (DP/FFT)
yyb大佬的博客 这线段期望好神啊... 还有O(nlogn)FFTO(nlogn)FFTO(nlogn)FFT的做法 Freopen大佬的博客 本蒟蒻只会O(n2)O(n^2)O(n2) CODE ...
- Codeforces Round #384 (Div. 2)D - Chloe and pleasant prizes 树形dp
D - Chloe and pleasant prizes 链接 http://codeforces.com/contest/743/problem/D 题面 Generous sponsors of ...
- Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形dp)
http://codeforces.com/contest/816/problem/E 题意: 去超市买东西,共有m块钱,每件商品有优惠卷可用,前提是xi商品的优惠券被用.问最多能买多少件商品? 思路 ...
- Codeforces Round #551 (Div. 2)A. Serval and Bus
A. Serval and Bus time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces Round #551 (Div. 2)B. Serval and Toy Bricks
B. Serval and Toy Bricks time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Codeforces Round #551 (Div. 2) E. Serval and Snake (交互题)
人生第一次交互题ac! 其实比较水 容易发现如果查询的矩阵里面包含一个端点,得到的值是奇数:否则是偶数. 所以只要花2*n次查询每一行和每一列,找出其中查询答案为奇数的行和列,就表示这一行有一个端点. ...
- Codeforces Round #564 (Div. 2) D. Nauuo and Circle(树形DP)
D. Nauuo and Circle •参考资料 [1]:https://www.cnblogs.com/wyxdrqc/p/10990378.html •题意 给出你一个包含 n 个点的树,这 n ...
- Codeforces Round #343 (Div. 2) E. Famil Door and Roads (树形dp,lca)
Famil Door's City map looks like a tree (undirected connected acyclic graph) so other people call it ...
随机推荐
- Jquery消息提示插件toastr使用详解
toastr是一个基于jQuery简单.漂亮的消息提示插件,使用简单.方便,可以根据设置的超时时间自动消失. 1.使用很简单,首选引入toastr的js.css文件 html <script s ...
- WPF 控件之 Popup
1.经常使用属性说明 IsOpen: 布尔值,指示 Popup 控件是否显示 StaysOpen: 布尔值,指示在 Popup 控件失去焦点的时候,是否关闭 Popup 控件的显示 PopupAnim ...
- python icmp\dns\http监控网络各个节点状态,并记录日志
配置文件如下:支持多节点: { "dns":[{"domainname":"www.baidu.com","dnsserver&q ...
- selenium3(java)之屏幕截图操作
selenium提供了截图的功能,分别是接口是TakesScreenshot和类RemoteWebDriver.该功能是在运行测试用例的过程中,需要验证某个元素的状态或者显示的数值时,可以将屏幕截取下 ...
- Centos7 IPv6 Route And Dhcpv6 Server(借鉴补充)
软件:radvd.dhcp 1)启用ipv6 vi /etc/sysctl.conf net.ipv6.conf.all.disable_ipv6 = 0net.ipv6.conf.default.d ...
- [转帖]EXPDP dumpfile和parallel的关系
http://blog.itpub.net/28602568/viewspace-2133375/ 转帖 EXPDP 里面 parallel 与 dumpfile 里面的文件数的关系. 但是我这里有一 ...
- Manifest merger failed : Attribute application@icon value=(@mipmap/ic_launcher) from AndroidManifest
情况是这样子的,导入一个比较老的项目(两年前),它依赖于一个 Libraray,已经先导入了 library,现在导入项目的时候出了错 (1) Android Studio 目前提供将 SDK包成 . ...
- dubbo+zookeeper+jenkins从打包开始
一.jenkins中maven构建 有如下图构建设置 解释说明: pom.xml 此处jenkins打包依赖的主要配置文件(规则) settings.xml 全局配置文件,主要用于配置maven的运行 ...
- Azure DevOps to Azure AppServices
Azure DevOps is a complete solution for software development, from planning to building to deploymen ...
- python 学习二
什么是JSON,JSON 是一种轻量级的数据格式,其实就是字符串 把字符串转换为字典用 json.loads() import jsons = '{"aa":1,"bb& ...