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
Test instructions: There are three kinds of operations, building edges, destroying edges and querying whether two points are interlinked, to ensure that there is at most one of the two points of the same path, that is to say the entire graph is always tree or forest.
Parse: Dynamic Tree template title, plus two URLs that describe the dynamic tree:
https://oi.abcdabcd987.com/summary-of-link-cut-tree/
Http://wenku.baidu.com/view/75906f160b4e767f5acfcedb.html
Code
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intmaxn=10005;structlct{LCT*fa,*son[2]; intRev; voidPushdown ()//remove the markup for delayed updates { if(!rev)return; Swap (son[0],son[1]);//Flip, swap around sonson[0]->rev^=1; son[1]->rev^=1; Rev=0; }};structlct{LCT DATA[MAXN]; LCT*NULL; voidInitintsize=maxn-1)//Initialize { NULL=data;//null points to the first element for(intI=0; i<=size;i++) data[i].son[0]=data[i].son[1]=data[i].fa=NULL; } BOOLSame (lct* x,lct* &y)//To determine if the father of X and X is in the same tree. { return(Y=X->FA)! =NULL&& (y->son[0]==x| | y->son[1]==x); } voidRotate (lct* x,intD//Flip{LCT* y=x->fa;//father of Xy->son[d^1]=x->Son[d]; if(x->son[d]!=NULL) x->son[d]->fa=y;//The father of the child node of x points to Yx->fa=y->fa;//Connection if(y->fa->son[0]==y) x->fa->son[0]=x; Else if(y->fa->son[1]==y) x->fa->son[1]=x; X->son[d]=y; Y->fa=x; } voidSplay (lct*x) {x->pushdown ();//Clear Markslct*y; while(Same (x, y))//not to the apex of the tree{y-pushdown (); X-pushdown (); Rotate (x, y->son[0]==X);//Flip}} LCT* Access (lct* u)//Get through the path{LCT*v=NULL; for(; u!=NULL;u=u->FA) {splay (U); U->son[1]=v; V=u; } returnv; } LCT* Getroot (lct* x)//Get Root { for(X=access (x); X->pushdown (), x->son[0]!=NULL; x=x->son[0]); returnx; } voidMakeroot (lct* x)//make X a root{Access (x)->rev^=1; Splay (x); } voidLink (lct* x,lct* y)//Connect two points{makeroot (x); X->fa=y; Access (x); } voidCut (lct* x,lct* y)//break two points{makeroot (x); Access (y); Splay (y); Y->son[0]->fa=NULL; Y->son[0]=NULL; }}a;intn,m;intMain () {scanf ("%d%d",&n,&M); A.init (N); intx, y; Chars[Ten]; while(m--) {scanf ("%s%d%d",s,&x,&y); if(s[0]=='Q') {LCT*a=a.getroot (a.data+x); LCT*b=a.getroot (a.data+y); if(A!=a.NULL&&A==B) printf ("yes\n"); Elseprintf"no\n"); } Else if(s[0]=='D') A.cut (a.data+x,a.data+y); ElseA.link (a.data+x,a.data+y); } return 0;}
View Code
bzoj2049-Cave Survey (Dynamic tree LCT template problem)