#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. 关于inline-block 元素之间为何会产生间隔

    关于inline-block 元素之间为何会产生间隔 现象: <body> <input type="text"> <input type=" ...

  2. Data truncated for column xxx

    对于字段XXX,数据发生截断.原因是:字段的取值,不满足约束条件.比如下面的情况: 原来的字段取值为null,现在约束字段not null,就会报错Data truncated for column ...

  3. JavaWeb学习总结(十四)--Apache的DBUtils

    一.commons-dbutils简介 commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbutils能极大简化 ...

  4. ajax跨域jsonp

    <!DOCTYPE html><html lang="zh-CN"><head> <meta charset="UTF-8&qu ...

  5. Css_Backgroud-position(背景图片)定位问题详解

    background-position的说明:    设置或检索对象的背景图像位置.必须先指定 background-image 属性.该属性定位不受对象的补丁属性( padding )设置影响.   ...

  6. hiho_1053_居民迁移

    题目大意 有N个居民点在一条直线上,每个居民点有一个x表示坐标,y表示居民点的现有居民数.现在要求将居民点的居民重新分配,每个居民点的居民最远迁移的距离为R,要求分配完之后,居民点中居民数最多的居民点 ...

  7. Graph-tool简介 - wiki

    graph-tool is a Python module for manipulation and statistical analysis of graphs[disambiguation nee ...

  8. [ 转]Android快速开发–使用ORMLite操作数据库

    OrmLite是一个数据库操作辅助的开源框架,主要面向Java语言.在Android面向数据库开发中,是一个比较流行的开源框架,方便操作而且功能强大,今天来学习一下,最近的项目中也有所涉及,写个博客来 ...

  9. PS去掉图片上的文字的6种基本方法,动态教程

    1.使用仿制图章工具去除文字这是比较常用的方法.具体的操作是,选取仿制图章工具,按住Alt键,在无文字区域点击相似的色彩或图案采样,然后在文字区域拖动鼠标复制以复盖文字.要注意的是,采样点即为复制的起 ...

  10. sqlserver计算表使用大小sql

    ) create table #spt_space ( ) null, [rows] int null, ) null, ) null, ) null, ) null ) set nocount on ...