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 ...
随机推荐
- python进阶之生成器
迭代器 什么叫迭代 可以被for循环的就说明他们是可迭代的,比如:字符串,列表,字典,元祖,们都可以for循环获取里面的数据 下面我们看一个代码: number = 12345 for i in nu ...
- P4014 分配问题 网络流
题目描述 有 nn 件工作要分配给 nn 个人做.第 ii 个人做第 jj 件工作产生的效益为 c_{ij}cij .试设计一个将 nn 件工作分配给 nn个人做的分配方案,使产生的总效益最大. 输 ...
- Web后台快速开发框架(.NET Core)
Web后台快速开发框架(.NET Core) Coldairarrow 目录 目录 第1章 目录 1 第2章 简介 3 第3章 基础准备 4 3.1 开发环境 ...
- jeecg入门操作—树型表单开发
树表类型表单 表单创建,基础配置如下: 1.设置表单类型为:单表; 2.是否树选择:是; 3.设置特殊字段:[树形表单父id][树开表单列] 结果测试
- c提高第四课
1.一维数组的初始化 , , }; //3个元素 ] = { , , }; //a[3], a[4]自动初始化为0 ] = { }; //全部元素初始化为0 memset(c, , sizeof(c) ...
- Win10开机“提示语音”以及”随机播放音乐”
1.在桌面建立一个.txt文件 2.把一下代码拷贝进去: RandomizeCreateObject("SAPI.SpVoice").Speak"先生,为你播放一首mus ...
- BZOJ1112[POI2008]砖块Klo——非旋转treap
题目描述 N柱砖,希望有连续K柱的高度是一样的. 你可以选择以下两个动作 1:从某柱砖的顶端拿一块砖出来,丢掉不要了. 2:从仓库中拿出一块砖,放到另一柱.仓库无限大. 现在希望用最小次数的动作完成任 ...
- antd Tree组件中,自定义右键菜单
最近项目中,有一个需求是自定义antd的Tree组件的右键菜单功能. 直接上代码 class Demo extends Component { state = { rightClickNodeTree ...
- laravel 配置MySQL读写分离
前言:说到应对大流量.高并发的解决方案的时候,总会有这样的回答,如:读写分离,主从复制...等,数据库层今天先不讨论,那么今天我们就来看看怎么在应用层实现读写分离. 框架:laravel5.7(所有配 ...
- (数组) leetcode 66. Plus One
Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The ...