标签:inline names alc amp str namespace stream 就是 name
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define ll long long
using namespace std;
inline int read(){
int x=0,o=1;char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')o=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*o;
}
const int N=500005;
int n,m,dep[N],f[N][21],dis[N];
int tot,head[N],nxt[N<<1],to[N<<1];
inline void add(int a,int b){
nxt[++tot]=head[a];head[a]=tot;to[tot]=b;
}
inline void dfs(int u,int fa){
for(int j=1;j<=20;++j)
f[u][j]=f[f[u][j-1]][j-1];
for(int i=head[u];i;i=nxt[i]){
int v=to[i];if(v==fa)continue;
f[v][0]=u;dis[v]=dis[u]+1;
dep[v]=dep[u]+1;dfs(v,u);
}
}
inline int LCA(int x,int y){
if(dep[x]<dep[y])swap(x,y);
for(int j=20;j>=0;--j)
if(dep[f[x][j]]>=dep[y])x=f[x][j];
if(x==y)return x;
for(int j=20;j>=0;--j)
if(f[x][j]!=f[y][j])x=f[x][j],y=f[y][j];
return f[x][0];
}
inline int calc(int x,int y){return dis[x]+dis[y]-2*dis[LCA(x,y)];}
int main(){
n=read();m=read();
for(int i=1;i<n;++i){
int a=read(),b=read();
add(a,b);add(b,a);
}
dfs(1,0);
while(m--){
int x=read(),y=read(),z=read();
int l1=LCA(x,y),l2=LCA(x,z),l3=LCA(y,z);
if(!l1)l1=1;if(!l2)l2=1;if(!l3)l3=1;
if(l1==l2&&l1==l3){
printf("%d %d\n",l1,calc(x,l1)+calc(y,l1)+calc(z,l1));
continue;
}
if(l1==l2){
printf("%d %d\n",l3,calc(x,l3)+calc(y,l3)+calc(z,l3));
continue;
}
if(l1==l3){
printf("%d %d\n",l2,calc(x,l2)+calc(y,l2)+calc(z,l2));
continue;
}
if(l2==l3){
printf("%d %d\n",l1,calc(x,l1)+calc(y,l1)+calc(z,l1));
continue;
}
}
return 0;
}
标签:inline names alc amp str namespace stream 就是 name
原文地址:https://www.cnblogs.com/PPXppx/p/11594899.html