社区讨论

求助,为什么会输出0

P3379【模板】最近公共祖先(LCA)参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@mi6v9jab
此快照首次捕获于
2025/11/20 11:23
4 个月前
此快照最后确认于
2025/11/20 11:23
4 个月前
查看原帖
CPP
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 500005;
const int limit = 20;
int next[maxn<<1],to[maxn<<1],head[maxn],dep[maxn],f[maxn][32],lg[maxn];
//lg为常数优化,用于优化某深度最大可开2的log_2(n)次方 
int n,m,s,cnt,u,v;
void add(int u,int v)
{
	to[++cnt]=v,next[cnt]=head[u],head[u]=cnt;
}
int read(void)
{
	int res=0;
	bool flag=false;
	char ch;
	while ((ch=getchar())>'9'||ch<'0')
		if (ch=='-')flag=true;
	while (ch>='0'&&ch<='9')res=(res<<3)+(res<<1)+ch-'0',ch=getchar();
	if (flag)return ~res+1;
	return res;
}
void Deal (int u,int father)
{
	dep[u]=dep[father]+1;
	f[u][0]=father;
	
	for (int i=1;(i<<1)<=dep[u];i++)
		f[u][i]=f[f[u][i-1]][i-1];
		
	for (int node=head[u];node;node=next[node])
		if (to[node]!=father)Deal (to[node],u);
}
//查询x与y的LCA 
int LCA (int x,int y)
{
	if (dep[x]<dep[y])swap(x,y);
	
	for (int i=lg[dep[x]];i>=0;i--)
		if (dep[f[x][i]]>=dep[y])x=f[x][i];
		
	if (x==y)return y;
	
	for (int i=lg[dep[x]];i>=0;i--)
		if (f[x][i]!=f[y][i])		x=f[x][i],y=f[y][i];
		
	return f[x][0];
}
int main ()
{
	//ios::sync_with_stdio(false);
	n=read(),m=read(),s=read();
	for (int i=1;i<n;i++){
		u=read(),v=read();
		add(u,v),add(v,u);
	}
	
	for (int i=1;i<n;i++)
		lg[i]=lg[i-1]+(1<<lg[i-1]==i);
	
	Deal (s,0);
	
	for (int i=1;i<=m;i++){
		u=read(),v=read();
		printf("%d\n",LCA(u,v));
	}
	return 0;
}

回复

1 条回复,欢迎继续交流。

正在加载回复...