算法:求树的最长链。两边Bfs即可,没有难度。不过出于练手,写了一个非递归的Dfs程序,仅供参考。
Program POJ1985;//By_Poetshy
Const
maxn=40005;
Var
pre,other,last,data :Array[1..maxn*2]of Longint;
i,j,k,m,n :Longint;
l,r,d,ans :Longint;
f :Array[1..2,1..maxn]of Longint;
v :Array[1..maxn]of Boolean;
Procedure Dfs;
var stack,j,k:Array[1..maxn]of Longint;
sz,re:Longint;
begin
sz:=1;stack[sz]:=1;j[sz]:=last[stack[sz]];v[1]:=true;
while sz>0 do
begin
re:=sz;
while j[sz]<>0 do
begin
k[sz]:=other[j[sz]];
if not v[k[sz]] then
begin
v[k[sz]]:=true;
inc(sz);j[sz]:=last[k[sz-1]];
stack[sz]:=k[sz-1];
break;
end;
j[sz]:=pre[j[sz]];
end;
if sz=re then
begin
if sz=1 then
begin
dec(sz);
break;