先上我原来的错误的代码
typenode&#61;^link;link&#61;recordnum:int64;next:node;end;varfa:array[0..300000,0..100] of int64;dep:array[0..300000] of int64;nd:array[0..300000] of node;b:array[0..300000] of boolean;dl:array[0..300000] of int64;n,m,maxdep,ans,t1,t2:int64;i:longint;procedure maketree;vart1,t2,head,tail:int64;i,j:longint;p:node;beginfor i:&#61;0 to n dob[i]:&#61;false;for i:&#61;1 to n-1 dobeginread(t1,t2);new(p);p^.num:&#61;t2;p^.next:&#61;nd[t1];nd[t1]:&#61;p;new(p);p^.num:&#61;t1;p^.next:&#61;nd[t2];nd[t2]:&#61;p;end;new(p);head:&#61;1;tail:&#61;1;dl[head]:&#61;1;b[1]:&#61;true;while head<&#61;tail dobeginp:&#61;nd[dl[head]];while p<>nil dobeginif b[p^.num]&#61;false thenbegininc(tail);dl[tail]:&#61;p^.num;fa[p^.num,0]:&#61;dl[head];dep[p^.num]:&#61;dep[dl[head]]&#43;1;b[p^.num]:&#61;true;if dep[p^.num]>maxdep then maxdep:&#61;dep[p^.num];end;p:&#61;p^.next;end;inc(head);end;for j:&#61;1 to trunc(ln(maxdep)/ln(2))&#43;1 dofor i:&#61;0 to n doif dep[i]>&#61;1<
这个写法WA了一个点&#xff0c;答案比标准答案大。
最后发现可能是ln出现了误差&#xff0c;导致结果偏小&#xff0c;使两点无法移到同层。在后面的移动中无论怎样都无法移到同点&#xff0c;使答案比原来大二
改正后的模板
typenode&#61;^link;link&#61;recordnum:int64;next:node;end;varfa:array[0..300000,0..100] of int64;dep:array[0..300000] of int64;nd:array[0..300000] of node;b:array[0..300000] of boolean;dl:array[0..300000] of int64;n,m,maxdep,ans,t1,t2:int64;i:longint;procedure maketree;vart1,t2,head,tail:int64;i,j:longint;p:node;beginfor i:&#61;0 to n dob[i]:&#61;false;for i:&#61;1 to n-1 dobeginread(t1,t2);new(p);p^.num:&#61;t2;p^.next:&#61;nd[t1];nd[t1]:&#61;p;new(p);p^.num:&#61;t1;p^.next:&#61;nd[t2];nd[t2]:&#61;p;end;new(p);head:&#61;1;tail:&#61;1;dl[head]:&#61;1;b[1]:&#61;true;while head<&#61;tail dobeginp:&#61;nd[dl[head]];while p<>nil dobeginif b[p^.num]&#61;false thenbegininc(tail);dl[tail]:&#61;p^.num;fa[p^.num,0]:&#61;dl[head];dep[p^.num]:&#61;dep[dl[head]]&#43;1;b[p^.num]:&#61;true;if dep[p^.num]>maxdep then maxdep:&#61;dep[p^.num];end;p:&#61;p^.next;end;inc(head);end;for j:&#61;1 to trunc(ln(maxdep)/ln(2))&#43;1 dofor i:&#61;0 to n doif dep[i]>&#61;1<