题目大意:有$n$个洞穴,$m$条指令,指令有三种

  1. $Connect\;u\;v$:在$u,v$之间连一条边
  2. $Destroy\;u\;v$:切断$u,v$之间的边
  3. $Query\;u\;v$:询问$u,v$是否连通

(数据保证合法)

题解:$LCT$(潘佳奇的板子)

卡点:无(潘佳奇的板子)

C++ Code:

#include <cstdio>
#include <cstring>
#define maxn 10010
using namespace std;
int son[2][maxn], fa[maxn], tg[maxn];
inline bool get(int x, int flag = 1) {return son[flag][fa[x]] == x;}
inline bool is_root(int x) {return !(get(x, 0) || get(x, 1));}
inline void swap(int &x, int &y) {x ^= y ^= x ^= y;}
inline void rotate(int x) {
int y = fa[x], z = fa[y]; bool b = get(x);
if (!is_root(y)) son[get(y)][z] = x;
son[b][y] = son[!b][x]; son[!b][x] = y;
fa[y] = x; fa[x] = z; fa[son[b][y]] = y;
}
inline void pushdown(int x) {
if (tg[x]) {
tg[son[0][x]] ^= 1; tg[son[1][x]] ^= 1;
swap(son[0][x], son[1][x]); tg[x] ^= 1;
}
}
int st[maxn], top;
inline void splay(int x) {
st[top = 1] = x;
for (int y = x; !is_root(y); st[++top] = y = fa[y]);
for (; top; --top) if (tg[st[top]]) pushdown(st[top]);
for (; !is_root(x); rotate(x)) if (!is_root(fa[x]))
get(x) ^ get(fa[x]) ? rotate(x) : rotate(fa[x]);
}
inline void access(int x) {for (int t = 0; x; son[1][x] = t, t = x, x = fa[x]) splay(x);}
inline void make_root(int x) {access(x); splay(x); tg[x] ^= 1;}
inline void split(int x, int y) {make_root(x); access(y); splay(y);}
inline void link(int x, int y) {make_root(x); fa[x] = y;}
inline void cut(int x, int y) {split(x, y); son[0][y] = fa[x] = 0;}
inline bool query(int x, int y) {
split(x, y); int now = y;
while (son[0][now]) now = son[0][now];
return now == x;
}
int n, Q, x, y;
char s[30];
int main() {
scanf("%d%d", &n, &Q);
while (Q --> 0) {
scanf("%s%d%d", s, &x, &y);
if (s[0] == 'Q') {
if (query(x, y)) puts("Yes");
else puts("No");
} else {
if (s[0] == 'C') link(x, y);
else cut(x, y);
}
}
return 0;
}

最新文章

  1. $.getJSON(&#39;url&#39;,function(data){}) 中回调函数不执行
  2. 《超级IP》:伪理论,没能比现有的市场营销理论更高明,只敢勉强去解释已经发生的事情,不敢去预测未来。2星。
  3. 【原】iOS学习38网络之数据解析
  4. OpenGL 小游戏 贪吃蛇1(2D)
  5. [转]仿World Wind构造自己的C#版插件框架——WW插件机制精简改造
  6. mfc控件学习
  7. Masonry自动布局
  8. ORACLE impdp 导入数据
  9. 区间求mex的两种方法
  10. C++ #pragma 预处理指令
  11. hibernate 映射&lt;三&gt;一对一外键键关联
  12. Visual Studio 2010 中的 Web 开发
  13. Windows Azure 网站上的 WebSocket 简介
  14. APUE 1 -- Unix数据结构
  15. 同步请求和异步请求的区别,ajax异步请求如何理解
  16. [C++]字符串相关操作
  17. Null hypothesis TypeⅠerror Type Ⅱ error
  18. readv writev示例程序
  19. C++ vector 排序
  20. 通过python构建集中式的病毒扫描机制

热门文章

  1. CSS3--j惊艳到你的新前端
  2. JDK9 新特性
  3. 堆数据结构(heapq)简单应用
  4. Go web表单
  5. linux实验-基本指令1
  6. 为什么我要放弃javaScript数据结构与算法(第三章)—— 栈
  7. PRO*C 函数事例 2 -- 数据库操作
  8. PS作业
  9. C#窗口抖动
  10. Java Web前后端分离的思考与实践