题意:初始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