题目链接

题意:给你一个有根树,假设有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=1m​xi​+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)的更多相关文章

  1. Codeforces Round #551 (Div. 2) D. Serval and Rooted Tree (树形dp)

    题目:http://codeforces.com/contest/1153/problem/D 题意:给你一棵树,每个节点有一个操作,0代表取子节点中最小的那个值,1代表取子节点中最大的值,叶子节点的 ...

  2. 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 ...

  3. 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 ...

  4. Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形dp)

    http://codeforces.com/contest/816/problem/E 题意: 去超市买东西,共有m块钱,每件商品有优惠卷可用,前提是xi商品的优惠券被用.问最多能买多少件商品? 思路 ...

  5. 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 ...

  6. 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 ...

  7. Codeforces Round #551 (Div. 2) E. Serval and Snake (交互题)

    人生第一次交互题ac! 其实比较水 容易发现如果查询的矩阵里面包含一个端点,得到的值是奇数:否则是偶数. 所以只要花2*n次查询每一行和每一列,找出其中查询答案为奇数的行和列,就表示这一行有一个端点. ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. VS2017 community版使用码云(gitee)的一些过程,看图学习,傻瓜式教程

    首先你得有一个gitee账号,VS2017IDE开发工具 第一步,打开VS2017,点击菜单栏上->工具->扩展与更新,如图 然后点击 联机 然后输入 gitee 回车搜索 一定要选择我圈 ...

  2. June 28th. 2018, Week 26th. Thursday

    You cannot change the circumstances but you can change yourself. 既然改变不了环境,那就改变自己. From Jim Rohn. Rec ...

  3. Step by Step Recipe for Securing Kafka with Kerberos

    Short Description: Step by Step Recipe for Securing Kafka with Kerberos. Article I found it is a lit ...

  4. php类注释生成接口文档

    参考地址:https://github.com/itxq/api-doc-php

  5. 网络流 之 P2766 最长不下降子序列问题

    题目描述 «问题描述: 给定正整数序列x1,...,xn . (1)计算其最长不下降子序列的长度s. (2)计算从给定的序列中最多可取出多少个长度为s的不下降子序列. (3)如果允许在取出的序列中多次 ...

  6. subgradients

    目录 定义 上镜图解释 次梯度的存在性 性质 极值 非负数乘 \(\alpha f(x)\) 和,积分,期望 仿射变换 仿梯度 混合函数 应用 Pointwise maximum 上确界 suprem ...

  7. springboot 配置文件

    – Spring Boot使用一个全局的配置文件 • application.properties • application.yml – 配置文件放在src/main/resources目录或者类路 ...

  8. google 跨域解决办法

    --args --disable-web-security --user-data-dir

  9. nodejs fs path

    内容详见我的gitHub: https://github.com/shangyueyue/ssy-utils/tree/master/src/nodejs/fs

  10. 总结idea几个实用的快捷键

    Ctrl+R,替换文本Ctrl+F,查找文本 Ctrl+shit+R,全局替换文本Ctrl+shit+F,全局查找文本 Ctrl+Alt+L,格式化代码Alt+Insert,可以生成构造器/Gette ...