2049: [Sdoi2008]cave Cave survey Time limit:10 Sec Memory limit:259 MB
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
10% of data meet n≤1000, m≤20000
20% of data meet n≤2000, m≤40000
30% of data meet n≤3000, m≤60000
40% of data meet n≤4000, m≤80000
50% of data meet n≤5000, m≤100000
60% of data meet n≤6000, m≤120000
70% of data meet n≤7000, m≤140000
80% of data meet n≤8000, m≤160000
90% of data meet n≤9000, m≤180000
100% of data meet n≤10000, m≤200000
Ensure that all destroy instructions will destroy an existing channel.
The input and output scale is large, it is recommended that c\c++ players use scanf and printf for i\o operation to avoid time-outs.
Source
Dynamic tree
Exercises
The first time to write LCT, according to other people's template imitation of the next.
This article is very good: http://quartergeek.com/summary-of-link-cut-tree/
Also need to understand carefully, this is only the most basic LCT. (P.S for the first time with so many pointers to really die)
1#include <bits/stdc++.h>2 using namespacestd;3 Const intmn=10002;4 structNode {5 BOOLRev;6Node *parent, *child[2];7}_memory[mn],*empty=_memory;8InlinevoidClear (node*Constx) {9 if(X==empty)return;Ten if(x->rev) { Onex->child[0]->rev^=1; Ax->child[1]->rev^=1; -Swap (x->child[0],x->child[1]); -x->rev=false; the } - } -InlinevoidRotate (node*ConstXConst intc) { -node*ConstY=x->parent; +y->child[c^1]=x->Child[c]; - if(X->child[c]!=empty) x->child[c]->parent=y; +X->parent=y->parent; A if(y->parent->child[0]==y) x->parent->child[0]=x; at Else if(y->parent->child[1]==y) x->parent->child[1]=x; -x->child[c]=y; -Y->parent=x; - } -InlineBOOLSplayy (node*Constx,node* (&y)) { - return(y=x->parent)!=empty && (y->child[0]==x| | y->child[1]==x); in } -InlinevoidSplay (Node *Constx) { to Clear (x); + for(Node *y,*Z;splayy (x, y);) - if(Splayy (y,z)) { the Clear (Z), Clear (Y), clear (x); * Const intc=y==z->child[0]; $ if(X==y->child[c]) rotate (x,c^1), rotate (x,c);Panax Notoginseng ElseRotate (y,c), rotate (x,c); -}Else { the Clear (Y); clear (x); +Rotate (x,x==y->child[0]); A } the } +Inline node* access (Node *u) { -node* v=EMPTY; $ for(;u!=empty;u=u->parent) { $ splay (u); -u->child[1]=v; -v=u; the } - returnv;Wuyi } theInline node* getroot (node*x) { - for(X=access (x); Clear (x), x->child[0]!=empty;x=x->child[0]); Wu returnx; - } AboutInlinevoidMakeroot (node*Constx) { $Access (x)->rev^=1; - splay (x); - } - voidLink (node*Constx,node*Consty) { A makeroot (x); +X->parent=y; the access (x); - } $InlinevoidCut (node*Constx,node*Consty) { the makeroot (x); the access (y), splay (y); they->child[0]->parent=EMPTY; they->child[0]=EMPTY; - } in intn,m; the intMain () { thescanf"%d%d",&n,&m); About for(intI=0; i<=n;++i) { thenode*Constnode=_memory+i; thenode->child[0]=node->child[1]=node->parent=EMPTY; the } + for(intI=0, x,y;i<m;++i) { - Charbuf[Ten]; thescanf"\n%s%d%d",buf,&x,&y);BayiNode *ra, *RB; the Switch(buf[0]) { the Case 'Q': -Ra=getroot (_memory+x), Rb=getroot (_memory+y); -printf (ra==rb&&ra!=empty?)"yes\n":"no\n"); the Break; the Case 'D': Cut (_memory+x,_memory+y); Break; the Case 'C': Link (_memory+x,_memory+y); the } - } the}
View Code
[Bzoj 2049] [SDOI2008] Cave Cave Survey