感觉学斜二进制的效益不大,所以没学。
倍增
倍增方法只在两年前另一个 oj 上打过板子,所以复习一下。
设 表示 往上跳 次的结点。
先 预处理深度 和父节点 ,然后考虑处理 。
枚举 (最大的 满足 ),枚举 ,令 。
证明显然,处理 时 都处理过了, 就是跳两次 等于 。
给我一种后缀数组的感觉说是。
求解 时,先令 (不满足就 ),然后让 往上跳到与 持平。然后一起往上跳,跳到深度最小的不想等的一对点,然后返回父结点即可。
对于怎么跳是好做的。首先我们对一个结点 的所有祖先结点的深度都要考虑到,这个是好做的,因为任何数字都可以拆分成不同的 的幂相加,只需要枚举跳 ()即可。如果满足限制条件就跳,不满足就继续枚举。
跳到与 持平的限制条件就是 ,一起跳的限制条件是 。
对于边界条件 是因为 足够大,缩小一点或者扩大一点都行,但是你需要考虑树的最大深度和 的数据范围。
考虑时间复杂度:
- 预处理为 ;
- 预处理 为 ;
- 单次查询 为 。
#include <bits/stdc++.h>
// #define int long long
#define lint long long
#define endl '\n'
using namespace std;
const int N = 5e5+5;
struct edge{
int ne,to;
}e[N<<1];
int cntE,pre[N];
int n,m,s;
int dep[N];
int f[N][30];
inline void add(int x,int y){
e[++cntE] = (edge){pre[x],y};
pre[x] = cntE;
}
inline void dfs(int x,int fa){
f[x][0] = fa;
dep[x] = dep[fa] + 1;
for(int i = pre[x]; i; i=e[i].ne){
int y = e[i].to;
if(y != fa) dfs(y,x);
}
}
inline void init(){
for(int j = 1; (1<<j) <= n; j++){
for(int i = 1; i <= n; i++)
f[i][j] = f[f[i][j-1]][j-1];
}
}
inline int lca(int x,int y){
if(dep[x] < dep[y]) swap(x,y);
for(int i = 22; i >= 0; i--){
if(dep[f[x][i]] >= dep[y])
x = f[x][i];
}
if(x == y) return x;
for(int i = 22; i >= 0; i--){
if(f[x][i] != f[y][i]){
x = f[x][i];
y = f[y][i];
}
}
return f[x][0];
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
// freopen("app.in","r",stdin);
// freopen("app.out","w",stdout);
cin >> n >> m >> s;
int x,y;
for(int i = 1; i < n; i++){
cin >> x >> y;
add(x,y),add(y,x);
}
dfs(s,0),init();
while(m--){
cin >> x >> y;
cout << lca(x,y) << endl;
}
return 0;
} 树剖
记 为 这条链里深度最小的结点, 为 的 序。
剖分完毕后求解 ,对于 ,如果 ,那么显然在一条链,直接返回 或 更小者即可。
当不相等时,令 更大者往上跳,直到 即可。
当然,可以直接令 更大者往上跳,因为很显然这里就不过多赘述了。
跳的时候直接跳到 即可,因为我们只关心 的值。
考虑时间复杂度:
- 两次 预处理都为 ;
- 单次查询 为 ,但是很难到 ,实际不卡都跑的飞快。
#include <bits/stdc++.h>
// #define int long long
#define lint long long
#define endl '\n'
using namespace std;
const int N = 5e5+5;
struct edge{
int ne,to;
}e[N<<1];
int cntE,pre[N];
int cnt;
int id[N],top[N];
int f[N],dep[N];
int w[N],wson[N];
int n,m,s;
inline void add(int x,int y){
e[++cntE] = (edge){pre[x],y};
pre[x] = cntE;
}
inline void dfs1(int x,int fa){
f[x] = fa,w[x] = 1;
dep[x] = dep[fa] + 1;
for(int i = pre[x]; i; i=e[i].ne){
int y = e[i].to;
if(y != fa){
dfs1(y,x);
w[x] += w[y];
if(w[wson[x]] < w[y])
wson[x] = y;
}
}
}
inline void dfs2(int x,int t){
top[x] = t,id[x] = ++cnt;
if(!wson[x]) return;
dfs2(wson[x],t);
for(int i = pre[x]; i; i=e[i].ne){
int v = e[i].to;
if(v != f[x] && v != wson[x])
dfs2(v,v);
}
}
inline int lca(int x,int y){
while(top[x] != top[y]){
if(dep[top[x]] < dep[top[y]]) swap(x,y);
// if(id[x] < id[y]) swap(x,y);
x = f[top[x]];
}
return id[x]<id[y] ? x : y;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
// freopen("app.in","r",stdin);
// freopen("app.out","w",stdout);
cin >> n >> m >> s;
int x,y;
for(int i = 1; i < n; i++){
cin >> x >> y;
add(x,y),add(y,x);
}
dfs1(s,0);
dfs2(s,s);
while(m--){
cin >> x >> y;
cout << lca(x,y) << endl;
}
return 0;
} 欧拉序
将 搜索回溯过程中经过的所有结点都记录下来,记为 ()。
设 号结点第一次出现的索引为 ,记 ,那么 为 的区间 中深度最小的结点。
即 为 ()中 值最小的结点 。
简易的证明是,设 为 。那么显然 在 这个区间内的,而且显然没有比 深度更小的结点。因为深度更小的结点,要么从 的祖先搜索过来,要么从 回溯上去。这分别在这个区间的左边和右边。
此时问题转化为 问题了,显然是好做的。
不带修改,用 表预处理即可。
考虑时间复杂度:
- 预处理为 ;
- 预处理 表数组 为 ;
- 单次查询 为 。
#include <bits/stdc++.h>
// #define int long long
#define lint long long
#define endl '\n'
using namespace std;
const int N = 5e5+5;
struct edge{
int ne,to;
}e[N<<1];
int cntE,pre[N];
int n,m,s;
int dep[N];
int f[N<<1][30];
int cnt,a[N];
int b[N<<1];
int lg[N<<1];
inline void add(int x,int y){
e[++cntE] = (edge){pre[x],y};
pre[x] = cntE;
}
inline void dfs(int x,int fa){
dep[x] = dep[fa] + 1;
b[++cnt] = x;
a[x] = cnt;
for(int i = pre[x]; i; i=e[i].ne){
int y = e[i].to;
if(y != fa){
dfs(y,x);
b[++cnt] = x;
}
}
}
inline void init(){
lg[0] = -1;
for(int i = 1; i <= cnt; i++)
lg[i] = lg[i>>1]+1;
for(int i = 1; i <= cnt; i++)
f[i][0] = b[i];
for(int j = 1; j <= lg[cnt]; j++){
for(int i = 1; i+(1<<j)-1 <= cnt; i++){
int x = f[i][j-1],y = f[i+(1<<(j-1))][j-1];
f[i][j] = (dep[x] < dep[y] ? x : y);
}
}
}
inline int lca(int x,int y){
int l = a[x],r = a[y];
if(l > r) swap(l,r);
int s = lg[r-l+1];
x = f[l][s],y = f[r-(1<<s)+1][s];
return (dep[x] < dep[y] ? x : y);
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
// freopen("app.in","r",stdin);
// freopen("app.out","w",stdout);
cin >> n >> m >> s;
int x,y;
for(int i = 1; i < n; i++){
cin >> x >> y;
add(x,y),add(y,x);
}
dfs(s,0),init();
while(m--){
cin >> x >> y;
cout << lca(x,y) << endl;
}
return 0;
} 序
设时间戳 表示结点 在 序中的索引。
对于求解 ,不妨先钦定 。
设 ,设结点 为 。
- 不妨先钦定 不为 的祖先,此时 序的区间 里有 的子树和 子树包含 的子结点 的子树。此时显然 的深度最小,那么 的父结点即为答案。在区间 中查找深度最小的结点即可;
- 当 为 的祖先结点时,显然这个区间为一条链,且 的深度最小。那么答案应该为 中深度最小的结点的父亲。对于情况 ,显然 肯定在 这个区间,所以同样适用这个答案。
故答案为 中深度最小的结点的父亲结点。当然 和 相同时要特判。
不带修改,用 表预处理即可。
考虑时间复杂度:
- 预处理为 ;
- 预处理 表数组 为 ;
- 单次查询 为 。
实际上比欧拉序少了一半常数。
#include <bits/stdc++.h>
// #define int long long
#define lint long long
#define endl '\n'
using namespace std;
const int N = 5e5+5;
struct edge{
int ne,to;
}e[N<<1];
int cntE,pre[N];
int n,m,s;
int dep[N];
int fa[N];
int f[N<<1][30];
int cnt,a[N];
int dfn[N],lg[N];
inline void add(int x,int y){
e[++cntE] = (edge){pre[x],y};
pre[x] = cntE;
}
inline void dfs(int x,int father){
dep[x] = dep[father] + 1;
fa[x] = father;
a[++cnt] = x;
dfn[x] = cnt;
for(int i = pre[x]; i; i=e[i].ne){
int y = e[i].to;
if(y != father) dfs(y,x);
}
}
inline void init(){
lg[0] = -1;
for(int i = 1; i <= n; i++)
lg[i] = lg[i>>1]+1;
for(int i = 1; i <= n; i++)
f[i][0] = a[i];
for(int j = 1; j <= lg[n]; j++){
for(int i = 1; i+(1<<j)-1 <= n; i++){
int x = f[i][j-1],y = f[i+(1<<(j-1))][j-1];
f[i][j] = (dep[x] < dep[y] ? x : y);
}
}
}
inline int lca(int x,int y){
int l = dfn[x],r = dfn[y];
if(l > r) swap(l,r);
int s = lg[r-(++l)+1];
x = f[l][s],y = f[r-(1<<s)+1][s];
return (dep[x] < dep[y] ? x : y);
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
// freopen("app.in","r",stdin);
// freopen("app.out","w",stdout);
cin >> n >> m >> s;
int x,y;
for(int i = 1; i < n; i++){
cin >> x >> y;
add(x,y),add(y,x);
}
dfs(s,0),init();
while(m--){
cin >> x >> y;
if(x == y) cout << x << endl;
else cout << fa[lca(x,y)] << endl;
}
return 0;
}