【SDOI2008】【BZOJ2049】Cave 洞穴勘測
Description
辉辉热衷于洞穴勘測。某天,他依照地图来到了一片被标记为JSZX的洞穴群地区。经过初步勘測,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成。而且每条通道连接了恰好两个洞穴。
假如两个洞穴能够通过一条或者多条通道按一定顺序连接起来,那么这两个洞穴就是连通的,按顺序连接在一起的这些通道则被称之为这两个洞穴之间的一条路径。洞穴都十分牢固无法破坏,然而通道不太稳定,时常由于外界影响而发生改变,比方。依据有关仪器的监測结果。123号洞穴和127号洞穴之间有时会出现一条通道,有时这条通道又会由于某种稀奇古怪的原因被毁。
辉辉有一台监測仪器能够实时将通道的每一次改变状况在辉辉手边的终端机上显示:假设监測到洞穴u和洞穴v之间出现了一条通道,终端机上会显示一条指令 Connect u v 假设监測到洞穴u和洞穴v之间的通道被毁,终端机上会显示一条指令 Destroy u v 经过长期的艰苦卓绝的手工推算。辉辉发现一个奇怪的现象:不管通道怎么改变,随意时刻随意两个洞穴之间至多仅仅有一条路径。因而,辉辉坚信这是由于某种本质规律的支配导致的。因而。辉辉更加夜以继日地坚守在终端机之前,试图通过通道的改变情况来研究这条本质规律。然而,最终有一天,辉辉在堆积成山的演算纸中崩溃了……他把终端机往地面一砸(终端机也足够牢固无法破坏),转而求助于你。说道:“你老兄把这程序写写吧”。
辉辉希望能随时通过终端机发出指令 Query u v。向监測仪询问此时洞穴u和洞穴v是否连通。
如今你要为他编敲代码回答每一次询问。已知在第一条指令显示之前,JSZX洞穴群中没有不论什么通道存在。
Input
第一行为两个正整数n和m,分别表示洞穴的个数和终端机上出现过的指令的个数。下面m行,依次表示终端机上出现的各条指令。每行开头是一个表示指令种类的字符串s(”Connect”、”Destroy”或者”Query”。区分大写和小写),之后有两个整数u和v (1≤u, v≤n且u≠v) 分别表示两个洞穴的编号。
Output
对每一个Query指令。输出洞穴u和洞穴v是否互相连通:是输出”Yes”,否则输出”No”。(不含双引號)
Sample Input
例子输入1 cave.in
200 5
Query 123 127
Connect 123 127
Query 123 127
Destroy 127 123
Query 123 127
例子输入2 cave.in
3 5
Connect 1 2
Connect 3 1
Query 2 3
Destroy 1 3
Query 2 3
Sample Output
例子输出1 cave.out
No
Yes
No
例子输出2 cave.out
Yes
No
HINT
数据说明 10%的数据满足n≤1000, m≤20000 20%的数据满足n≤2000, m≤40000 30%的数据满足n≤3000, m≤60000 40%的数据满足n≤4000, m≤80000 50%的数据满足n≤5000, m≤100000 60%的数据满足n≤6000, m≤120000 70%的数据满足n≤7000, m≤140000 80%的数据满足n≤8000, m≤160000 90%的数据满足n≤9000, m≤180000 100%的数据满足n≤10000, m≤200000 保证全部Destroy指令将摧毁的是一条存在的通道本题输入、输出规模比較大,建议c\c++选手使用scanf和printf进行I\O操作以免超时
Source
动态树
裸题啊…
link然后cut.查询就查一下连通性…
(请叫我写LCT必挂星人QAQ)
写残Cut就是切不掉那条边233
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #define MAXN 10010 using namespace std; int n,m,u,v; char ch[]; struct splay { int fa,ch[]; bool rev; }tree[MAXN]; int sta[MAXN],top; void in(int &x) { char ch=getchar();int flag=;x=; while (!(ch>='0'&&ch<='9')) flag=ch=='-'?
-<span class="hljs-number">1</span>:<span class="hljs-number">1</span>,ch=getchar();
<span class="hljs-keyword">while</span> (ch>=<span class="hljs-string">'0'</span>&&ch<=<span class="hljs-string">'9'</span>) x=x*<span class="hljs-number">10</span>+ch-<span class="hljs-string">'0'</span>,ch=getchar();
}
<span class="hljs-keyword">inline</span> <span class="hljs-keyword">bool</span> is_root(<span class="hljs-keyword">int</span> x)
{
<span class="hljs-keyword">return</span> tree[tree[x].fa].ch[<span class="hljs-number">0</span>]!=x&&tree[tree[x].fa].ch[<span class="hljs-number">1</span>]!=x;
}
<span class="hljs-keyword">inline</span> <span class="hljs-keyword">void</span> push_down(<span class="hljs-keyword">int</span> x)
{
<span class="hljs-keyword">if</span> (tree[x].rev)
{
tree[x].rev^=<span class="hljs-number">1</span>;tree[tree[x].ch[<span class="hljs-number">0</span>]].rev^=<span class="hljs-number">1</span>;tree[tree[x].ch[<span class="hljs-number">1</span>]].rev^=<span class="hljs-number">1</span>;
swap(tree[x].ch[<span class="hljs-number">0</span>],tree[x].ch[<span class="hljs-number">1</span>]);
}
}
<span class="hljs-keyword">inline</span> <span class="hljs-keyword">void</span> rot(<span class="hljs-keyword">int</span> x)
{
<span class="hljs-keyword">int</span> y=tree[x].fa,z=tree[y].fa,l,r;
l=(tree[y].ch[<span class="hljs-number">1</span>]==x);r=l^<span class="hljs-number">1</span>;
<span class="hljs-keyword">if</span> (!is_root(y)) tree[z].ch[tree[z].ch[<span class="hljs-number">1</span>]==y]=x;
tree[tree[x].ch[r]].fa=y;tree[y].fa=x;tree[x].fa=z;
tree[y].ch[l]=tree[x].ch[r];tree[x].ch[r]=y;
}
<span class="hljs-keyword">inline</span> <span class="hljs-keyword">void</span> Splay(<span class="hljs-keyword">int</span> x)
{
sta[++top]=x;
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i=x;!is_root(i);i=tree[i].fa) sta[++top]=tree[i].fa;
<span class="hljs-keyword">while</span> (top) push_down(sta[top--]);
<span class="hljs-keyword">while</span> (!is_root(x))
{
<span class="hljs-keyword">int</span> y=tree[x].fa,z=tree[y].fa;
<span class="hljs-keyword">if</span> (!is_root(y))
{
<span class="hljs-keyword">if</span> (tree[y].ch[<span class="hljs-number">0</span>]==x^tree[z].ch[<span class="hljs-number">0</span>]==y) rot(x);
<span class="hljs-keyword">else</span> rot(y);
}
rot(x);
}
}
<span class="hljs-keyword">inline</span> <span class="hljs-keyword">void</span> access(<span class="hljs-keyword">int</span> x)
{
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> t=<span class="hljs-number">0</span>;x;t=x,x=tree[x].fa) Splay(x),tree[x].ch[<span class="hljs-number">1</span>]=t;
}
<span class="hljs-keyword">inline</span> <span class="hljs-keyword">void</span> make_root(<span class="hljs-keyword">int</span> x)
{
access(x);Splay(x);tree[x].rev^=<span class="hljs-number">1</span>;
}
<span class="hljs-keyword">inline</span> <span class="hljs-keyword">void</span> link(<span class="hljs-keyword">int</span> x,<span class="hljs-keyword">int</span> y)
{
make_root(x);tree[x].fa=y;
}
<span class="hljs-keyword">inline</span> <span class="hljs-keyword">void</span> cut(<span class="hljs-keyword">int</span> x,<span class="hljs-keyword">int</span> y)
{
make_root(x);access(y);Splay(y);tree[y].ch[<span class="hljs-number">0</span>]=tree[x].fa=<span class="hljs-number">0</span>;
}
<span class="hljs-keyword">inline</span> <span class="hljs-keyword">int</span> find(<span class="hljs-keyword">int</span> x)
{
access(x);Splay(x);
<span class="hljs-keyword">int</span> ret=x;
<span class="hljs-keyword">while</span> (tree[ret].ch[<span class="hljs-number">0</span>]) ret=tree[ret].ch[<span class="hljs-number">0</span>];
<span class="hljs-keyword">return</span> ret;
}
<span class="hljs-keyword">int</span> main()
{
in(n);in(m);
<span class="hljs-keyword">while</span> (m--)
{
<span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%s"</span>,ch);in(u);in(v);
<span class="hljs-keyword">if</span> (ch[<span class="hljs-number">0</span>]==<span class="hljs-string">'Q'</span>) find(u)==find(v)?<span class="hljs-built_in">puts</span>(<span class="hljs-string">"Yes"</span>):<span class="hljs-built_in">puts</span>(<span class="hljs-string">"No"</span>);
<span class="hljs-keyword">if</span> (ch[<span class="hljs-number">0</span>]==<span class="hljs-string">'C'</span>) link(u,v);
<span class="hljs-keyword">if</span> (ch[<span class="hljs-number">0</span>]==<span class="hljs-string">'D'</span>) cut(u,v);
}
}