Bzoj2049: [sdoi2008] Cave cave Survey

Source: Internet
Author: User
2049: [sdoi2008] Cave cave survey time limit: 10 sec memory limit: 259 MB
Submit: 2776 solved: 1247
[Submit] [Status] Description

Hui is keen on cave surveys. One day, he followed the map to a cave Group marked as jszx. After a preliminary survey, hui found that this area consists of N caves (numbered 1 to n) and several tunnels, and each channel connects exactly two caves. If the two caves can be connected in a certain order through one or more tunnels, the two caves are connected, these tunnels connected in order are called a path between the two caves. The caves are solid and cannot be damaged. However, the tunnel is unstable and often changes due to external influences. For example, according to the monitoring results of the relevant instruments, sometimes there is a channel between cave 123 and cave 127, and sometimes it is destroyed for some strange reason. Hui has a monitoring instrument that can display every change in the channel on the terminal at Hui's hand in real time: if a channel is detected between the cave U and the cave V, A command connect u v will be displayed on the terminal. If the channel between the cave U and the cave V is detected to be destroyed, a command destroy u v will be displayed on the terminal after a long time of arduous manual calculation, hui found a strange phenomenon: No matter how the channel changes, there is at most one path between any two caves at any time. Therefore, Hui believes that this is caused by the control of some essential law. As a result, huihui sticks to the terminal day and night and tries to study this essential law through channel changes. However, one day, Hui collapsed in the computing paper accumulated into the mountains ...... He smashed the terminal to the ground (the terminal is solid enough to be damaged) and turned to you and said, "write this program, dude ". Hui hopes to issue a command query u v at any time through the terminal and ask the monitor whether the cave U is connected to the cave V at this time. Now you have to write a program for him to answer every query. It is known that no channel exists in the jszx cave group before the first command is displayed.

Input

The first two positive integers n and M represent the number of caves and the number of commands on the terminal. The following M lines indicate the commands on the terminal in sequence. Each line starts with a string S ("Connect", "Destroy", or "query", which is case sensitive) indicating the type of the command. There are two integers U and V (1 ≤ U, v ≤ n and U =v) represent the numbers of the two caves respectively.

Output

For each query command, the output cave U and cave V are connected to each other: the output is "yes"; otherwise, the output is "no ". (Without double quotation marks)

Sample input example input 1 cave. In
2005
Query123127
Connect123127
Query123127
Destroy0000123
Query123127
Example 2 cave. In

3 5
Connect12
Connect31
Query23
Destroy13
Query23



Sample output sample output 1 cave. Out
No
Yes
No


Sample output 2 cave. Out

Yes
No

Hint

 

Data shows that 10% of the data meet n ≤ 1000, m ≤ 20000 of the data meet n ≤ 20%, m ≤ 2000 of the data meet n ≤ 40000, m ≤ 60000 40% of the Data satisfies n ≤ 4000, m ≤ 80000 of the Data satisfies n ≤ 50%, m ≤ 5000 of the Data satisfies n ≤ 100000, m ≤ 120000 70% of the Data satisfies n ≤ 7000, m ≤ 140000 of the Data satisfies n ≤ 80%, m ≤ 8000 of the Data satisfies n ≤ 160000, m ≤ 180000 100% of Data satisfies n ≤ 10000, m ≤ 200000 ensures that all destroy commands will destroy an existing channel. In this case, the input and output scale is relatively large, it is recommended that C \ c ++ players use scanf and printf for I \ O operations to avoid timeout.

 

Source

Dynamic Tree

Question: I used to check the collection. Today I will write LCT. After understanding it, it is quite easy to write. You only need to support root check, so no additional information is maintained. Code:
  1 #include<cstdio>  2 #include<cstdlib>  3 #include<cmath>  4 #include<cstring>  5 #include<algorithm>  6 #include<iostream>  7 #include<vector>  8 #include<map>  9 #include<set> 10 #include<queue> 11 #include<string> 12 #define inf 1000000000 13 #define maxn 20000+1000 14 #define maxm 500+100 15 #define eps 1e-10 16 #define ll long long 17 #define pa pair<int,int> 18 using namespace std; 19 inline int read() 20 { 21     int x=0,f=1;char ch=getchar(); 22     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 23     while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();} 24     return x*f; 25 } 26 int fa[maxn],c[maxn][2],sta[maxn],n,m; 27 bool rev[maxn]; 28 inline bool isroot(int x) 29 { 30     return c[fa[x]][0]!=x&&c[fa[x]][1]!=x; 31 } 32 void rotate(int x) 33 { 34     int y=fa[x],z=fa[y],l=(c[y][1]==x),r=l^1; 35     if(!isroot(y))c[z][c[z][1]==y]=x; 36     fa[x]=z;fa[y]=x;fa[c[x][r]]=y; 37     c[y][l]=c[x][r];c[x][r]=y; 38 } 39 void pushdown(int x) 40 { 41     if(!rev[x])return; 42     rev[x]^=1;rev[c[x][0]]^=1;rev[c[x][1]]^=1; 43     swap(c[x][0],c[x][1]); 44 } 45 void splay(int x) 46 { 47     int top=0;sta[++top]=x; 48     for(int y=x;!isroot(y);y=fa[y])sta[++top]=fa[y]; 49     for(;top;)pushdown(sta[top--]); 50     while(!isroot(x)) 51     { 52         int y=fa[x],z=fa[y]; 53         if(!isroot(y)) 54          { 55              if(c[z][0]==y^c[y][0]==x)rotate(x);else rotate(y); 56          } 57         rotate(x);  58     } 59      60 } 61 void access(int x) 62 { 63     for(int y=0;x;x=fa[x]) 64     { 65         splay(x);c[x][1]=y;y=x; 66     } 67 } 68 void makeroot(int x) 69 { 70     access(x);splay(x);rev[x]^=1; 71 } 72 int find(int x) 73 { 74     access(x);splay(x); 75     while(c[x][0])x=c[x][0]; 76     return x; 77 } 78 void link(int x,int y) 79 { 80     makeroot(x);fa[x]=y;splay(x); 81 } 82 void cut(int x,int y) 83 { 84     makeroot(x);access(y);splay(y);c[y][0]=fa[x]=0; 85 } 86 int main() 87 { 88     freopen("input.txt","r",stdin); 89     freopen("output.txt","w",stdout); 90     n=read();m=read(); 91     char ch[10];int x,y; 92     while(m--) 93     { 94         scanf("%s",ch);x=read();y=read(); 95         switch(ch[0]) 96         { 97             case ‘C‘:link(x,y);break; 98             case ‘D‘:cut(x,y);break; 99             case ‘Q‘:{if(find(x)==find(y))printf("Yes\n");else printf("No\n");}break;100         }101     }102     return 0;103 }
View code

 

Bzoj2049: [sdoi2008] Cave cave Survey

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.