求助,RMQ做法 求调 subtask1的1,2 wa了
查看原帖
求助,RMQ做法 求调 subtask1的1,2 wa了
622542
liyuan2021楼主2022/11/19 11:05

求助,RMQ做法 求调

#include <bits/stdc++.h>
using namespace std;
int head[510500],cnt=1,n,m,s,frist[510500],dep[1100500],tot=1,ver[1100500];
bool vis[510500];
struct node{
	int to,next,vale;
}edge [1100500];
struct stt{
	int long vale,dep;
	friend bool operator >(stt a,stt b){
		return a.dep>b.dep;
	}
	friend bool operator <(stt a,stt b){
		return a.dep<b.dep;
	}
}st[1100500][21];
void add(int a,int b,int c){
	edge[cnt].to=b;
	edge[cnt].next=head[a];
	edge[cnt].vale=c;
	head[a]=cnt;
	cnt++;
} 

void init(){
	for(int i=1;i<=n*2-1;i++)st[i][0]=(stt){ver[i],dep[i]};
	int t=log(n*2-1)/log(2)+1;
	for(int j=1;j<t;j++){
		for(int i=1;i<=n*2-(1<<(j));i++){
			st[i][j]=min(st[i][j-1],st[i+(1<<(j-1))][j-1]);
		}
	}
}


void dfs(int now,int fa,int de){
	ver[tot]=now;
	vis[now]=1;
	dep[tot]=de;
	frist[now]=tot;
	tot++;
	for(int i=head[now];i!=0;i=edge[i].next){
		if(!vis[edge[i].to]){
			dfs(edge[i].to,now,de+1);
			ver[tot]=now;dep[tot]=de;tot++;
		}
	}
	
}

stt LCA(int a,int b){
	a=frist[a];
	b=frist[b];
	if(a>b)swap(a,b);
	int t=log(b-a+1)/log(2);
	return min(st[a][t],st[b-(1<<(t))+1][t]);
	
}

int main(){
	//freopen("P3379_1.in","r",stdin);
	//freopen("P3379_1.txt","w",stdout);
	memset(st,127,sizeof(st));
	int a,b,c=1; 
	scanf("%d %d %d",&n,&m,&s);
	for(int i=1;i<n;i++){
		scanf("%d %d",&a,&b);
		add(a,b,c);
		add(b,a,c);
	}
	dfs(s,0,1);
	init();
	for(int i=0;i<n;i++){
		scanf("%d %d",&a,&b);
		printf("%d\n",LCA(a,b).vale);
	}
} 
/**

	
	*/

subtask1的1,2过不去,球球,救救我,大佬看看我qwq

2022/11/19 11:05
加载中...