#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)的更多相关文章

  1. Codeforces Round #474-E(树形dp)

    一.题目链接 http://codeforces.com/contest/960/problem/B 二.题意 给定一棵$N$个节点的树,每个节点的权值$V$.定义树中两点$u_1$和$u_m$的权值 ...

  2. codeforces Educational Codeforces Round 16-E(DP)

    题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...

  3. Educational Codeforces Round 60 D dp + 矩阵快速幂

    https://codeforces.com/contest/1117/problem/D 题意 有n个特殊宝石(n<=1e18),每个特殊宝石可以分解成m个普通宝石(m<=100),问组 ...

  4. Educational Codeforces Round 26 D dp

    D. Round Subset time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  5. Educational Codeforces Round 15 A dp

    A. Maximum Increase time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Educational Codeforces Round 57D(DP,思维)

    #include<bits/stdc++.h>using namespace std;char s[100007];long long a[100007];long long dp[100 ...

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

  8. Educational Codeforces Round 53 E. Segment Sum(数位DP)

    Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...

  9. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

随机推荐

  1. jsp学习(3)

    1.model模式 2.model1.x分层模式 3.mvc模式(model2) model2的具体体现就是mvc. dao:就是data access object数据访问对象,即专门对数据库进行操 ...

  2. ajax技术返回json如何处理

    json只是一种文本字符串. Smarty是一个使用PHP写出来的模板引擎. ajax如何处理json数据格式 ①json的格式如下: "{属性名:属性值,属性名:属性值}". 因 ...

  3. 分享知识-快乐自己:HBase编程

    HBase编程: 一):大数据(hadoop)初始化环境搭建 二):大数据(hadoop)环境搭建 三):运行wordcount案例 四):揭秘HDFS 五):揭秘MapReduce 六):揭秘HBa ...

  4. js的trim方法(转)

    写成类的方法格式如下:(str.trim();) <script language="javascript"> String.prototype.trim=functi ...

  5. MyBaties--Mapper configuration

    method one: <!-- Using classpath relative resources --> <mappers> <mapper resource=&q ...

  6. google android sdk下载hoosts

    203.208.46.146 www.google.com #这行是为了方便打开Android开发官网 现在好像不VPN也可以打开 74.125.113.121 developer.android.c ...

  7. bzoj 2648 SJY摆棋子 —— K-D树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2648 学习资料:https://blog.csdn.net/zhl30041839/arti ...

  8. JQUERYUI 框架 http://jqueryui.com/

    http://jqueryui.com/

  9. HDOJ1272(并查集,判断是否为树)

    0 0 Yes 1 1 0 0 Yes 1 2 2 1 0 0 No //自回路不算一条边的! 居然有 0 0 这样的测试数据 #include<iostream> #include< ...

  10. 问题7:如何实现用户的历史记录功能(最多n条)

    实例:制作猜字游戏,添加历史记录功能,显示用户最近猜过的数字 解决方案:使用容量为n的队列存储历史记录 使用标准库colections中的deque,一个双端循环队列 程序退出前,可以使用pickle ...