90分TLE求助
查看原帖
90分TLE求助
580202
Siegerkranz_2735楼主2023/2/2 11:29
#include <bits/stdc++.h>
using namespace std;
const int N=5e5+5;
int n,m,s;
vector<int> vec[N];
int depth[N],fa[N];
void dfs(int now,int father){
	depth[now]=depth[father]+1;
	fa[now]=father;
	for(int i=0;i<vec[now].size();i++)if(vec[now][i]!=father)dfs(vec[now][i],now);
}
int main(){
	int x,y;
	cin>>n>>m>>s;
	for (int i=1;i<=n-1;i++){cin>>x>>y;vec[x].push_back(y),vec[y].push_back(x);}
	dfs(s,0);
	for(;m--;cout<<x<<endl)
	{
		cin>>x>>y;
		if(depth[x]<depth[y])swap(x,y);
		for(;depth[x]>depth[y];x=fa[x]);
		for(;x!=y;x=fa[x],y=fa[y]);
	}
	return 0;
}

Subtask #0 #12

Subtask #1 所有

2023/2/2 11:29
加载中...