#include<cstdio>
#include<iostream>
using namespace std;
int shu[][],n,m,fa[],st[];
bool rev[];
bool isroot(int a1)
{
return shu[fa[a1]][]!=a1&&shu[fa[a1]][]!=a1;
}
void pushdown(int a1)
{
int l=shu[a1][],r=shu[a1][];
if(rev[a1])
{
rev[a1]^=;
rev[l]^=;
rev[r]^=;
swap(shu[a1][],shu[a1][]);
}
}
void zhuan(int a1)
{
int y=fa[a1],z=fa[y],l,r;
if(shu[y][]==a1)
l=;
else
l=;
r=l^;
if(!isroot(y))
if(shu[z][]==y)
shu[z][]=a1;
else
shu[z][]=a1;
fa[a1]=z;
fa[y]=a1;
shu[y][l]=shu[a1][r];
fa[shu[y][l]]=y;
shu[a1][r]=y;
}
void splay(int a1)
{
int top=;
top++;
st[top]=a1;
for(int i=a1;!isroot(i);i=fa[i])
{
top++;
st[top]=fa[i];
}
for(int i=top;i;i--)
pushdown(st[i]);
for(;!isroot(a1);)
{
int y=fa[a1],z=fa[a1];
if(!isroot(y))
if(y==shu[z][]^a1==shu[y][])
zhuan(a1);
else
zhuan(y);
zhuan(a1);
}
}
void access(int a1)
{
int t=;
for(;a1;)
{
splay(a1);
shu[a1][]=t;
t=a1;
a1=fa[a1];
}
}
void gen(int a1)
{
access(a1);
splay(a1);
rev[a1]^=;
}
void lian(int a1,int a2)
{
gen(a1);
fa[a1]=a2;
splay(a1);
}
void cut(int a1,int a2)
{
gen(a1);
access(a2);
splay(a2);
shu[a2][]=fa[a1]=;
}
int find(int a1)
{
access(a1);
splay(a1);
int y;
for(y=a1;shu[y][];y=shu[y][]);
return y;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<m;i++)
{
int a1,a2;
char ch[];
scanf("%s%d%d",ch,&a1,&a2);
if(ch[]=='C')
lian(a1,a2);
if(ch[]=='D')
cut(a1,a2);
if(ch[]=='Q')
{
int a3,a4;
a3=find(a1);
a4=find(a2);
if(a3==a4)
printf("Yes\n");
else
printf("No\n");
}
}
return ;
}

link-cut-tree

bzoj 2049: [Sdoi2008]Cave 洞穴勘测的更多相关文章

  1. BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 LCT

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnli ...

  2. bzoj 2049: [Sdoi2008]Cave 洞穴勘测 动态树

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 3119  Solved: 1399[Submit] ...

  3. bzoj 2049: [Sdoi2008]Cave 洞穴勘测 (LCT)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2049 题面: 2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 ...

  4. BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 (动态树入门)

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1528  Solved: 644[Submit][ ...

  5. [BZOJ 2049] [Sdoi2008] Cave 洞穴勘测 【LCT】

    题目链接:BZOJ - 2049 题目分析 LCT的基本模型,包括 Link ,Cut 操作和判断两个点是否在同一棵树内. Link(x, y) : Make_Root(x); Splay(x); F ...

  6. 【刷题】BZOJ 2049 [Sdoi2008]Cave 洞穴勘测

    Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...

  7. bzoj 2049 [Sdoi2008]Cave 洞穴勘测(LCT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2049 [题意] 给定森林,可能有连边或断边的操作,回答若干个连通性的询问. [思路] ...

  8. ●BZOJ 2049 [Sdoi2008]Cave洞穴勘测

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2049 题解: LCT入门题 就是判两个点是否在同一颗树里 代码: #include<c ...

  9. BZOJ 2049: [Sdoi2008]Cave 洞穴勘测——LCT

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2049 省选之前来切一道数据结构模板题. 题意 这是一道模板题. N个点,M次操作,每次加边/ ...

  10. BZOJ 2049 [Sdoi2008]Cave 洞穴勘测(动态树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2049 [题目大意] 要求支持树的断边和连边,以及连接查询 [题解] LCT练习题 [代 ...

随机推荐

  1. amd64_or_ia64?

    amd64 网上资料: 1. IA64是intel推出的架构,AMD64是AMD推出的.IA64不兼容原有的32位x86架构指令集,后来被证明这种做法是不成功的,于是Intel发展处IA64e架构,对 ...

  2. python中super关键字的用法

    http://python.jobbole.com/86787/ class A: def __init__(self):    print "enter A"    print ...

  3. [转]C++中引用(&)的用法和应用实例

    from: here 对于习惯使用C进行开发的朋友们,在看到c++中出现的&符号,可能会犯迷糊,因为在C语言中这个符号表示了取地址符,但是在C++中它却有着不同的用途,掌握C++的&符 ...

  4. D3.js 完整的柱形图

    一个完整的柱形图包含三部分:矩形.文字.坐标轴.制作一个实用的柱形图,内容包括:选择集.数据绑定.比例尺.坐标轴等内容. 1. 添加 SVG 画布 //画布大小 var width = 400; va ...

  5. 转!数据库连接池概念、种类、配置(DBCP\C3P0\JndI与Tomact配置连接池)

    数据库连接池概念.种类.配置(DBCP\C3P0\JndI与Tomact配置连接池) 一.DBCP 连接:DBCP 连接池是 Apache 软件基金组织下的一个开源连接池实现. 需要的 java 包c ...

  6. php imagecreatetruecolor输出字符符或验证码

    $img = imagecreatetruecolor(100,100); //创建真彩图像资源 $color = imagecolorAllocate($img,200,200,200); //分配 ...

  7. 有用的dede表单代码

    <form action="" class="demoform">                <table>             ...

  8. 原生js基础问题的一些备忘

    1.在原生js里面  window.onload=function(){}  这个就相当于jquery中 $(document).ready(function(){}); 这样 2.getElemen ...

  9. 3.3 使用Code First数据库迁移

    当Entity Framework Code First的数据模型发生异动时,默认会引发一个System.InvalidOpertaionException异常.一种解决方法是在Global.asax ...

  10. Django开发博客- 三部曲

    其实在django中实现一个功能只需要三个步骤即可,这里我姑且叫它三部曲. 这三部曲就是: 定义urls映射 定义views 定义templates 什么是URL? URL就算一个WEB地址,你在浏览 ...