2016-05-30 11:04:51
Studied Link-cut-tree.
The second Chinese god Ben Feng's handout feels super clear and easy to understand AH (no can q nest
It's a template.
#include <bits/stdc++.h>#defineN 10005#defineINF 1000000000#definell Long Longusing namespacestd;intRead () {intx=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}namespacelct{intch[n][2],fa[n];BOOLRev[n]; InlineBOOLIsRootintx) { if(ch[fa[x]][0]!=x&&ch[fa[x]][1]!=X)return 1;return 0; } InlinevoidPushdown (intx) { if(Rev[x]) {rev[ch[x][0]]^=1; rev[ch[x][1]]^=1; rev[x]=0; Swap (ch[x][0],ch[x][1]); } } voidPushdown (intx) { if(!isroot (x)) Pushdown (Fa[x]);//whether the splay is a rootpushdown (x); } voidRotateintx) { inty=fa[x],k=ch[y][1]==x; if(!isroot (y)) ch[fa[y]][ch[fa[y]][1]==y]=x; FA[X]=Fa[y]; Fa[y]=x; fa[ch[x][!k]]=y; CH[Y][K]=ch[x][!K]; ch[x][!k]=y; } voidSplay (intx) {pushdown (x); for(inty=fa[x];! IsRoot (x); y=Fa[x]) { if(!isroot (y)) { if((ch[fa[y]][1]==y)! = (ch[y][1]==x)) Rotate (x); Elserotate (y); }rotate (x); }} inlinevoidAccessintx) { inty=0; while(x) {splay (x); ch[x][1]=y; X=fa[y=x]; }} inlinevoidMoveroot (intx) {access (x); splay (x); Rev[x]^=1; } InlinevoidLinkintXinty) {moveroot (x); Fa[x]=y;splay (x); } InlinevoidCutintXinty) {moveroot (x); Access (y); splay (y); ch[y][0]=fa[x]=0; } InlineintFindintx) {access (x); splay (x); while(ch[x][0]) {x=ch[x][0]; Pushdown (x); } returnx; }}intn,m;Charch[Ten];intMain () {n=read (); m=read (); while(m--) {scanf ("%s", CH); intX=read (), y=read (); if(ch[0]=='C') Lct::link (x, y); Else if(ch[0]=='D') lct::cut (x, y); Else if(Lct::find (x) ==lct::find (y)) printf ("yes\n");Elseprintf"no\n"); } return 0;}
View Code 2049: [Sdoi2008]cave Cave survey Time limit:10 Sec Memory limit:259 MB
submit:6355 solved:2950
[Submit] [Status] [Discuss] Description
Hui Hui is keen on cave surveying. One day, he followed the map to a region of caves marked JSZX. After a preliminary survey, FAI discovered that the area consisted of N caves (numbered 1 to n respectively) and several channels, each connected to exactly two caves. If two caves can be connected in a certain order by one or more channels, then the two caves are interconnected, and these are connected in sequence and are referred to as a path between the two caves. The caves are very strong and cannot be destroyed, but the passages are not very stable and often change due to external influences, for example, according to the monitoring results of the relevant instruments, there is sometimes a passage between Cave 123th and cave 127th, and sometimes the passage is destroyed for some strange reason. Hui-Hui has a monitoring instrument that can be displayed in real-time every change in the channel at the terminal on the side of the display: If a channel is detected between the cave U and the Cave V, the terminal will display an instruction connect U v if the tunnel between the cave U and Cave v is detected, an instruction is displayed on the terminal Destroy U v after a long and arduous manual reckoning, Fai found a strange phenomenon: no matter how the channel changes, any time between any two caves between at most only one path. As a result, FAI believes that this is due to the domination of some kind of essential law. As a result, FAI has been holding on to the terminal more and more day and night, trying to study this essential law through the change of channel. However, one day, Phillip collapsed in the calculation paper piled up in the mountains ... He smashed the terminal to the ground (the terminal was strong enough to break), turned to you and said, "Your brother writes this program." FAI wants to be able to issue command Query U v at any time via the terminal and ask the monitor if the cave U and the Cave v are connected at this time. Now you have to write a program for him to answer every question. It is known that no channel exists in the Jszx Cave group until the first instruction is shown.
Input
The first behavior is two positive integers n and m, respectively, indicating the number of caves and the number of instructions appearing on the terminal. The following m lines, in turn, represent the instructions that appear on the terminal. Each line begins with a string s ("Connect", "Destroy", or "Query") representing the type of instruction, followed by two integers u and V (1≤u, V≤n and U≠v) representing the number of the two caves respectively.
Output
For each query command, the output cave U and Cave v are interconnected: the output is "yes", otherwise the output "No". (does not contain double quotes)
Sample InputSample Input 1 cave.in
2005
Query123127
Connect123127
Query123127
Destroy127123
Query123127
Sample Input 2 cave.in
3 5
Connect12
Connect31
Query23
Destroy13
Query23
Sample OutputSample Output 1 cave.out
No
Yes
No
Sample Output 2 Cave.out
Yes
No
HINT
Data description 10% data satisfies n≤1000, m≤20000 20% data satisfies n≤2000, m≤40000 30% data satisfies n≤3000, m≤60000 40% data satisfies n≤4000, m≤80000 50% data satisfies n≤5000, m≤100000 60% data satisfies n≤6000, m≤120000 70% data satisfies n≤7000, m≤140000 80% data satisfies n≤8000, m≤160000 90% data satisfies n≤9000, m≤180000 100% The data satisfies n≤10000, m≤200000 guarantees that all destroy instructions will destroy an existing channel input, the output size is large, it is recommended that c\c++ players use scanf and printf i\o operation to avoid time-out
"bzoj2049" [Sdoi2008]cave Cave survey Link-cut-tree