直接把x设为根,然后查询y所在联通块的根是不是x就行了.

CODE

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
template<typename T>inline void read(T &num) {
char ch; int flg = 1;
while((ch=getchar())<'0'||ch>'9')if(ch=='-')flg=-flg;
for(num=0;ch>='0'&&ch<='9';num=num*10+ch-'0',ch=getchar());
num*=flg;
}
const int MAXN = 10005;
int n, q;
namespace LCT {
#define ls ch[x][0]
#define rs ch[x][1]
int ch[MAXN][2], fa[MAXN], sz[MAXN];
bool rev[MAXN];
inline bool isr(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; }
inline bool get(int x) { return x == ch[fa[x]][1]; }
inline void upd(int x) {
sz[x] = sz[ls] + sz[rs] + 1;
}
inline void rot(int x) {
int y = fa[x], z = fa[y], l = get(x), r = l^1;
if(!isr(y)) ch[z][get(y)] = x;
fa[ch[x][r]] = y; fa[y] = x; fa[x] = z;
ch[y][l] = ch[x][r]; ch[x][r] = y;
upd(y), upd(x);
}
inline void mt(int x) { if(rev[x]) rev[x] ^= 1, rev[ls] ^= 1, rev[rs] ^= 1, swap(ls, rs); }
void mtpath(int x) { if(!isr(x)) mtpath(fa[x]); mt(x); }
inline void splay(int x) {
mtpath(x);
for(; !isr(x); rot(x))
if(!isr(fa[x])) rot(get(x)==get(fa[x])?fa[x]:x);
}
inline int access(int x) { int y=0;
for(; x; x=fa[y=x]) splay(x), ch[x][1]=y, upd(x);
return y;
}
inline void bert(int x) { access(x), splay(x), rev[x] ^= 1; }
inline int sert(int x) {
access(x), splay(x);
for(; ch[x][0]; x=ch[x][0]);
return x;
}
inline void link(int x, int y) {
bert(x);
if(sert(y) == x) return;
fa[x] = y;
}
inline void cut(int x, int y) {
bert(x), access(y), splay(y);
if(sert(y) != x || fa[x] != y || ch[x][1] != 0) return;
fa[x] = ch[y][0] = 0; upd(y);
}
inline void modify(int x, int val) {
access(x), splay(x);
ch[x][0] = fa[ch[x][0]] = 0; upd(x);
if(x + val <= n) link(x, x+val);
}
inline int split(int x, int y) {
bert(x), access(y), splay(y);
return y;
}
inline int query(int x, int y) {
bert(x);
return sert(y) == x;
}
}
using namespace LCT;
int main () {
read(n), read(q);
char s[10];
int x, y;
while(q--) {
scanf("%s", s), read(x), read(y);
if(s[0] == 'Q') puts(query(x, y) ? "Yes" : "No");
else if(s[0] == 'C') link(x, y);
else cut(x, y);
}
}

BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 (LCT维护连通性)的更多相关文章

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

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

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

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

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

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

  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 洞穴勘测 动态树

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

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

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

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

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

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

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

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

    [题意]给定n个点和m个操作,每次操作:1.连接2个点.2.断开2个点.3.查询2个点是否连通.m<=2*10^5. [算法]Link-Cut Tree [题解]LCT模板题,Link,Cut, ...

随机推荐

  1. Hadoop之HDFS介绍

    1. 概述 HDFS是一种分布式文件管理系统. HDFS的使用场景: 适合一次写入,多次读出的场景,且不支持文件的修改: 适合用来做数据分析,并不适合用来做网盘应用: 1.2 优缺点 优点: 高容错性 ...

  2. 使用mybatis出现异常:invalid comparison: java.time.LocalDateTime and java.lang.String

    整了半天终于找到问题所在:在mapper文件中,对该参数进行了和字符串的对比,如下: <if test="startTime != null and startTime != '' a ...

  3. Ubuntu18.04中安装Python3.7教程

    Ubuntu18.04中安装Python3.7教程 链接https://blog.csdn.net/weixin_42056625/article/details/82970358

  4. 第十章 MIZ702 ZYNQ制作UBOOT固化程序

    10.0难度系数★☆☆☆☆☆☆ 10.1是什么是固化 我们前几章将的程序都是通过JTAG先下载bit流文件,再下载elf文件,之后点击Run As来运行的程序.JTAG的方法是通过TCL脚本来初始化P ...

  5. bzoj 2734 集合悬殊 (状压dp)

    大意: 给定$n$, 求集合{1,2,...n}的子集数, 满足若$x$在子集内, 则$2x,3x$不在子集内. 记$f(x)$为$x$除去所有因子2,3后的数, 那么对于所有$f$值相同的数可以划分 ...

  6. docker 入门5 - 栈 【翻译】

    入门,第 5 部分:堆栈 先决条件 安装 Docker 版本 1.13 或更高版本. 获取第 3 部分先决条件中所述的 Docker Compose. 获取 Docker Machine,如第 4 部 ...

  7. IP 、127.0.0.1、localhost 三者区别

    一.Ping命令 1.Ping命令,用来检查两台物理机间的TCP/IP网络是否通畅或者网络连接速度,是TCP/IP协议的一部分. 2.PING (Packet Internet Groper),因特网 ...

  8. Angular7如何动态刷新Echarts图表

    1 概述 echarts是百度的开源图表插件 Angular中引入echarts网上教程很多 Angular引入echarts,并使用动态刷新 2 安装 请参考大神的博客:https://blog.c ...

  9. winfrom_权限设置_TreeView的相关问题

    1.获取TreeView的值: 循环TreeView,获取checked每个节点的Text,串起来用逗号“,”隔开,保存到数据库. List<string> list = new List ...

  10. JS基础_函数的返回值

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...