https://qoj.ac/contest/1287/problem/6735
考虑定一个根,然后把每个点的点权附属在父边权上,让每条边的边权变成一个 pair。
这样,一个符合条件的路径需要满足的条件是:路径内所有边的边权 pair 相同,以及 路径根节点(lca)的颜色符合。
对于当前树上每个边权相同的连通块分开考虑。
对于连通块内不经过连通块根的路径,路径 lca 的颜色一定符合。对于连通块内经过根的路径,lca 的颜色可能符合。需要分这两种情况统计。
这样做每次修改相当于只修改一条边的颜色。
考虑用 LCT 维护每个边权相同的连通块。
一开始以为要直接用 top tree 维护合并每个子树的直径,然而其实不需要。对于一条实链上做 pushup,只需要知道每个点的轻儿子集合中到自己的最长链,就可以算出这条实链的信息,因此每个点用 set 维护这个最长链的集合。
其实 LCT 的部分应该可以用树剖代替,但用 LCT 更直观一点(?
时间复杂度 \(O(n\log^2 n)\).
- Universal Shaanxi Stage 6735 Treeuniversal shaanxi stage 6735 universal stage the 2nd universal qingdao stage the universal stage 6555 good universal contest nanjing stage universal binjiang stage the universal okayama stage the universal shenyang stage the universal northern stage good universal stage hefei the