Educational Codeforces Round 52F(树形DP,VECTOR)
#include<bits/stdc++.h>
using namespace std;
int n,k;
vector<int>son[1000007];
int dp[1000007],depth[1000007],ans[1000007];//dp【i】表示离i最近的叶子节点距离i的深度,depth【i】表示以i为根,回到i所能到达的叶子节点的数量,ans【i】表示以i为根,能到达的叶子节点数目最大,即题意所需
void dfs(int now){
if(!son[now].size()){//本身为叶子结点
depth[now]=0;
dp[now]=1;
return;
}
int mn=1e9,mx=0;
for(const int&tmp:son[now]){//遍历孩子结点
dfs(tmp);//继续深搜
if(depth[tmp]<k)//小于k的话从now向下走可以走到孩子结点tmp所能触及的叶子结点
dp[now]+=dp[tmp];//把孩子能碰到的叶子向上传递给父亲
mx=max(mx,ans[tmp]-(depth[tmp]<k?dp[tmp]:0));//depth【tmp】<k时,dp【tmp】已经加到了dp【now】里,把它减掉,mx留下的是最大的一次下去回不来所能碰到的叶子结点数
mn=min(mn,depth[tmp]+1);//now的深度为最小的孩子深度+1
}
depth[now]=mn;//mn只放最小的深度,那些子节点深度过大的都碰不到,只会碰一次符合题意的叶子结点(这次下去了就回不到祖先节点(这一次dfs的参数)了)
ans[now]=dp[now]+mx;//mx只能加一个所以放在循环之外
}
int main(){
scanf("%d%d",&n,&k);
int x;
for(int i=2;i<=n;i++){
scanf("%d",&x);
son[x].push_back(i);
}
dfs(1);
printf("%d\n",ans[1]);
return 0;
}
//可以用数组模拟链表头插法,遍历链表的方式解决,思路相同,储存方式不同而已
Educational Codeforces Round 52F(树形DP,VECTOR)的更多相关文章
- Codeforces Round #474-E(树形dp)
一.题目链接 http://codeforces.com/contest/960/problem/B 二.题意 给定一棵$N$个节点的树,每个节点的权值$V$.定义树中两点$u_1$和$u_m$的权值 ...
- codeforces Educational Codeforces Round 16-E(DP)
题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...
- Educational Codeforces Round 60 D dp + 矩阵快速幂
https://codeforces.com/contest/1117/problem/D 题意 有n个特殊宝石(n<=1e18),每个特殊宝石可以分解成m个普通宝石(m<=100),问组 ...
- Educational Codeforces Round 26 D dp
D. Round Subset time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Educational Codeforces Round 15 A dp
A. Maximum Increase time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Educational Codeforces Round 57D(DP,思维)
#include<bits/stdc++.h>using namespace std;char s[100007];long long a[100007];long long dp[100 ...
- [Educational Codeforces Round 63 ] D. Beautiful Array (思维+DP)
Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds ...
- Educational Codeforces Round 53 E. Segment Sum(数位DP)
Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
随机推荐
- yield生成器的经典案例
如何生成斐波那契數列 斐波那契(Fibonacci)數列是一个非常简单的递归数列,除第一个和第二个数外,任意一个数都可由前两个数相加得到.用计算机程序输出斐波那契數列的前 N 个数是一个非常简单的问题 ...
- Java_io_02_从一个目录拷贝文件到另一个目录下
java从一个目录拷贝文件到另一个目录下 http://www.cnblogs.com/langtianya/p/4857524.html ** * 复制单个文件 * @param oldPath ...
- C语言小程序(八)、统计字母个数
这么简单的程序本不应贴在这里,但每写一篇博客,积分涨10分,距离摆脱千里之外的排名又进一步,相当于刷榜了,哈哈! #include <stdio.h> #include <strin ...
- Mybatis学习--Mapper XML文件
学习笔记,选自Mybatis官方中文文档:http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html#insert_update_and_delete My ...
- BZOJ3033 太鼓达人
3033: 太鼓达人 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 690 Solved: 497[Submit][Status][Discuss] ...
- BZOJ2212:[POI2011]Tree Rotation
浅谈线段树合并:https://www.cnblogs.com/AKMer/p/10251001.html 题目传送门:https://lydsy.com/JudgeOnline/problem.ph ...
- python setuptools安装
执行# python setup.py install 时发生如下错误 Traceback (most recent call last): File "setup.py", li ...
- Poj 2503 Babelfish(Map操作)
一.Description You have just moved from Waterloo to a big city. The people here speak an incomprehens ...
- puppet前端管理工具foreman-proxy bind 127.0.0.1:8443问题解决
最近在玩foreman,发现部署foreman-proxy的时候,总是出现8443bind在127.0.0.1端口,导致无法访问的情况. 如下图: 经过strace -o log.txt bin/sm ...
- Ruby迭代器(Iterator)
简单的讲,一个迭代器就是一个能接受代码块的方法.当初为了进行迭代操作而设置了带块方法,现在很多时候仍然称它为迭带器. 可实际上,早期版本的 Ruby 将使用代码块的方法称为迭代器,因为它们就是被设计来 ...