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 ...
随机推荐
- 仿新浪游戏频道js多栏目全屏下拉菜单导航条
仿新浪游戏频道js多栏目全屏下拉菜单导航条,新浪,游戏频道,js组件,多栏目,全屏下拉,下拉菜单,导航条.代码下载地址:http://www.huiyi8.com/sc/26765.html更多请访问 ...
- 英语发音规则---字母组合ou的发音规律
英语发音规则---字母组合ou的发音规律 一.总结 一句话总结: 1.先练习一下题,单词enough划线部分与下列那个单词划线部分读音相同:A. touch B. mouth C. soul D ...
- node.js+express+jade系列二:rotue路由的配置
页面的访问最常见的是get和post两种,无论是get请求还是post请求express自动判断执行app.get或app.post 1:app.get(名称,路径)或app["get&qu ...
- Sharepoint list webpart
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.0.min.js&qu ...
- codeforces 637A A. Voting for Photos(水题)
题目链接: A. Voting for Photos time limit per test 1 second memory limit per test 256 megabytes input st ...
- 【leetcode刷题笔记】Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- 每天一个linux命令(7):rmdir命令
版权声明更新:2017-05-11博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下面的rmdir命令. ...
- NSDictionary 用法
//Dictionary //不可变 //字典里面:是以键值对存放的 //字典存放顺序是无序的 //字典里面:value可以重复,但key不能重复 //字典里面:key一般用字符串表示,value可用 ...
- POJ 2017 No Brainer(超级水题)
一.Description Zombies love to eat brains. Yum. Input The first line contains a single integer n indi ...
- Python:更改字典的key
思路:先删除原键值对,保存值,然后以新键插入字典 格式:dict[newkey] = dict.pop(key) d = {'a':1, 'aa':11} d['b'] = d.pop('a') d[ ...