跳到正文

题解

CF1438F 题解

liangbowen约 2 分钟
原文
文章信息与版本

文章信息

发布
2023年7月18日
收录
2026年8月27日
更新
2026年11月8日
篇幅
约 2 分钟
获赞
25
题目
CF1438F

版本

  1. v1当前

problem & blog。

神秘随机题。


众所周知:

(u,v)(u,v) 的 LCA 是所有点 ii 中 dis⁡(u,i)+dis⁡(v,i)+dis⁡(root,i)\operatorname{dis}(u,i)+\operatorname{dis}(v,i)+\operatorname{dis}(\text{root},i) 最小的。

对于一个点 uu,设其有两个子树 T1,T2T_1,T_2,它能作为 LCA 的方案数是 ∣T1∣×∣T2∣×(n−∣T1∣−∣T2∣)|T_1|\times|T_2|\times(n-|T_1|-|T_2|)。

  • ∣T1∣=∣T2∣=2h−depu−1|T_1|=|T_2|=2^{h-dep_u}-1。
  • n−∣T1∣−∣T2∣=(2h−1)−2×(2h−depu−1)=2h−2h−depu+1+1n-|T_1|-|T_2|=(2^h-1)-2\times(2^{h-dep_u}-1)=2^h-2^{h-dep_u+1}+1。
  • ∣T1∣×∣T2∣×(n−∣T1∣−∣T2∣)=(2h−depu−1)2×(2h−2h−depu+1+1)|T_1|\times|T_2|\times(n-|T_1|-|T_2|)=(2^{h-dep_u}-1)^2\times(2^h-2^{h-dep_u+1}+1)。
  • 返回的 LCA 深度为 depudep_u 的方案数为 2depu−1×∣T1∣×∣T2∣×(n−∣T1∣−∣T2∣)=2depu−1×(2h−depu−1)2×(2h−2h−depu+1+1)2^{dep_u-1}\times|T_1|\times|T_2|\times(n-|T_1|-|T_2|)=2^{dep_u-1}\times(2^{h-dep_u}-1)^2\times(2^h-2^{h-dep_u+1}+1)。
  • 返回的 LCA 深度为 depudep_u 的概率为 2depu−1×(2h−depu−1)2×(2h−2h−depu+1+1)(2h−13)\dfrac{2^{dep_u-1}\times(2^{h-dep_u}-1)^2\times(2^h-2^{h-dep_u+1}+1)}{\begin{pmatrix}2^h-1\\3\end{pmatrix}}。

这时我们就可以玩赖的了,扔 desmos 上观察,发现随到 depu=2dep_u=2 的概率比较大,即容易出现 root\text{root} 的儿子。

  • 先用 420420 次乱随,记录每个点的出现次数。
  • 找出最大和次大,它们极大概率就是 root\text{root} 的儿子,记为 u,vu,v。
  • 暴力查询 (u,v,i)(u,v,i),如果 query⁡({u,v,i})=i\operatorname{query}(\{u, v, i\})=i 的话就代表 ii 是根。输出即可。

代码,时间复杂度 O(n)O(n)。

原文
存档评论10 条抓取于