CF1881F 题解
shinzAnmono
·
·
题解
看到题目,可以发现比较像 dp。
设 f_u 表示 u 到 u 子树里的关键点的距离最大值,g_u 表示 u 到 u 子树外的关键的的距离最大值。
可以得出状态转移方程。
f_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.
g_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)。