首页 > 代码库 > 【BZOJ2049】洞穴勘测(LCT)
【BZOJ2049】洞穴勘测(LCT)
题意:一张图,要求支持以下操作:
1.加边
2.删边
3.询问两点之间是否联通
100%的数据满足n≤10000, m≤200000
思路:LCT裸题,不需要维护任何信息
1 var t:array[0..500000,0..1]of longint; 2 fa,rev,q:array[0..500000]of longint; 3 n,m,i,x,y,k,j,top,s:longint; 4 ch:string; 5 6 procedure swap(var x,y:longint); 7 var t:longint; 8 begin 9 t:=x; x:=y; y:=t; 10 end; 11 12 function isroot(x:longint):boolean; 13 begin 14 if (t[fa[x],0]<>x)and(t[fa[x],1]<>x) then exit(true); 15 exit(false); 16 end; 17 18 procedure pushdown(x:longint); 19 var l,r:longint; 20 begin 21 l:=t[x,0]; r:=t[x,1]; 22 if rev[x]>0 then 23 begin 24 rev[x]:=rev[x] xor 1; rev[l]:=rev[l] xor 1; rev[r]:=rev[r] xor 1; 25 swap(t[x,0],t[x,1]); 26 end; 27 end; 28 29 procedure rotate(x:longint); 30 var y,z,l,r:longint; 31 begin 32 y:=fa[x]; z:=fa[y]; 33 if t[y,0]=x then l:=0 34 else l:=1; 35 r:=l xor 1; 36 while not isroot(y) do 37 begin 38 if t[z,0]=y then t[z,0]:=x 39 else t[z,1]:=x; 40 end; 41 fa[x]:=z; fa[y]:=x; fa[t[x,r]]:=y; 42 t[y,l]:=t[x,r]; t[x,r]:=y; 43 end; 44 45 procedure splay(x:longint); 46 var y,z,k:longint; 47 begin 48 inc(top); q[top]:=x; 49 k:=x; 50 while not isroot(k) do 51 begin 52 inc(top); q[top]:=fa[k]; 53 k:=fa[k]; 54 end; 55 56 while top>0 do 57 begin 58 pushdown(q[top]); 59 dec(top); 60 end; 61 62 while not isroot(x) do 63 begin 64 y:=fa[x]; z:=fa[y]; 65 if not isroot(y) then 66 begin 67 if (t[y,0]=x)xor(t[z,0]=y) then rotate(x) 68 else rotate(y); 69 end; 70 rotate(x); 71 end; 72 end; 73 74 procedure access(x:longint); 75 var last:longint; 76 begin 77 last:=0; 78 while x>0 do 79 begin 80 splay(x); t[x,1]:=last; 81 last:=x; x:=fa[x]; 82 end; 83 end; 84 85 procedure makeroot(x:longint); 86 begin 87 access(x); splay(x); rev[x]:=rev[x] xor 1; 88 end; 89 90 procedure link(x,y:longint); 91 begin 92 makeroot(x); fa[x]:=y; 93 end; 94 95 procedure cut(x,y:longint); 96 begin 97 makeroot(x); access(y); splay(y); t[y,0]:=0; fa[x]:=0; 98 end; 99 100 function findroot(x:longint):longint; 101 var k:longint; 102 begin 103 access(x); splay(x); 104 k:=x; 105 while t[k,0]<>0 do k:=t[k,0]; 106 exit(k); 107 end; 108 109 begin 110 assign(input,‘bzoj2049.in‘); reset(input); 111 assign(output,‘bzoj2049.out‘); rewrite(output); 112 readln(n,m); 113 for i:=1 to m do 114 begin 115 readln(ch); k:=length(ch); s:=0; x:=0; y:=0; j:=1; 116 repeat 117 if (ch[j]=‘ ‘) then 118 begin 119 inc(s); 120 while ch[j]=‘ ‘ do inc(j); 121 continue; 122 end; 123 if (ch[j]>=‘0‘)and(ch[j]<=‘9‘) then 124 begin 125 case s of 126 1:x:=x*10+ord(ch[j])-ord(‘0‘); 127 2:y:=y*10+ord(ch[j])-ord(‘0‘); 128 end; 129 end; 130 inc(j); 131 until j>k; 132 case ch[1] of 133 ‘C‘:link(x,y); 134 ‘D‘:cut(x,y); 135 ‘Q‘: 136 if findroot(x)<>findroot(y) then writeln(‘No‘) 137 else writeln(‘Yes‘); 138 end; 139 end; 140 141 142 close(input); 143 close(output); 144 end.
【BZOJ2049】洞穴勘测(LCT)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。