跳到正文

题解

CF1881F 题解

shinzAnmono约 1 分钟
原文
文章信息与版本

文章信息

发布
2023年10月13日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 1 分钟
获赞
3
题目
CF1881F

版本

  1. v1当前

看到题目,可以发现比较像 dp。

设 fuf_u 表示 uu 到 uu 子树里的关键点的距离最大值,gug_u 表示 uu 到 uu 子树外的关键的的距离最大值。

可以得出状态转移方程。

fu={max⁡(0,max⁡v∈sonufv+1)    u is a key pointmax⁡v∈sonufv+1    otherwisef_u=\left\{ \begin{aligned} \max(0,\max_{v\in son_u}f_v+1)&\ \ \ \ \text{u is a key point}\\ \max_{v\in son_u}f_v+1 &\ \ \ \ \text{otherwise} \end{aligned} \right. gu={max⁡(0,gfau+1,max⁡v∈brotherufv+2)    u is a key pointmax⁡(gfau+1,max⁡v∈brotherufv+2)    otherwiseg_u=\left\{ \begin{aligned} \max(0,g_{fa_u}+1,\max_{v\in brother_u}f_v+2)&\ \ \ \ \text{u is a key point}\\ \max(g_{fa_u}+1,\max_{v\in brother_u}f_v+2) &\ \ \ \ \text{otherwise} \end{aligned} \right.

然后根据换根 dp 老套路,记录每个点的兄弟节点,跑两边 DFS 即可,时间复杂度 O(n)O(n)。

原文
存档评论1 条抓取于