首页 > 代码库 > NOIP模板

NOIP模板

LCA

倍增

1.不要忘记dfs后调用work

2.注意枚举2^i时要包含0,注意顺序

技术分享
procedure add(x,y,v:longint);var p:point;begin  new(p); p^.x:=y; p^.v:=v; p^.next:=a[x]; a[x]:=p;end;procedure dfs(x,k:longint);var p:point;begin  new(p); p:=a[x];  f[0,x]:=k; deep[x]:=deep[k]+1;  while p<>nil do   begin     if p^.x<>k then       begin s[p^.x]:=s[x]+p^.v; dfs(p^.x,x); end;p:=p^.next;   end;end;procedure work;var i,j:longint;begin  for i:=0 to trunc(ln(n)/ln(2))-1 do   for j:=1 to n do    f[i+1,j]:=f[i,f[i,j]];end;function lca(x,y:longint):longint;var i,t:longint;begin  if deep[x]<deep[y] then begin t:=x; x:=y; y:=t; end;  for i:=0 to trunc(ln(n)/ln(2)) do   if ((deep[x]-deep[y])>>i) and 1=1 then x:=f[i,x];  if x=y then exit(x);  for i:=trunc(ln(n)/ln(2)) downto 0 do   if f[i,x]<>f[i,y] then    begin x:=f[i,x]; y:=f[i,y]; end;  exit(f[0,x]);end;   
View Code

 

 

二分图

染色

技术分享
function dfs(x,c:longint):boolean;var i,y:longint; p:point;begin  color[x]:=c;  new(p);p:=w[x];  while p<>nil do   begin     y:=p^.x;     if color[y]=c then exit(false);     if (color[y]=0)and(not dfs(y,3-c)) then exit(false);     p:=p^.next;   end;  exit(true);end;procedure work;var i:longint;begin  for i:=1 to n do color[i]:=0;  for i:=1 to n do   if color[i]=0 then    if not dfs(i,1) then     begin flag:=false; exit; end;end;    
View Code

 

NOIP模板