其实这道题是和bzoj1787一样的
但我用bzoj1787MLE了,于是正好练一下树上倍增
type node=record
po,next:longint;
end; var w:array[..] of node;
anc:array[..,..] of longint;
dep,fa,p:array[..] of longint;
v:array[..] of boolean;
t,len,x,y,z,a,b,c,i,n,m:longint; procedure add(x,y:longint);
begin
inc(len);
w[len].po:=y;
w[len].next:=p[x];
p[x]:=len;
end; procedure swap(var a,b:longint);
var c:longint;
begin
c:=a;
a:=b;
b:=c;
end; procedure dfs(x:longint);
var i,y:longint;
begin
v[x]:=true;
i:=p[x];
while i<> do
begin
y:=w[i].po;
if not v[y] then
begin
fa[y]:=x;
dep[y]:=dep[x]+;
dfs(y);
end;
i:=w[i].next;
end;
end; procedure prework;
var i,j,h:longint;
begin
fillchar(anc,sizeof(anc),);
h:=;
for i:= to n do
begin
anc[i,]:=fa[i];
if dep[i]>h then h:=dep[i];
end;
t:=trunc(ln(h)/ln());
for j:= to t do
for i:= to n do
begin
b:=anc[i,j-];
if anc[b,j-]<>- then anc[i,j]:=anc[b,j-];
end;
end; function lca(x,y:longint):longint;
var i,p:longint;
begin
if dep[x]<dep[y] then swap(x,y);
if dep[x]<>dep[y] then
begin
p:=trunc(ln(dep[x])/ln());
for i:=p downto do
if dep[x]- shl i>=dep[y] then
begin
x:=anc[x,i];
if dep[x]=dep[y] then break;
end;
end; if x=y then exit(y);
p:=trunc(ln(dep[x])/ln());
for i:=p downto do
if (anc[x,i]<>-) and (anc[x,i]<>anc[y,i]) then
begin
x:=anc[x,i];
y:=anc[y,i];
end;
exit(fa[x]);
end; begin
readln(n,m);
for i:= to n- do
begin
readln(x,y);
add(x,y);
add(y,x);
end;
fa[]:=-;
dfs();
prework;
for i:= to m do
begin
readln(x,y,z);
a:=lca(x,y);
b:=lca(x,z);
c:=lca(y,z);
if a=b then
writeln(c,' ',dep[y]+dep[z]-*dep[c]+dep[c]+dep[x]-*dep[lca(x,c)])
else if a=c then
writeln(b,' ',dep[x]+dep[z]-*dep[b]+dep[b]+dep[y]-*dep[lca(y,b)])
else if b=c then
writeln(a,' ',dep[x]+dep[y]-*dep[a]+dep[a]+dep[z]-*dep[lca(z,a)])
end;
end.