标题
增加 1 行删除 1 行
1炒鸡不可爱的树链剖分1树链剖分
正文
增加 2 行删除 7 行
39 行未改动4041424344454647484911 行未改动6162636465666768697077 行未改动14814915015115215315415515645 行未改动202203204205206207208209210211212213214215216217101 行未改动全部展开31932032132232339 行未改动第一次 dfs 记录父节点 $p$,深度 $dep$,子树大小 $siz$ 和重子节点 $hs$第二次 dfs 连接重链,并记录所在链的链顶 $top$,重边优先遍历时的 dfs 序 $dfn$,以及对应的节点编号 $rk$::::success[code]```cppvoid dfs1(int u,int fa){dep[u]=dep[fa]+1,p[u]=fa,siz[u]=1;int mx=-1;for(int i=head[u];i;i=e[i].next){11 行未改动if(v!=hs[u]&&v!=p[u])dfs2(v,v);}}```::::## 一些性质### 性质1:树上每个点属于且仅属于一条重链77 行未改动预处理复杂度 $O(n)$:两次 dfs查询复杂度 $O(\log n)$:跳重链次数等于轻边数,而轻边数小于 $\log_2 n$::::success[code]```cpp#include<bits/stdc++.h>using namespace std;typedef long long ll;45 行未改动while(m--){int u=read(),v=read();printf("%d\n",lca(u,v));}return 0;}```::::## [P3384 【模板】重链剖分 / 树链剖分](https://www.luogu.com.cn/problem/P3384)板题,树剖完用线段树维护链::::success[code]```cpp#include<bits/stdc++.h>using namespace std;typedef long long ll;101 行未改动全部展开return 0;}```::::39 行未改动404142434445464711 行未改动5960616263646566676877 行未改动14614714814915015115215345 行未改动199200201202203204205206207208209210211212213101 行未改动全部展开31531631731839 行未改动第一次 dfs 记录父节点 $p$,深度 $dep$,子树大小 $siz$ 和重子节点 $hs$第二次 dfs 连接重链,并记录所在链的链顶 $top$,重边优先遍历时的 dfs 序 $dfn$,以及对应的节点编号 $rk$```cppvoid dfs1(int u,int fa){dep[u]=dep[fa]+1,p[u]=fa,siz[u]=1;int mx=-1;for(int i=head[u];i;i=e[i].next){11 行未改动if(v!=hs[u]&&v!=p[u])dfs2(v,v);}}```## 一些性质### 性质1:树上每个点属于且仅属于一条重链77 行未改动预处理复杂度 $O(n)$:两次 dfs查询复杂度 $O(\log n)$:跳重链次数等于轻边数,而轻边数小于 $\log_2 n$```cpp#include<bits/stdc++.h>using namespace std;typedef long long ll;45 行未改动while(m--){int u=read(),v=read();printf("%d\n",lca(u,v));}return 0;}```## [P3384 【模板】重链剖分 / 树链剖分](https://www.luogu.com.cn/problem/P3384)板题,树剖完用线段树维护链```cpp#include<bits/stdc++.h>using namespace std;typedef long long ll;101 行未改动全部展开return 0;}```