bzoj 2049 LCT模板

61 篇文章 0 订阅
6 篇文章 0 订阅

题意:初始n个点独立,三种操作:(1)Connect x y x和y之间连一条边 (2)Destroy x y x和y之间的边断掉 (3)Query x y 查询x和y是否联通

LCT模板..

var
        n,m,x,y         :longint;
        i,j             :longint;
        ch              :char;
        s               :string;
        father          :array[-1..200010] of longint;
        son             :array[-1..200010,0..1] of longint;
        flag            :array[-1..200010] of boolean;

function root(x:longint):boolean;
begin
   if (x=son[father[x],0]) or (x=son[father[x],1]) then exit(false);
   exit(true);
end;

procedure swap(var a,b:longint);
var
        c:longint;
begin
   c:=a; a:=b; b:=c;
end;

procedure renew(x:longint);
begin
   flag[x]:=not flag[x];
end;

procedure pushdown(x:longint);
var
        l,r:longint;
begin
   if flag[x] then
   begin
      swap(son[x,0],son[x,1]);
      l:=son[x,0]; r:=son[x,1];
      flag[l]:=not flag[l];
      flag[r]:=not flag[r];
   end;
   flag[x]:=false;
end;

procedure push(x:longint);
begin
   if not root(x) then push(father[x]);
   pushdown(x);
end;

procedure ro(x,y:longint);
var
        f:longint;
begin
   f:=father[x];
   if son[x,y xor 1]<>-1 then father[son[x,y xor 1]]:=f;
   son[f,y]:=son[x,y xor 1];
   if f=son[father[f],0] then son[father[f],0]:=x else
     if f=son[father[f],1] then son[father[f],1]:=x;
   father[x]:=father[f];
   father[f]:=x;
   son[x,y xor 1]:=f;
end;

procedure splay(x:longint);
var
        u,v:longint;
begin
   while not root(x) do
   begin
      if root(father[x]) then  ro(x,ord(x=son[father[x],1])) else
      begin
         if x=son[father[x],0] then u:=1 else u:=-1;
         if father[x]=son[father[father[x]],0] then v:=1 else v:=-1;
         if u*v=1 then
         begin
            ro(father[x],ord(x=son[father[x],1]));
            ro(x,ord(x=son[father[x],1]));
         end else
         begin
            ro(x,ord(x=son[father[x],1]));
            ro(x,ord(x=son[father[x],1]));
         end;
      end;
   end;
end;

procedure access(x:longint);
var
        y:longint;
begin
   y:=-1;
   while x<>0 do
   begin
      push(x);
      splay(x);
      son[x,1]:=y;
      y:=x;
      x:=father[x];
   end;
end;

function find_root(x:longint):longint;
begin
   access(x); splay(x);
   while (son[x,0]<>-1) do x:=son[x,0];
   exit(x);
end;

begin
   read(n,m);readln;
   fillchar(son,sizeof(son),255);
   for i:=1 to m do
   begin
      s:='';
      read(ch);
      while (ch<>' ') do
      begin
         s:=s+ch;read(ch);
      end;
      readln(x,y);
      if s='Connect' then
      begin
         access(x); splay(x); renew(x);
         access(y);
         son[y,1]:=x;
         father[x]:=y;
      end else
      if s='Destroy' then
      begin
         access(x); splay(x); renew(x);
         access(y); push(x); splay(x);
         son[x,1]:=-1;
         father[y]:=0;
      end else
      begin
         if find_root(x)=find_root(y) then writeln('Yes') else writeln('No');
      end;
   end;
end.
——by Eirlys



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值