并查集

function getfather(v:longint):longint;//寻找根节点
begin
  if father[v]=v then
    exit(v);
  father[v]:=getfather(father[v]);
  exit(father[v]);
end;

 

procedure merge(x,y:longint);//合并两集合
var
  xx,yy:longint;
begin
  xx:=getfather(x);
  yy:=getfather(y);
  father[xx]:=yy;
end;

 

function judge(x,y:longint):boolean;//判断是否在一集合中
var
  xx,yy:longint;
begin
  xx:=getfather(x);
  yy:=getfather(y);
  exit(xx=yy);
end;

 

posted on 2015-08-18 09:05  川汉唐  阅读(144)  评论(0编辑  收藏  举报

导航