题意

给定一棵以 \(1\) 为根的树,你每次可以选择跳到某个叶子节点,再跳到和他深度差不超过 \(k\) 的祖先。询问最多能够跳到多少个叶子节点。

\(n,k\leq 10^6\) .

分析

  • 最后的决策一定是跳很多叶子然后回到 \(u\) 后向下跳上不来。

  • 发现如果能够跳进 \(u\) 子树再跳回 \(u\),取决于最浅的叶子和 \(u\) 之间的距离是否 \(\leq k\)。

  • 记 \(f_u\) 表示以 \(u\) 为根的子树的最大收益, \(g_u\) 表示跳下去之后回到 \(u\) 的最大收益,\({len}_u\) 表示 \(u\) 的最浅叶子到 \(u\) 的距离。

  • 对于 \(u\) 来说,如果 \({len}_v+1>k\) 那么就不能跳到 \(v\) 的子树再回来了,此时设置 \(g_v=0\).

  • 要选定跳进去后不跳出来的一个子树,这时因为不能考虑 \(g_v\) 的贡献,所以要找一个 \(f_v-g_v\) 最大的子树。

  • 总时间复杂度为 \(O(n)\)。

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define go(u) for(int i=head[u],v=e[i].to;i;i=e[i].last,v=e[i].to)
  4. #define rep(i,a,b) for(int i=a;i<=b;++i)
  5. #define pb push_back
  6. typedef long long LL;
  7. inline int gi(){
  8. int x=0,f=1;char ch=getchar();
  9. while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
  10. while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-48;ch=getchar();}
  11. return x*f;
  12. }
  13. template<typename T>inline bool Max(T &a,T b){return a<b?a=b,1:0;}
  14. template<typename T>inline bool Min(T &a,T b){return b<a?a=b,1:0;}
  15. const int N=1e6 + 7,inf=0x3f3f3f3f;
  16. int n,edc,K;
  17. int head[N],len[N],f[N],g[N];
  18. struct edge {
  19. int last,to;
  20. edge() {} edge(int last,int to):last(last),to(to) {}
  21. } e[N*2];
  22. void Add(int a,int b) {
  23. e[++edc]=edge(head[a],b),head[a]=edc;
  24. e[++edc]=edge(head[b],a),head[b]=edc;
  25. }
  26. void dfs(int u,int fa) {
  27. len[u]=inf;
  28. int fg=1;
  29. go(u)if(v^fa){
  30. fg=0;dfs(v,u);
  31. Min(len[u],len[v]+1);
  32. if(len[v]+1>K) g[v]=0;
  33. g[u]+=g[v];
  34. Max(f[u],f[v]-g[v]);
  35. }
  36. if(fg) len[u]=0,g[u]=f[u]=1;
  37. else f[u]+=g[u];
  38. }
  39. int main() {
  40. n=gi(),K=gi();
  41. rep(i,2,n) Add(i,gi());
  42. dfs(1,0);
  43. printf("%d\n",f[1]);
  44. return 0;
  45. }

[CF1065F]Up and Down the Tree[树dp]的更多相关文章

  1. [CF1060F]Shrinking Tree[树dp+组合计数]

    题意 你有一棵 \(n\) 个点的树,每次会随机选择树上的一条边,将两个端点 \(u,v\) 合并,新编号随机为 \(u,v\).问最后保留的编号分别为 \(1\) 到 \(n\) 的概率. \(n\ ...

  2. Codeforces 161 D. Distance in Tree (树dp)

    题目链接:http://codeforces.com/problemset/problem/161/D 题意: 给你一棵树,问你有多少对点的距离为k. 思路: dp[i][j]表示离i节点距离为j的点 ...

  3. URAL1018 Binary Apple Tree(树dp)

    组队赛的时候的一道题,那个时候想了一下感觉dp不怎么好写呀,现在写了出来,交上去过了,但是我觉得我还是应该WA的呀,因为总感觉dp的不对. #pragma warning(disable:4996) ...

  4. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  5. ural 1039 树dp

    http://acm.timus.ru/problem.aspx?space=1&num=1039 1039. Anniversary Party Time limit: 0.5 second ...

  6. uva 12452 Plants vs. Zombies HD SP (树DP)

    Problem I: Plants vs. Zombies HD Super Pro Plants versus Zombies HD Super Pro is a game played not a ...

  7. CF456D A Lot of Games (字典树+DP)

    D - A Lot of Games CF#260 Div2 D题 CF#260 Div1 B题 Codeforces Round #260 CF455B D. A Lot of Games time ...

  8. HDU4916 Count on the path(树dp??)

    这道题的题意其实有点略晦涩,定义f(a,b)为 minimum of vertices not on the path between vertices a and b. 其实它加一个minimum ...

  9. Codeforces 219D. Choosing Capital for Treeland (树dp)

    题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...

随机推荐

  1. PHP的CURL的POST/GET访问

    本文出至:新太潮流网络博客 /** * [curl以GET的方式访问] * @E-mial wuliqiang_aa@163.com * @TIME 2017-04-07 * @WEB http:// ...

  2. 如何使用 Packer 在 Azure 中创建 Windows 虚拟机映像

    Azure 中的每个虚拟机 (VM) 都是基于定义 Windows 分发和操作系统版本的映像创建的. 映像可以包括预安装的应用程序和配置. Azure 应用商店为最常见的操作系统和应用程序环境提供了许 ...

  3. 准备好要上传到 Azure 的 Windows VHD 或 VHDX

    在将 Windows 虚拟机 (VM) 从本地上传到 Azure 之前,必须准备好虚拟硬盘(VHD 或 VHDX). Azure 仅支持采用 VHD 文件格式且具有固定大小磁盘的第 1 代 VM. V ...

  4. redis之安装与简单使用

    python操作redis: https://www.cnblogs.com/melonjiang/p/5342505.html https://www.jianshu.com/p/2639549be ...

  5. 汇编语言debug命令与指令机器码

    一.debug命令 二.标志信息 -r用这个指令,得到的信息右下角: NV UP EI PL NZ NA PO NC 这些符号代表的就是标志寄存器里常用标志位的值.这个是符号值对应表: 溢出标志OF( ...

  6. Sr Software Engineer - Big Data Team

    Sr Software Engineer - Big Data Team   About UberWe’re changing the way people think about transport ...

  7. malloc,calloc,realloc函数用法,原理及不同解析

    https://blog.csdn.net/lixungogogo/article/details/50887028 一.malloc malloc在MSDN中原型为: void *malloc( s ...

  8. CentOS7 安装 JDK

    安装步骤 1. 将 jdk 安装包传到 用户 centos 的 home 目录 (/home/centos) 通过 Xftp 将安装包( jdk-8u161-linux-x64.tar.gz )传到用 ...

  9. 【转】Java学习---10个测试框架介绍

    [原文]https://www.toutiao.com/i6594302925458113027/ JAVA 程序员需要用到 10 个测试框架和库 Java 程序员需要用到十大单元测试和自动化集成测试 ...

  10. Coursera-AndrewNg(吴恩达)机器学习笔记——第三周编程作业(逻辑回归)

    一. 逻辑回归 1.背景:使用逻辑回归预测学生是否会被大学录取. 2.首先对数据进行可视化,代码如下: pos = find(y==); %找到通过学生的序号向量 neg = find(y==); % ...