Bzoj topic 2049: [Sdoi2008]cave Cave Survey (link cut tree)

Source: Internet
Author: User

2049: [Sdoi2008]cave Cave survey Time limit: ten Sec Memory Limit: 259 MB
Submit: 4698 Solved: 2107
[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
200 5
Query 123 127
Connect 123 127
Query 123 127
Destroy 127 123
Query 123 127
Sample Input 2 cave.in

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



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

Source

Dynamic tree

More and more worship of middle school students.

AC Code

/************************************************************** problem:2049 user:kxh1995 language:c++ Resu lt:accepted time:1620 Ms memory:1820 kb****************************************************************/#include &L t;stdio.h> #include <string.h> #include <algorithm> #include <iostream>using namespace Std;int ch[    28080][2],pre[28080];int Rev[28080];int rt[28080];//Judging is not the root int n,m;void init () {int i;        for (i=0;i<=n;i++) {pre[i]=0;        ch[i][0]=ch[i][1]=0;        rev[i]=0;    Rt[i]=1;    }}void Update_rev (int r) {if (!r) return;    Swap (ch[r][0],ch[r][1]); Rev[r]^=1;}        void pushdown (int r) {if (Rev[r]) {Update_rev (ch[r][0]);        Update_rev (ch[r][1]);    rev[r]=0;    }}//void pushup (int r)//{}void rotate (int x) {int y=pre[x];    int 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]=0; RT[x]=1;    } else {ch[pre[x]][ch[pre[x]][1]==y]=x; }//pushup (y);}    void P (int r) {if (!rt[r]) p (Pre[r]); Pushdown (r);}    void splay (int r) {P (R);        while (!rt[r]) {int f=pre[r];        int ff=pre[f];        if (rt[f]) rotate (R);                else if (((ch[ff][1]==f) = = (ch[f][1]==f))) {rotate (f);            Rotate (R);                } else {rotate (R);            Rotate (R); }}//pushup (r);}    int access (int x)//through X to root node {int y=0;        do {splay (x);        Rt[ch[x][1]]=1;        rt[ch[x][1]=y]=0;        Pushup (x);    X=PRE[Y=X];    }while (x); return y;}    void Mroot (int r) {access (R);    Splay (R); Update_rev (r);}    int judge (int U,int v) {while (Pre[u]) u=pre[u];    while (Pre[v]) v=pre[v]; return u==v;}    void link (int u,int v) {if (judge (U,v)) {return;    } mroot (U); Pre[u]=v;} void Cut (int u,int v) {if (u==v| |!    Judge (U,v)) {return;    } mroot (U);    Splay (v);    PRE[CH[V][0]]=PRE[V];    pre[v]=0;    Rt[ch[v][0]]=1;    ch[v][0]=0; Pushup (v);}    int main () {//int n,m;        while (scanf ("%d%d", &n,&m)!=eof) {init ();        Char op[2];            while (m--) {int u,v;            scanf ("%s%d%d", op,&u,&v);            if (op[0]== ' C ') {link (u,v);                } else if (op[0]== ' D ') {cut (u,v);                    } else {if (judge (u,v)) printf ("yes\n");                else printf ("no\n"); }        }    }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Bzoj topic 2049: [Sdoi2008]cave Cave Survey (link cut tree)

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.