struct Lca{
int tot=0;
int dep[N],pos[N],lca[N*2][20],lg[N*2];
void pre(int x,int fa){
dep[x]=dep[fa]+1,pos[x]=++tot,lca[tot][0]=x;
for(int i=h[x];i;i=d[i].n){
int y=d[i].b;
if(y==fa) continue;
pre(y,x);lca[++tot][0]=x;
}
}
int xiao(int x,int y){
if(dep[x]<dep[y]) return x;
return y;
}
void ST(){
lg[0]=-1;
for(int i=1;i<=tot;i++) lg[i]=lg[i/2]+1;
for(int j=1;j<=19;j++){
for(int i=1;i+(1<<j)-1<=tot;i++) lca[i][j]=xiao(lca[i][j-1],lca[i+(1<<(j-1))][j-1]);
}
}
int LCA(int x,int y){
if(pos[x]>pos[y]) swap(x,y);
int len=pos[y]-pos[x]+1;
return xiao(lca[pos[x]][lg[len]],lca[pos[y]-(1<<lg[len])+1][lg[len]]);
}
int dis(int x,int y){
int _lca=LCA(x,y);
return dep[x]+dep[y]-2*dep[_lca];
}
}Tl;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探