题意

给定一棵以 \(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)\)。

代码

#include<bits/stdc++.h>
using namespace std;
#define go(u) for(int i=head[u],v=e[i].to;i;i=e[i].last,v=e[i].to)
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define pb push_back
typedef long long LL;
inline int gi(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-48;ch=getchar();}
return x*f;
}
template<typename T>inline bool Max(T &a,T b){return a<b?a=b,1:0;}
template<typename T>inline bool Min(T &a,T b){return b<a?a=b,1:0;}
const int N=1e6 + 7,inf=0x3f3f3f3f;
int n,edc,K;
int head[N],len[N],f[N],g[N];
struct edge {
int last,to;
edge() {} edge(int last,int to):last(last),to(to) {}
} e[N*2];
void Add(int a,int b) {
e[++edc]=edge(head[a],b),head[a]=edc;
e[++edc]=edge(head[b],a),head[b]=edc;
}
void dfs(int u,int fa) {
len[u]=inf;
int fg=1;
go(u)if(v^fa){
fg=0;dfs(v,u);
Min(len[u],len[v]+1);
if(len[v]+1>K) g[v]=0;
g[u]+=g[v];
Max(f[u],f[v]-g[v]);
}
if(fg) len[u]=0,g[u]=f[u]=1;
else f[u]+=g[u];
}
int main() {
n=gi(),K=gi();
rep(i,2,n) Add(i,gi());
dfs(1,0);
printf("%d\n",f[1]);
return 0;
}

[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. 软件工程-XP方法十二个最佳实践

  2. 适用于 Windows VM 的 Azure 示例基础结构演练

    本文是更广泛系列的一部分,当你在 Azure 中构建应用程序基础结构时,这些系列向你提供设计注意事项和准则. 用户可以查看系列中的其他主题. 尽管用户可以在 Azure 中快速地构建出开发/测试环境, ...

  3. mysql中InnoDB表为什么要建议用自增列做主键

    InnoDB引擎表的特点 1.InnoDB引擎表是基于B+树的索引组织表(IOT) 关于B+树 (图片来源于网上) B+ 树的特点: (1)所有关键字都出现在叶子结点的链表中(稠密索引),且链表中的关 ...

  4. 转:C# lock用法

    lock 的目的很明确:就是不想让别人使用这段代码,体现在多线程情况下,只允许当前线程执行该代码区域,其他线程等待直到该线程执行结束:这样可以多线程避免同时使用某一方法造成数据混乱. 一般定义如下: ...

  5. 从PFX文件中获取私钥、公钥证书、公钥

    https://blog.csdn.net/ZuoYanYouYan/article/details/77868584 该类具体功能:根据pfx证书得到私钥.根据私钥字节数组获取私钥对象.根据公钥字节 ...

  6. Innodb存储引擎的缓存命中率计算

    数据库的慢查询是我们在生产环境中必须经常检测的,如果慢查询语句过多,说明我们应该增加buffer_pool的大小了.常常检查的指标就是查看缓存命中率是否过低. mysql> show statu ...

  7. Linux命令——磁盘管理

    Linux命令--磁盘管理 命令df 作用:查看已挂载磁盘的总容量.使用容量.剩余容量等 参数:-i,查看inodes的使用状况 参数:-h,使用合适的单位显示(推荐) 命令du 作用:查看某个目录或 ...

  8. PReLU与ReLU

    PReLU激活函数,方法来自于何凯明paper <Delving Deep into Rectifiers:Surpassing Human-Level Performance on Image ...

  9. rlwrap与历史命令

    # yum install rlwrap $ vi .bash_profile alias sqlplus='rlwrap sqlplus'alias rman='rlwrap rman' 或者 l ...

  10. jmeter测试webservice接口

    webservice怎样使用jmeter测试呢? 测试样例url=http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx ...