Bzoj 2049: [sdoi2008] Cave cave survey LCT

Source: Internet
Author: User


Entry-level LCT: only cut Link

2049: [sdoi2008] Cave cave survey time limit: 10 sec memory limit: 259 MB
Submit: 3073 solved: 1379
[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
200 5
Query 123 127
Connect 123 127
Query 123 127
Destroy 127 123
Query 123 127
Example 2 cave. In

3 5
Connect 1 2
Connect 3 1
Query 2 3
Destroy 1 3
Query 2 3



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

[Submit] [Status] Bytes


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=11000;int ch[maxn][2],pre[maxn];int rev[maxn];bool rt[maxn];void update_rev(int r){  if(!r) return ;  swap(ch[r][0],ch[r][1]);  rev[r]^=1;}void push_down(int r){  if(rev[r])    {      update_rev(ch[r][0]);      update_rev(ch[r][1]);      rev[r]=0;    }}void Rotate(int x){  int y=pre[x],kind=ch[y][1]==x;  ch[y][kind]=ch[x][!kind];  pre[ch[y][kind]]=y;  pre[x]=pre[y];  pre[y]=x;  ch[x][!kind]=y;  if(rt[y])    rt[y]=false,rt[x]=true;  else    ch[pre[x]][ch[pre[x]][1]==y]=x;}void P(int r){  if(!rt[r]) P(pre[r]);  push_down(r);}void Splay(int r){  P(r);  while(!rt[r])    {      int f=pre[r],ff=pre[f];      if(rt[f])        Rotate(r);      else if((ch[ff][1]==f)==(ch[f][1]==r))        Rotate(f),Rotate(r);      else        Rotate(r),Rotate(r);    }}int Access(int x){  int y=0;  for(;x;x=pre[y=x])    {      Splay(x);      rt[ch[x][1]]=true,rt[ch[x][1]=y]=false;    }  return y;}bool judge(int u,int v){  while(pre[u]) u=pre[u];  while(pre[v]) v=pre[v];  return u==v;}void mroot(int r){  Access(r);  Splay(r);  update_rev(r);}void link(int u,int v){  mroot(u);  pre[u]=v;}void cut(int u,int v){  mroot(u);  Splay(v);  pre[ch[v][0]]=pre[v];  pre[v]=0;  rt[ch[v][0]]=true;  ch[v][0]=0;}char op[50];int n,m;void init(){  memset(pre,0,sizeof(pre));  memset(ch,0,sizeof(ch));  memset(rev,0,sizeof(rev));  memset(rt,true,sizeof(rt));}int main(){  while(scanf("%d%d",&n,&m)!=EOF)    {      init();      while(m--)        {          scanf("%s",op);          int u,v;          scanf("%d%d",&u,&v);          if(op[0]=='C') link(u,v);          else if(op[0]=='D') cut(u,v);          else            {              if(judge(u,v)==true) puts("Yes");              else puts("No");            }        }    }  return 0;}



Bzoj 2049: [sdoi2008] Cave cave survey LCT

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.