Codeforces Round #551 (Div. 2)D(树形DP)
#define HAVE_STRUCT_TIMESPEC
#include <bits/stdc++.h>
using namespace std;
int val[300007],num[300007],ans=0;
vector<int>v[300007];
void dfs(int u){
if(v[u].size()==0){
num[u]=1;
ans++;
return;
}
int tmp=0;
for(int i=0;i<v[u].size();i++){
int vv=v[u][i];
dfs(vv);
tmp+=num[vv];//u所有子树的所有叶子节点个数
if(val[u]==1)
num[u]=min(num[u],num[vv]);//如果是max的话,要使传递到根节点的叶子个数最少,只需要选某颗子树中最少的叶子个数
else
num[u]=max(num[u],tmp);//如果是min的话,别无选择,只能选子树中叶子节点最多的值
}
}
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>val[i];
if(val[i]==1)
num[i]=1e9;
else
num[i]=0;
}
for(int i=2;i<=n;i++){
int u;
cin>>u;
v[u].push_back(i);
}
dfs(1);
cout<<ans-num[1]+1;
}
/* 另一种较快的代码,本质思路相同
#include<bits/stdc++.h>
using namespace std;
int col[300007];
int dp[300007];
int a[300007];
int head[300007];
int nex[600007];
int to[600007];
int f[300007];
int du[300007];
void dfs(int u,int fa){
if(col[u])
dp[u]=1;
if(a[u]==0){
for(int i=head[u];i;i=nex[i]){
int v=to[i];
if(v==fa)
continue ;
dfs(v,u);
dp[u]+=dp[v];
}
}
else if(!col[u]){
int z=1e9;
for(int i=head[u];i;i=nex[i]){
int v=to[i];
if(v==fa)
continue ;
dfs(v,u);
z=min(z,dp[v]);
}
dp[u]=z;
}
}
int tot;
void add(int u,int v){
nex[++tot]=head[u];//上一条边
head[u]=tot;//最后一条边
to[tot]=v;//连向的点
}
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;++i)
scanf("%d",&a[i]);
for(int i=2;i<=n;++i){
scanf("%d",&f[i]);
add(f[i],i);
add(i,f[i]);
du[i]++;
du[f[i]]++;
}
int ans=0;
for(int i=2;i<=n;++i){
if(du[i]==1)
col[i]=1,ans++;
}
dfs(1,0);
printf("%d",ans-dp[1]+1);
return 0;
}*/
Codeforces Round #551 (Div. 2)D(树形DP)的更多相关文章
- Codeforces Round #551 (Div. 2) D 树形dp
https://codeforces.com/contest/1153/problem/D 题意 一颗有n个点的数,每个点a[i]为0代表取子节点的最小,1代表取最大,然后假设树上存在k个叶子,问你如 ...
- Codeforces Round #530 (Div. 2) F (树形dp+线段树)
F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...
- 【Codeforces】Codeforces Round #551 (Div. 2)
Codeforces Round #551 (Div. 2) 算是放弃颓废决定好好打比赛好好刷题的开始吧 A. Serval and Bus 处理每个巴士最早到站且大于t的时间 #include &l ...
- 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) D. Serval and Rooted Tree (树形dp)
题目链接 题意:给你一个有根树,假设有k个叶子节点,你可以给每个叶子节点编个号,要求编号不重复且在1-k以内.然后根据节点的max,minmax,minmax,min信息更新节点的值,要求根节点的值最 ...
- Codeforces Round #551 (Div. 2) A-E
A. Serval and Bus 算出每辆车会在什么时候上车, 取min即可 #include<cstdio> #include<algorithm> #include< ...
- AIM Tech Round 3 (Div. 1) (构造,树形dp,费用流,概率dp)
B. Recover the String 大意: 求构造01字符串使得子序列00,01,10,11的个数恰好为$a_{00},a_{01},a_{10},a_{11}$ 挺简单的构造, 注意到可以通 ...
- Educational Codeforces Round 67 E.Tree Painting (树形dp)
题目链接 题意:给你一棵无根树,每次你可以选择一个点从白点变成黑点(除第一个点外别的点都要和黑点相邻),变成黑点后可以获得一个权值(白点组成连通块的大小) 问怎么使权值最大 思路:首先,一但根确定了, ...
- 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 ...
随机推荐
- js加密(七)steam登录
1. url: https://store.steampowered.com/login/?redir=&redir_ssl=1 2. target: 登录 3. 分析 3.1 老样子,抓包, ...
- git密码相关问题
一.解决:每次都需要输入账号密码 git config --global credential.helper store 二.后期git密码更改后,重置密码操作 git config --system ...
- SQL常用语句和函数
从一个表中选取数据插入到另一个表中: select column_name(s) into new_table_name from old_table_name --new_table_name表不必 ...
- Idea 工具快捷合集
官方下载地址 https://www.jetbrains.com/idea/download/#section=windows 商业版 与 社区版,商业版具有更多的功能 快捷一.修改 terminal ...
- 5_5 集合栈计算机(UVa12096)<stack与STL其他容器的综合运用>
有一个专门为了集合运算而设计的“集合栈”计算机.该机器有一个初始化为空的栈,并支持以下操作:( 维护 N(1≤N≤2000) 个操作, 可能的五种操作如下:) ■PUSH: 在栈顶加入一个空集合 A= ...
- ZooKeeper技术总结
因为之前学习并使用了Kafka,所以专门查看了有关zookeeper相关的资料,看了大量的博客及官网资料,也因为有些地方理解不清楚向认识的专业人士进行了咨询,这里对这段时间的学习进行总结. ZooKe ...
- 获取天气预报java代码
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; imp ...
- ➡️➡️➡️IELTS speaking by simon
目录 p1 课程概述 p2 speaking part1, intro, warm up introduction questions then 4 questions about one topic ...
- 谈一谈并查集QAQ(上)
最近几日理了理学过的很多oi知识...发现不知不觉就有很多的知识忘记了... 在聊聊并查集的时候顺便当作巩固吧.... 什么是并查集呢? ( Union Find Set ) 是一种用于处理分离集合的 ...
- Update(Stage5):Kudu_javaApi使用_Spark整合
Table of Contents: 2.3. 安装 Zookeeper 2.4. 安装 Hadoop 2.4. 安装 MySQL 2.5. 安装 Hive 2.6. 安装 Kudu 2.7. 安装 ...