博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LCA倍增算法的错误与模板
阅读量:7072 次
发布时间:2019-06-28

本文共 4305 字,大约阅读时间需要 14 分钟。

先上我原来的错误的代码

type  node=^link;  link=record    num:int64;    next:node;  end;var fa: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; var  t1,t2,head,tail:int64;  i,j:longint;  p:node;   begin         for i:=0 to n do       b[i]:=false;     for i:=1 to n-1 do       begin         read(t1,t2);                    new(p);         p^.num:=t2;p^.next:=nd[t1];nd[t1]:=p;         new(p);         p^.num:=t1;p^.next:=nd[t2];nd[t2]:=p;       end;      new(p);      head:=1;tail:=1;dl[head]:=1;b[1]:=true;       while head<=tail do         begin           p:=nd[dl[head]];           while p<>nil do             begin               if b[p^.num]=false then                 begin                   inc(tail);                   dl[tail]:=p^.num;                   fa[p^.num,0]:=dl[head];                   dep[p^.num]:=dep[dl[head]]+1;                   b[p^.num]:=true;                   if dep[p^.num]>maxdep then maxdep:=dep[p^.num];                 end;               p:=p^.next;             end;           inc(head);         end;      for j:=1 to trunc(ln(maxdep)/ln(2))+1 do        for i:=0 to n do          if dep[i]>=1<
dep[b] then begin t:=a; a:=b; b:=t; end; if dep[a]<>dep[b] then for i:=trunc(ln(dep[b]-dep[a])/ln(2)) downto 0 do if dep[b]-1<
=dep[a] then begin b:=fa[b,i]; ans:=ans+1<
b then for i:=trunc(ln(dep[a])/ln(2)) downto 0 do if fa[a,i]<>fa[b,i] then begin a:=fa[a,i]; b:=fa[b,i]; ans:=ans+1<<(i+1); end; if a<>b then inc(ans,2); end; begin readln(n); if n=1 then begin writeln(0); halt; end; maketree; readln(m); read(t1); for i:=2 to m do begin read(t2); lca(t1,t2); t1:=t2; end; writeln(ans); end.

这个写法WA了一个点,答案比标准答案大。

最后发现可能是ln出现了误差,导致结果偏小,使两点无法移到同层。在后面的移动中无论怎样都无法移到同点,使答案比原来大二

 

改正后的模板

type  node=^link;  link=record    num:int64;    next:node;  end;var fa: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; var  t1,t2,head,tail:int64;  i,j:longint;  p:node;   begin         for i:=0 to n do       b[i]:=false;     for i:=1 to n-1 do       begin         read(t1,t2);                    new(p);         p^.num:=t2;p^.next:=nd[t1];nd[t1]:=p;         new(p);         p^.num:=t1;p^.next:=nd[t2];nd[t2]:=p;       end;      new(p);      head:=1;tail:=1;dl[head]:=1;b[1]:=true;       while head<=tail do         begin           p:=nd[dl[head]];           while p<>nil do             begin               if b[p^.num]=false then                 begin                   inc(tail);                   dl[tail]:=p^.num;                   fa[p^.num,0]:=dl[head];                   dep[p^.num]:=dep[dl[head]]+1;                   b[p^.num]:=true;                   if dep[p^.num]>maxdep then maxdep:=dep[p^.num];                 end;               p:=p^.next;             end;           inc(head);         end;      for j:=1 to trunc(ln(maxdep)/ln(2))+1 do        for i:=0 to n do          if dep[i]>=1<
dep[b] then begin t:=a; a:=b; b:=t; end; if dep[a]<>dep[b] then for i:=trunc(ln(dep[b]-dep[a])/ln(2))+10 downto 0 do if dep[b]-1<
=dep[a] then begin b:=fa[b,i]; ans:=ans+1<
b then for i:=trunc(ln(dep[a])/ln(2))+10 downto 0 do if fa[a,i]<>fa[b,i] then begin a:=fa[a,i]; b:=fa[b,i]; ans:=ans+1<<(i+1); end; if a<>b then inc(ans,2); end; begin readln(n); if n=1 then begin writeln(0); halt; end; maketree; readln(m); read(t1); for i:=2 to m do begin read(t2); lca(t1,t2); t1:=t2; end; writeln(ans); end.

 

转载于:https://www.cnblogs.com/zhujiangning/p/5252868.html

你可能感兴趣的文章
C#中static关键字的作用(转)
查看>>
Codeforces Round #428 (Div. 2) B
查看>>
2019春第十周作业
查看>>
(十六)Activitivi5之内置用户组(角色)设计表以及IdentityService
查看>>
python曲线拟合
查看>>
Linux & Vim Command Wallpaper
查看>>
Linux常用命令备忘
查看>>
小程序右上角转发分享web-view页面(备份前端网)
查看>>
virtualbox linux虚拟机相关
查看>>
关于.net和java我的见解
查看>>
【Android】设置Dialog点击屏幕不消失
查看>>
ConcurrentDictionary与Dictionary
查看>>
Atom Remote-FTP connecting FTP with SSL/TLS
查看>>
《代码大全》阅读笔记-27-程序规模对构建的影响
查看>>
What is R语言
查看>>
【给你一个承诺 - 玩转 AngularJS 的 Promise】
查看>>
P4962 朋也与光玉
查看>>
关于flash cs4意外退出的问题
查看>>
一道笔试指针题目详解
查看>>
easyui datagrid 绑定从后台得到的复杂的特殊数据结构
查看>>