BZOJ 2049 [Sdoi2008]Cave 洞穴勘测(动态树)
【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=2049
【题目大意】
要求支持树的断边和连边,以及连接查询
【题解】
LCT练习题
【代码】
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=10010;
namespace Link_Cut_Tree{
int f[N],son[N][2],val[N],sum[N],tmp[N],Xor[N];bool rev[N];
void Initialize(){
memset(f,0,sizeof(f));
memset(son,0,sizeof(son));
memset(val,0,sizeof(val));
memset(rev,0,sizeof(rev));
memset(sum,0,sizeof(sum));
memset(Xor,0,sizeof(Xor));
}
bool isroot(int x){return !f[x]||son[f[x]][0]!=x&&son[f[x]][1]!=x;}
void rev1(int x){if(!x)return;swap(son[x][0],son[x][1]);rev[x]^=1;}
void pb(int x){if(rev[x])rev1(son[x][0]),rev1(son[x][1]),rev[x]=0;}
void up(int x){
sum[x]=Xor[x]=val[x];
if(son[x][0])sum[x]+=sum[son[x][0]];
if(son[x][1])sum[x]+=sum[son[x][1]];
if(son[x][0])Xor[x]^=Xor[son[x][0]];
if(son[x][1])Xor[x]^=Xor[son[x][1]];
}
void rotate(int x){
int y=f[x],w=son[y][1]==x;
son[y][w]=son[x][w^1];
if(son[x][w^1])f[son[x][w^1]]=y;
if(f[y]){
int z=f[y];
if(son[z][0]==y)son[z][0]=x;else if(son[z][1]==y)son[z][1]=x;
}f[x]=f[y];f[y]=x;son[x][w^1]=y;up(y);
}
void splay(int x){
int s=1,i=x,y;tmp[1]=i;
while(!isroot(i))tmp[++s]=i=f[i];
while(s)pb(tmp[s--]);
while(!isroot(x)){
y=f[x];
if(!isroot(y)){if((son[f[y]][0]==y)^(son[y][0]==x))rotate(x);else rotate(y);}
rotate(x);
}up(x);
}
void access(int x){for(int y=0;x;y=x,x=f[x])splay(x),son[x][1]=y,up(x);}
// 查询x所在的树的根
int root(int x){access(x);splay(x);while(son[x][0])x=son[x][0];return x;}
// 使x成为根
void makeroot(int x){access(x);splay(x);rev1(x);}
// 将x和y所属树合并
void link(int x,int y){makeroot(x);f[x]=y;access(x);}
// 将x和其父节点分开
void cutf(int x){access(x);splay(x);f[son[x][0]]=0;son[x][0]=0;up(x);}
// 将边x-y切断
void cut(int x,int y){makeroot(x);cutf(y);}
// 查询x到y的链和
int ask(int x,int y){makeroot(x);access(y);splay(y);return sum[y];}
// 计算x到y的xor和
int xorsum(int x,int y){makeroot(x);access(y);splay(y);return Xor[y];}
// 查询节点到根的距离
int query(int x){access(x);splay(x);return sum[x];}
// 将x为下标的值改为y
int change(int x,int y){makeroot(x);val[x]=y;up(x);}
// 将x的父亲改为y
int changef(int x,int y){cutf(x);f[x]=y;}
}
char op[10];
int n,m,x,y;
int main(){
scanf("%d%d",&n,&m);
using namespace Link_Cut_Tree;
while(m--){
scanf("%s%d%d",op,&x,&y);
if(op[0]=='C')link(x,y);
else if(op[0]=='D')cut(x,y);
else puts(root(x)==root(y)?"Yes":"No");
}return 0;
}
BZOJ 2049 [Sdoi2008]Cave 洞穴勘测(动态树)的更多相关文章
- bzoj 2049: [Sdoi2008]Cave 洞穴勘测 动态树
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 3119 Solved: 1399[Submit] ...
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 (动态树入门)
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1528 Solved: 644[Submit][ ...
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 LCT
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnli ...
- bzoj 2049: [Sdoi2008]Cave 洞穴勘测 (LCT)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2049 题面: 2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 ...
- bzoj 2049 [Sdoi2008]Cave 洞穴勘测(LCT)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2049 [题意] 给定森林,可能有连边或断边的操作,回答若干个连通性的询问. [思路] ...
- [BZOJ 2049] [Sdoi2008] Cave 洞穴勘测 【LCT】
题目链接:BZOJ - 2049 题目分析 LCT的基本模型,包括 Link ,Cut 操作和判断两个点是否在同一棵树内. Link(x, y) : Make_Root(x); Splay(x); F ...
- 【刷题】BZOJ 2049 [Sdoi2008]Cave 洞穴勘测
Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测——LCT
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2049 省选之前来切一道数据结构模板题. 题意 这是一道模板题. N个点,M次操作,每次加边/ ...
- ●BZOJ 2049 [Sdoi2008]Cave洞穴勘测
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2049 题解: LCT入门题 就是判两个点是否在同一颗树里 代码: #include<c ...
随机推荐
- 将文件内容导入到MySQL中
1.作用 把文件系统的内容导入到数据库中 2.语法 load data infile "文件名" into table 表名 fields terminated by " ...
- Vue 键盘事件
Vue2键盘事件:keydown/keyup... 1.使用 <!DOCTYPE html> <html> <head> <title></tit ...
- Python3.3.3 安装(Linux系统)
1.wget http://www.python.org/ftp/python/3.3.3/Python-3.3.3.tgz //检查http://www.python.org/ftp/python网 ...
- hdu 2059 龟兔赛跑(动态规划DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2059 龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others) M ...
- 【EverydaySport】健身笔记——动态牵拉
动态牵拉,包含了动态和牵拉两个概念.动态牵拉要求牵拉的过程要伴随着走路,即行进中牵拉,动态牵拉不仅可以使得肌肉得到延展,还可以激活肌肉协同工作,发展协调性.灵活性.进行动态牵拉时每个动作要进行5~10 ...
- 【Git/GitHub学习笔记】基本操作——创建仓库,本地、远程同步等
近日想分享一些文件,但是用度盘又太麻烦了(速度也很恶心).所以突发奇想去研究了下GitHub的仓库,这篇文章也就是一个最最最基础的基本操作.基本实现了可以在GitHub上存储文本信息与代码. 由于我的 ...
- Linux抓包工具tcpdump
Linux抓包工具tcpdump http://www.xuchanggang.cn/archives/1107.html
- sicily 1016. 排队接水--课程作业
1016. 排队接水 Time Limi ...
- C++中STL容器的比较
基本参考 https://blog.csdn.net/qq_14898543/article/details/51381642 容器特性: vector:典型的序列容器,C++标准严格要求次容器的实现 ...
- OC 01 类和对象
一. 定义OC的类和创建OC的对象 接下来就在OC中模拟现实生活中的情况,创建一辆车出来.首先要有一个车子类,然后再利用车子类创建车子对象 要描述OC中的类稍微麻烦一点,分2大步骤:类的声明.类的实 ...