本题求的是树的重心。树的重心定义为删掉这个节点之后将树分成几部分使得这几部分中点个数的最大值最小。.
{$M 100000000}
Program POJ1655;
Const
maxn=20005;
Var
i,j,k,m,n,t :Longint;
father,size :Array[0..maxn]of Longint;
pre,other,last :Array[0..maxn*2]of Longint;
ans :Array[0..maxn]of Longint;
Procedure Dfs(i:Longint);
var j,k:Longint;
begin
j:=last[i];
size[i]:=1;
while j<>0 do
begin
k:=other[j];
if k<>father[i] then
begin
father[k]:=i;
Dfs(k);
inc(size[i],size[k]);
if size[k]>ans[i] then ans[i]:=size[k];
end;
j:=pre[j];
end;
if n-size[i]>ans[i] then ans[i]:=n-size[i];
end;
BEGIN
readln(t);
while t>0 do
begin
dec(t);k:=0;
readln(n);
fillchar(father,sizeof(father),0);
fillchar(ans,sizeof(ans),255);
fillchar(last,sizeof(last),0);
for i:=1 to n-1 do
begin
readln(m,j);
inc(k);pre[k]:=last[m];last[m]:=k;other[k]:=j;
inc(k);pre[k]:=last[j];last[j]:=k;other[k]:=m;
end;
Dfs(1);
m:=1;
for i:=2 to n do
if ans[m]>ans[i] then m:=i;
writeln(m,' ',ans[m]);
end;
END.