正文
增加 54 行删除 14 行
1234567880 行未改动8990919293949596977 行未改动10510610710810911011111211354 行未改动16816917017117217317417517614 行未改动1911921931941951961971981998 行未改动20820921021121221321421521684 行未改动3013023033043053063073083097 行未改动31731831932032132232332432585 行未改动4114124134144154164174184198 行未改动42842943043143243343443543653 行未改动49049149249349449549649749849919 行未改动51952052152252352452552652752852953053153216 行未改动5495505515525535545555565578 行未改动566567568569570571572介绍了 $5$ 种求最近公共祖先的方法。看情况未来添加 tarjin 和 LCT 求 $\text{lca}$ 的笔记。---### 倍增80 行未改动signed main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n >> m >> s;int x,y;for(int i = 1; i < n; i++){7 行未改动cin >> x >> y;cout << lca(x,y) << endl;}return 0;}```::::---54 行未改动}inline void dfs2(int x,int t){top[x] = t,id[x] = ++cnt;if(!wson[x]) return;dfs2(wson[x],t);14 行未改动signed main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n >> m >> s;int x,y;for(int i = 1; i < n; i++){8 行未改动cin >> x >> y;cout << lca(x,y) << endl;}return 0;}```::::---84 行未改动signed main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n >> m >> s;int x,y;for(int i = 1; i < n; i++){7 行未改动cin >> x >> y;cout << lca(x,y) << endl;}return 0;}```::::---85 行未改动signed main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n >> m >> s;int x,y;for(int i = 1; i < n; i++){8 行未改动if(x == y) cout << x << endl;else cout << lca(x,y) << endl;}return 0;}```::::---53 行未改动| $8$ | $101$ | $1$ | 最低有效位不为 $2$,直接进一 || $9$ | $102$ | $1$ | 最低有效位为 $2$,向高位进一 || $10$ | $110$ | $2$ | 最低有效位不为 $2$,直接进一 |我们只关心斜二进制的性质,不关心其是否唯一。::::info[$\texttt{code}$]```cpp#include <bits/stdc++.h>#define lint long long19 行未改动inline void dfs(int x,int father){dep[x] = dep[father] + 1;fa[x] = father;int p = lb[father],q = lb[p];if(dep[father] - dep[p] != dep[p] - dep[q])lb[x] = father;else lb[x] = q;for(int i = pre[x]; i; i=e[i].ne){int y = e[i].to;if(y != father) dfs(y,x);}16 行未改动signed main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n >> m >> s;int x,y;for(int i = 1; i < n; i++){8 行未改动if(x == y) cout << x << endl;else cout << lca(x,y) << endl;}return 0;}```::::1234567891080 行未改动9192939495969798997 行未改动10710810911011111211311411554 行未改动17017117217317417517617717814 行未改动1931941951961971981992002018 行未改动21021121221321421521621721884 行未改动3033043053063073083093103117 行未改动31932032132232332432532632785 行未改动4134144154164174184194204218 行未改动43043143243343443543643743853 行未改动49249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853919 行未改动55956056156256356456556656756856957057157216 行未改动5895905915925935945955965978 行未改动606607608609610611612介绍了 $5$ 种求最近公共祖先的方法。看情况未来添加 tarjin 和 LCT 求 $\text{lca}$ 的笔记。斜二倍增给出详细证明,有笔误欢迎提出。---### 倍增80 行未改动signed main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n >> m >> s;int x,y;for(int i = 1; i < n; i++){7 行未改动cin >> x >> y;cout << lca(x,y) << endl;}return 0;}```::::---54 行未改动}inline void dfs2(int x,int t){top[x] = t,id[x] = ++cnt;if(!wson[x]) return;dfs2(wson[x],t);14 行未改动signed main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n >> m >> s;int x,y;for(int i = 1; i < n; i++){8 行未改动cin >> x >> y;cout << lca(x,y) << endl;}return 0;}```::::---84 行未改动signed main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n >> m >> s;int x,y;for(int i = 1; i < n; i++){7 行未改动cin >> x >> y;cout << lca(x,y) << endl;}return 0;}```::::---85 行未改动signed main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n >> m >> s;int x,y;for(int i = 1; i < n; i++){8 行未改动if(x == y) cout << x << endl;else cout << lca(x,y) << endl;}return 0;}```::::---53 行未改动| $8$ | $101$ | $1$ | 最低有效位不为 $2$,直接进一 || $9$ | $102$ | $1$ | 最低有效位为 $2$,向高位进一 || $10$ | $110$ | $2$ | 最低有效位不为 $2$,直接进一 |我们只关心斜二进制的性质,**不关心其是否唯一**(具体可以看后面的推式子)。此时我们设 $\text{lowbit}(x)$ 表示 $(x)_2'$ 的**最低有效位的位权**。不妨钦定 $g_x$ 表示 $x$ 的 $\text{lowbit}(\text{dep}_x)$ 级祖先,不存在默认为 $0$。因为是 $\text{lowbit}(\text{dep}_x)$ 级祖先,那么深度显然减少了 $\text{lowbit}(\text{dep}_x)$,即 $\text{dep}_x-\text{dep}_{g_x}=\text{lowbit}(\text{dep}_x)$,在斜二进制下看就是对最低有效位减了一。这个时候考虑如何获得 $g_x$。设 $p=g_{f_x}$、$q=g_p$,有且仅有两种情况:1. $(\text{dep}_{f_x})_2'$ 的最低有效位不为 $2$。移项得 $\text{dep}_x=\text{dep}_{g_x}+\text{lowbit}(\text{dep}_x)$。此时 $\text{dep}_{f_x}+1$ 就是在最低位加 $1$ 得到 $\text{dep}_x$,不难发现 $g_x=f_x$ 是满足等式的。因为最低位为一所以 $\text{lowbit}(\text{dep}_x)=1$。$\text{dep}_{f_x}$ 在斜二进制下加一即为最低位加一。故此时 $g_x=f_x$。2. $(\text{dep}_{f_x})_2'$ 的最低有效位为 $2$。此时 $\text{dep}_x=\text{dep}_{f_x}+1$ 所以 $(\text{dep}_x)_2'$ 相对 $(\text{dep}_{f_x})_2'$ 进了一位。移项得 $\text{dep}_x-\text{lowbit}(\text{dep}_x)=\text{dep}_{g_x}$。想要减少一个最低有效位显然等同与 $(\text{dep}_{f_x})_2'$ 直接将最低有效位抹去,相当于减少两次。因为 $(\text{dep}_{f_x})_2'$ 最低有效位为 $2$,所以 $\text{lowbit}(\text{dep}_{f_x})=\text{lowbit}(\text{dep}_p)$。此时 $(\text{dep}_x)_2'$ 最低有效位减一相当于 $(\text{dep}_p)_2'$ 的最低有效位减一即 $q$。所以 $g_x=q$。形象来讲,我们不妨假设 $(\text{dep}_{f_x})_2'=\dots200\dots$。显然 $(\text{dep}_{x})_2'=(\text{dep}_{f_x})_2'+1$ 直接相对 $(\text{dep}_{f_x})_2'$ 进了一位。此时进位后 $(\text{dep}_{f_x})_2'$ 的最低有效位开始想当于抹除了。这和 $(\text{dep}_{f_x})_2'-\text{lowbit}(\text{dep}_{f_x})\times 2$ 是等价的。因为 $\text{lowbit}(\text{dep}_{f_x})=\text{lowbit}(\text{dep}_p)$,我们直接令 $g_x=g_p=q$ 即可。具体来讲,$(\text{dep}_{f_x})_2'$ 的最低有效位为 $2$ 时,$\text{dep}_x-\text{lowbit}(\text{dep}_x)=\text{dep}_{f_x}-\text{lowbit}(\text{dep}_{f_x})\times 2=\text{dep}_p-\text{lowbit}_p=\text{dep}_q$。需要注意的是边界 $f_0=g_0=\text{dep}_0=\text{lowbit}(\text{dep}_0)=0$。现在考虑一下怎么判断 $(\text{dep}_{f_x})_2'$ 的最低有效位是否为 $2$。通过情况 $2$ 的类比,不难发现 $\text{lowbit}(\text{dep}_{f_x})=\text{lowbit}(\text{dep}_p)$ 时满足最低有效位为 $2$。简单代换得 $\text{dep}_{f_x}-\text{dep}_p=\text{dep}_p-\text{dep}_q$。不难发现跳使深度变小,本质上是斜二进制的退位。退位的过程中我们不断使斜二进制从左到右向 $\text{lca}$ 匹配。抽象来说,对于前面左边已经匹配好了若干位,匹配好的后一位比 $\text{lca}$ 大 $0\sim 2$,需要先减 $\text{lowbit}$ 到这一位后面全部肃清,然后再减 $\text{lowbit}$ 到比 $\text{lca}$ 只大 $1$,然后减一使最低有效位降 $1$,此时这一位匹配成功,再考虑后面。显然肃清只需要进行一次,之后开始交互跳 $\text{lowbit}$ 匹配和跳 $1$ 退位至完毕。而肃清和总体交互跳的匹配都是 $O(\log n)$ 的。恕笔者数学没学好,无法给出正式的归纳,只能用自己的语言将这个表述清楚。::::info[$\texttt{code}$]```cpp#include <bits/stdc++.h>#define lint long long19 行未改动inline void dfs(int x,int father){dep[x] = dep[father] + 1;fa[x] = father;int p = lb[father],q = lb[p];if(dep[father] - dep[p] != dep[p] - dep[q])lb[x] = father;else lb[x] = q;for(int i = pre[x]; i; i=e[i].ne){int y = e[i].to;if(y != father) dfs(y,x);}16 行未改动signed main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n >> m >> s;int x,y;for(int i = 1; i < n; i++){8 行未改动if(x == y) cout << x << endl;else cout << lca(x,y) << endl;}return 0;}```::::