#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. 操作符 Thinking in Java 第三章

    3.1 更简单的打印语句 3.2 使用Java操作符 3.3 优先级 *int类型+String类型  直接转换为String类型 3.4 赋值 1. *引用=引用  两个引用指向同一个对象,所以操作 ...

  2. count-the-repetitions

    https://leetcode.com/problems/count-the-repetitions/ 下面是我的方法,结果对的,超时了... package com.company; class ...

  3. 线程池的应用及Callable接口的使用

    public interface Executor { /** * Executes the given command at some time in the future.  The comman ...

  4. 工作流学习——Activiti整体认识二步曲 (zhuan)

    http://blog.csdn.net/zwk626542417/article/details/46594505 ***************************************** ...

  5. OpenGL的glTexCoord2f纹理坐标配置

    纹理坐标配置函数,先看定义: void glTexCoord2f (GLfloat s, GLfloat t); 1.glTexCoord2f()函数 有两个参数:GLfloat s, GLfloat ...

  6. J2EE 第二阶段项目之编写代码(三)

    我的任务就是项目统计. 1 效益统计 1 教育效益统计表 (教育效益统计表,增,改,查看,查) 2 农牧林效益统计表 (农牧林效益统计表,增,改,查看,查) 3 乡村效益统计表    (乡村效益统计表 ...

  7. commonJS — 浏览器操作(for Browser)

    for Browser github: https://github.com/laixiangran/commonJS/blob/master/src/forBrowser.js 代码 /** * C ...

  8. sql cast()和convert()

    19.CAST()函数和CONVERT()函数 CAST()函数可以将某种数据类型的表达式转化为另一种数据类型 CONVERT()函数也可以将制定的数据类型转换为另一种数据类型 19.1 CAST() ...

  9. C++ Primer 第三章 标准库类型vector+迭代器iterator 运算

    1.vector: 标准库类型vector表示对象的集合,其中所有对象的类型都相同,集合中的每个对象都有一个与之对应的索引,索引用于访问对象.因为vector“容纳着”其他对象,所以它也常被称作容器( ...

  10. shell脚本基础知识

    虽然现在能在Linux系统下生存,但是自觉效率太低,和高手有很大的差距. 这就是关于Linux的知识太过匮乏,有很多事情知道该怎么做,但是就是没法在Linux下实现,为了提升工作效率,必须要接触Lin ...