不是VIP会员,不能显示答案

题目解答

题目:
求出一棵树的深度和宽度。例如有如下的一棵树:( 4+4+4+4+4=20分)
其树的深度为从根结点开始到叶结点结束的最大深度,树的宽度为同一层上结点数的最大值。在上图中树的深度为4,宽度为3。
用邻接表来表示树,上图中的树的邻接表示如下:
1 2 3 4 0 0
2 0 0 0 0 0
3 5 0 0 0 0
4 6 0 0 0 0
5 0 0 0 0 0
6 7 0 0 0 0
7 0 0 0 0 0
程序清单
program noi00_6;
var i,j,sp1,sp2,l,max:integer; tree:array[1..20,1..6]of integer;
q:array[1..100,0..6] of integer; d:array[0..20]of integer;
begin
for i:=1 to 14 do for j:=1 to 6 do tree[i,j]:=o;
for j:=1 to 14 do tree[j,1]:=j;
tree[1,2]:=2; tree [1,3]:=3; tree[1,4]:=4; tree[2,2]:=5;
tree[2,3]:=6; tree [3,2]:=7; tree[3,3]:=8; tree[4,2]:=9;
tree[4,3]:=10; tree[4,4]:=11; tree[7,2]:=12;
tree[7,3]:=13; tree[13,2]:=14;
sp1:=1; sp2:=1;
for i:=1 to 6 do q[1,i]:=tree[1,i];
q[1,0]:=1;
while sp1<=sp2  do
begin
l:= q[sp1,0]+1; j:=2;
while q[sp1,j]<>0 do
begin
sp2:=sp2+1;q[sp2,0]:=l;q[sp2,1]:=q[sp1,j];
for i:=2 to 6 do
q[sp2,i]:=tree[q[sp1,j],i];
j:=j+1
end;
sp1:=sp1+1
end;
writeln( q[sp2,0] );
for i:=0 to 20 do d[i]:=0;
for i:=1 to sp2 do
d[q[i,0]]:=( d[q[i,0]]+1 );
max:=d[1];
for i:=2 to 20 do
if d[i]>max then max:=d[i];
writeln(max);
readln;
end.
考点:
分析:
解答:
评论:
老师: