在线算法ST算法
自学过程参考链接
https://blog.csdn.net/liangzhaoyang1/article/details/52549822
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
using namespace std;
const int N = 500100;
struct node
{
int v,next;
}t[N * 2];
int tot,cnt,n,m,s,x,y;
int head[2 * N],rmq[2 * N],dp[2 * N][22];
int F[2 * N],P[2 * N];
void add(int u,int v)
{
t[tot].v = v;
t[tot].next = head[u];
head[u] = tot++;
}
void dfs(int x,int fa,int dep)
{
F[++cnt] = x;
rmq[cnt] = dep;
P[x] = cnt;
for(int i = head[x];i != -1; i = t[i].next)
{
if(t[i].v == fa) continue;
dfs(t[i].v,x,dep + 1);
F[++cnt] = x;
rmq[cnt] = dep;
}
}
void init(int n)
{
for(int i = 1;i <= n; ++i) dp[i][0] = i;
int k = trunc(log2(n));
for(int j = 1;j <= k; ++j)
for(int i = 1;i + (1 << j) - 1 <= n; ++i)
{
if(rmq[dp[i][j - 1]] < rmq[dp[i + (1 << (j - 1))][j - 1]])
dp[i][j] = dp[i][j - 1];
else dp[i][j] = dp[i + (1 << (j - 1))][j - 1];
}
}
int lca(int a,int b)
{
if(a > b) swap(a,b);
int k = trunc(log2(b - a + 1));
return rmq[dp[a][k]] <= rmq[dp[b - (1 << k) + 1][k]] ?
dp[a][k]:dp[b - (1 << k) + 1][k];
}
int main()
{
scanf("%d%d%d",&n,&m,&s);
tot = 0;
memset(head,-1,sizeof(head));
for(int i = 1;i < n; ++i)
{
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
cnt = 0;
dfs(s,s,0);
init(2 * n - 1);
for(int i = 0;i < m; ++i)
{
scanf("%d%d",&x,&y);
printf("%d\n",F[lca(P[x],P[y])]);
}
return 0;
}
倍增法