"Bzoj 2049" [Sdoi2008]cave Cave Survey

Source: Internet
Author: User

2049: [Sdoi2008]cave Cave survey Time limit: ten Sec Memory Limit: 259 MB
Submit: 3215 Solved: 1449
[Submit] [Status] 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


This problem began with LCT write always re, so first with violence wrote a, a fell after found himself to LCT completely understand ~


Let's talk about the violence:

A[X].FA represents the father of X.


Connect (x, y): To refer to the root, that is, the x all the time to the side of the root to turn all the children, grandchildren of X. And then directly the father of X is assigned to Y and connected together.


< This is all about the violence of link operation in LCT, so I understand why cut has to reverse the side ~>


Destroy (x, y): Because x, y has a connecting edge, so it's not Y's father, or Y's son, straight off the line ~


#include <algorithm> #include <cstring> #include <cstdio> #include <iostream>using namespace Std;int n,m,fa[10005];int getfather (int x) {if (fa[x]==x) return X;return getfather (fa[x]);} void Root (int x) {if (fa[x]==x) return; Root (Fa[x]); fa[fa[x]]=x;} void Link (int x,int y) {root (x), root (y); fa[y]=y;fa[x]=y;} void Cut (int x,int y) {if (fa[x]==y) Fa[x]=x;else fa[y]=y;} int main () {scanf ("%d%d", &n,&m);    for (int i=1;i<=n;i++) Fa[i]=i;while (m--) {char str[20];int x,y;scanf ("%s%d%d", str,&x,&y); switch (str[0]) { Case ' C ': Link (x, y);    Break;case ' D ': Cut (x, y), break;case ' Q ': if (getfather (x) ==getfather (y)) printf ("yes\n"), Else printf ("no\n"); return 0;}


(faster than LCT.) )


LCT's approach is the template.

(See here)


But one thing to note:

Splay all the marks of the current node to the root are push_down!! before the


Why did you write splay when you push_down on the side of the rotation?


Because the previous splay before the findkth () at this time has already put all the marks are Push_down, and the LCT is directly rotated, so the first to pass all the tags.


#include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include < Cstring>using namespace Std;int root[50005],n,q;struct lct{int rev,l,r,fa;} A[50005];char str[20];void push_down (int x) {if (!x| |!        A[x].rev) Return;swap (A[X].L,A[X].R); a[a[x].l].rev^=1;a[a[x].r].rev^=1;a[x].rev=0;} void Zig (int x) {int y=a[x].fa;int z=a[y].fa;a[x].fa=z,a[y].fa=x;a[y].l=a[x].r,a[a[x].r].fa=y,a[x].r=y;if (root[y]) Root[y]=0,root[x]=1;else {if (Y==A[Z].L) A[z].l=x;else a[z].r=x;}} void zag (int x) {int y=a[x].fa;int z=a[y].fa;a[x].fa=z,a[y].fa=x;a[y].r=a[x].l,a[a[x].l].fa=y,a[x].l=y;if (root[y]) Root[y]=0,root[x]=1;else{if (Y==A[Z].L) A[z].l=x;else a[z].r=x;}} void p (int x) {if (!root[x]) p (A[X].FA); Push_down (x);} void splay (int x) {P (x), while (!root[x]) {int y=a[x].fa;int z=a[y].fa;if (Root[y]) {if (X==A[Y].L) zig (x); else zag (x);} else if (a[z].l==y) {if (a[y].l==x) Zig (y), Zig (x), else Zag (x), Zig (x);} Else{if (a[y].r==x) zag (y), Zag (x), else Zig (x), Zag (x);}} void Access (int x) {int y=0;while (x) {splay (x); root[a[x].r]=1;a[x].r=y;root[a[x].r]=0;y=x;x=a[x].fa;}} void makeroot (int x) {Access (x); Splay (x); a[x].rev^=1;} void Link (int u,int v) {makeroot (u); a[u].fa=v;}        void Cut (int u,int v) {makeroot (U); Access (v); Splay (v); root[a[v].l]=1;a[a[v].l].fa=0;a[v].l=0;} BOOL Same (int U,int v) {while (A[U].FA) u=a[u].fa;while (A[V].FA) V=a[v].fa;return u==v;}        int main () {//freopen ("cave.in", "R", stdin),//freopen ("Cave.out", "w", stdout); scanf ("%d%d", &n,&q); for (int i=0;i<=n;i++) {a[i].fa=0;a[i].l=a[i].r=a[i].rev=0;root[i]=1;} while (q--) {int x,y;scanf ("%s%d%d", str,&x,&y), switch (str[0]) {case ' C ': Link (x, y); Break;case ' d ': Cut (x, y); Break;case ' Q ': if (same (x, y)) printf ("yes\n"), Else printf ("no\n"); break;}} return 0;}


Did two LCT, wrote a summary, the idea of LCT has a certain understanding, but not completely mastered, so after a few more questions ~

"Bzoj 2049" [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.