记录编号 608202 评测结果 TTTTTTTTTTTTTTTTTTTT
题目名称 3919.坡伊踹 最终得分 0
用户昵称 Gravatar我常常追忆未来 是否通过 未通过
代码语言 C++ 运行时间 80.012 s
提交时间 2025-10-24 12:21:39 内存使用 25.48 MiB
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+7; 
struct edge{
	int to,val;
};
vector<edge>g[N];
vector<int>G[N]; 
int n,q,siz[N],dep[N],f[N],top[N],id[N],tot,a[N],at[N],to_root[N],son[N];
void dfs1(int u,int fa){
	siz[u]=1;
	dep[u]=dep[fa]+1;
	f[u]=fa;
	for(auto v:G[u]){
		if(v!=fa){
			dfs1(v,u);
			siz[u]+=siz[v];
			if(!son[u]||siz[son[u]]<siz[v]){
				son[u]=v;
			}
		}
	}
} 
void dfs2(int u,int topu){
	id[u]=++tot;
	at[tot]=a[u]; 
	top[u]=topu;
	if(!son[u]){
		return;
	}
	dfs2(son[u],topu);
	for(auto v:G[u]){
		if(v!=f[u]&&v!=son[u]){
			dfs2(v,v);
		}
	}
}
void dfs3(int u,int fa){
	for(auto vv:g[u]){
		int v=vv.to,val=vv.val;
		if(v!=fa){
			to_root[v]=to_root[u]+val;
			dfs3(v,u);
		}
	}
}
int lca(int x,int y){
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]]){
			swap(x,y);
		}
		x=f[top[x]];
	}
	if(dep[x]>dep[y]){
		swap(x,y);
	}
	return x;
} 
int main(){
	freopen("poitry.in","r",stdin); 
	freopen("poitry.out","w",stdout);
	cin>>n>>q;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	} 
	for(int i=1;i<n;i++){
		int u,v,w;
		cin>>u>>v>>w;
		G[u].push_back(v);
		G[v].push_back(u);
		g[u].push_back({v,w});
		g[v].push_back({u,w});
	} 
	dfs1(1,0);
	dfs2(1,1);
	dfs3(1,0);
	while(q--){
		int a,b;
		cin>>a>>b;
		for(int i=1;i<=N;i++){
			for(int j=1;j<=N;j++){
				for(int k=1;k<=N;k++){
					cout<<lca(a,b)<<" \n";
				}
			}
		}
	}
	

	return 0;
}