2026.9.29

· · 个人记录

学习内容

  1. DFS 序和欧拉序(负责造课件)
  2. LCA (朴素,倍增,Tarjan,RMQ)
  3. 树上前缀和 & 差分(负责讲+造课件)

    原理

    DFS 序

    定义

    DFS 序是指从根节点开始深度优先遍历(DFS)的访问顺序,也可以当做每个点访问的时间戳。

    求法

    进行一遍 DFS,每到一个节点进行一次记录就行了。

    欧拉序1

    求法

    先正常往下遍历,进入节点就记录,遍历完一棵子树后,回来再记录,这样会产生 2n-1 个节点。

    欧拉序2

    求法

    进入的时候就记录一次,遍历完所有子树后,出节点再记录。每个节点会在欧拉序种出现两次,节点个数为 2n。

    朴素 LCA

    本质上是 x 一个一个跳一直跳到和 y 同一层,再一起网上跳跳到同一个位置。

    倍增 LCA

    把朴素 LCA 的跳法换成倍增跳。

    Tarjan LCA(不常用)

  4. 首先接受输入边(邻接链表)、查询边(存储在另一个邻接链表内).查询边其实是虚拟加上去的边,为了方便,每次输入查询边的时候,将这个边及其反向边都加入到 queryEdge 数组里
  5. 然后对其进行一次 DFS 遍历,同时使用 visited 数组进行记录某个结点是否被访问过、parent 记录当前结点的父亲结点
  6. 其中涉及到了 回溯思想,我们每次遍历到某个结点的时候,认为这个结点的根结点就是它本身.让以这个结点为根节点的 DFS 全部遍历完毕了以后,再将这个结点的根节点设置为这个结点的父一级结点
  7. 回溯的时候,如果以该节点为起点,queryEdge 查询边的另一个结点也恰好访问过了,则直接更新查询边的 LCA 结果
  8. 最后输出结果

    欧拉序+RMQ LCA

    首先跑一遍欧拉序,接着找到 x 和 y 的位置,再求出深度最小的位置就行了。

    树上前缀和

    点前缀和

距离公式为 sum_u + sum_v - sum_{\operatorname{lca}(u,v)} - sum_{f_{\operatorname{lca}(u,v)}}。

边前缀和

距离公式为 $sum_u + sum_v - 2\times sum_{\operatorname{lca}(u,v)}$。 ### 树上差分 边差分可以下放权值到节点上变成点差分。 - 叶子结点:差分值等于本身。 - 其他结点:父权减子权和。 我们如果需要将 $u$ 到 $v$ 全部加 $k$,只需要 `d[s] += k, d[t] += k, d[lca] -= k, d[fa[lca]]-= k` 就行了。 ## 题目 ### 老师布置 - [P3379 【模板】最近公共祖先(LCA)](https://www.luogu.com.cn/problem/P3379) - [P5002 专心OI - 找祖先](https://www.luogu.com.cn/problem/P5002) - [P5836 [USACO19DEC] Milk Visits S](https://www.luogu.com.cn/problem/P5836) - [P8855 [POI 2002 R1] 商务旅行](https://www.luogu.com.cn/problem/P8855) - [P3398 仓鼠找 sugar](https://www.luogu.com.cn/problem/P3398) - [P16161 [ICPC 2016 NAIPC] Tourists](https://www.luogu.com.cn/problem/P16161) - [P3833 [SHOI2012] 魔法树](https://www.luogu.com.cn/problem/P3833) - [P3320 [SDOI2015] 寻宝游戏](https://www.luogu.com.cn/problem/P3320) - [P2495 【模板】虚树 / [SDOI2011] 消耗战](https://www.luogu.com.cn/problem/P2495) - [P2305 [NOI2014] 购票](https://www.luogu.com.cn/problem/P2305) - [P1852 [国家集训队] 跳跳棋](https://www.luogu.com.cn/problem/P1852) - [CF1210C Kamil and Making a Stream](https://www.luogu.com.cn/problem/CF1210C) - [P2680 [NOIP 2015 提高组] 运输计划](https://www.luogu.com.cn/problem/P2680) - [P4281 [AHOI2008] 紧急集合 / 聚会](https://www.luogu.com.cn/problem/P4281) - [CF916E Jamie and Tree](https://www.luogu.com.cn/problem/CF916E) - [P2486 [SDOI2011] 染色](https://www.luogu.com.cn/problem/P2486) - [P1967 [NOIP 2013 提高组] 货车运输](https://www.luogu.com.cn/problem/P1967) - [P1084 [NOIP 2012 提高组] 疫情控制](https://www.luogu.com.cn/problem/P1084) - [P1600 [NOIP 2016 提高组] 天天爱跑步](https://www.luogu.com.cn/problem/P1600) - [P6869 [COCI 2019/2020 #5] Putovanje](https://www.luogu.com.cn/problem/P6869) - [P3258 [JLOI2014] 松鼠的新家](https://www.luogu.com.cn/problem/P3258) 只做完了魔法树及之前的,松鼠的新家,紧急集合,Kamil and Making a Stream。 ### DFS 序和欧拉序(自己找的) - B2169 树的深度优先遍历序列 - P8578 [CoE R5] So What Do We Do Now - T824664 dfs 序与欧拉序 - CF1006E Military Problem 全部完成 ### 前缀和差分(自己找的) - [ABC138D] Ki - P4427 [BJOI2018] 求和 - P3128 [USACO15DEC] Max Flow P - P3258 [JLOI2014] 松鼠的新家 - CF1076E Vasya and a Tree - P2680 运输计划 - P10931 闇の連鎖 - P8201 生活在树上 - P3252 [JLOI2012] 树 - CF1891F A Growing Tree ## 易错点 前缀和和差分别求错了,LCA 注意时间复杂度,点前缀和和边前缀和公式别搞错。