#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. Git教程(二)-如何上传和同步自己的git项目

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! Git接触并使用多年, 工作中使用较多,它的分布式存储,使每个人的电脑均为服务器的策略非常棒:再加上 ...

  2. C++中类型转换

    static_cast            静态类型转换. 在编译的时候C++编译器会做类型检查,基本类型能转换,指针类型不进行转换. C语言中隐式类型转换的地方均可以使用static_cast. ...

  3. 51nod 1686 第K大区间2

    1685 第K大区间2 定义一个区间的值为其众数出现的次数.现给出n个数,求将所有区间的值排序后,第K大的值为多少. 众数(统计学/数学名词)_百度百科 Input 第一行两个数n和k(1<=n ...

  4. POJ3417Network

    Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5311   Accepted: 1523 Descripti ...

  5. python setuptools安装

    执行# python setup.py install 时发生如下错误 Traceback (most recent call last): File "setup.py", li ...

  6. Poj 1659 Distance on Chessboard(国际象棋的走子规则)

    一.Description 国际象棋的棋盘是黑白相间的8 * 8的方格,棋子放在格子中间.如下图所示: 王.后.车.象的走子规则如下: 王:横.直.斜都可以走,但每步限走一格. 后:横.直.斜都可以走 ...

  7. 串口编程3:使用串口读取GPS信息

    关于GPS的使用,参考. 本文主要参考的博客,在此表示感谢!!! 主函数 主函数gps_main.c,这里便涉及到了串口的打开,读操作,以及调用了串口设置函数: #include <stdio. ...

  8. 杂项:zabbix(WEB界面的提供分布式系统监视以及网络监视功能)

    ylbtech-杂项:zabbix(WEB界面的提供分布式系统监视以及网络监视功能) zabbix(音同 zæbix)是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案.z ...

  9. C#自定义控件 ————进度条

    先看看样式 一个扇形的进度条 对外公开的方法和属性 事件 value_change;//值改变时触发的事件progress_finshed;//进度条跑完时触发的事件 属性 Max_value//获取 ...

  10. 网站跳转到Apache 2 Test Page powered by CentOS

    原来是80端口被占用的问题 解决80端口占用问题 sudo fuser -n tcp -k 覆盖原来的httpd cp /usr/local/apache2/bin/apachectl /etc/in ...