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 ...
随机推荐
- div 超出高度滚动条,超出宽度点点点
超出高度滚动条 style="width:230px; height: 180px; overflow: auto;" 超出宽度点点点 style="width: 220 ...
- CSS 竖线 点 时间节点
效果如图 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- python 实现二叉树相关算法
一.构建与遍历二叉树 基本性质 1)在二叉树的第i层上最多有2i-1 个节点 .(i>=1)2)二叉树中如果深度为k,那么最多有2k-1个节点.(k>=1)3)在完全二叉树中,具有n个节点 ...
- arch中pacman的使用
Pacman 是archlinux 下的包管理软件.它将一个简单的二进制包格式和易用的构建系统结合了起来.不管软件包是来自官方的 Arch 库还是用户自己创建,Pacman 都能方便得管理. pacm ...
- perl6中的替换
use v6; =begin pod perl6 中的替换用S/// S有几个可选参数: :g —(长形式::global)全局匹配:替换掉所有的出现 :i —不区分大小写的匹配 :ii —(长形式: ...
- ubuntu下virtualbox安装freebsd及初步配置
最近尝试了在虚拟机中安装freebsd并进行尝试性的使用 获取镜像 在freebsd的官网,https://www.freebsd.org,即可看到 "Download Freebsd&qu ...
- python基础===创建大量对象是节省内存方法
问题: 你的程序要创建大量(可能上百万) 的对象,导致占用很大的内存. 解决方案: 对于主要是用来当成简单的数据结构的类而言,你可以通过给类添加__slots__属性来极大的减少实例所占的内存.比如: ...
- Python Matplotlib图表汉字显示成框框的解决办法
http://blog.sina.com.cn/s/blog_662dcb820102vu3d.html http://blog.csdn.net/fyuanfena/article/details/ ...
- Vim中的键映射【转】
转自:http://www.cnblogs.com/softwaretesting/archive/2011/09/28/2194515.html http://www.pythonclub.org/ ...
- javascript方法--apply()
今天琢磨了一下apply,以前对这个方法觉得比较懵,今天一琢磨确实觉得挺好玩的. 一开始把MDN的apply文档看了一遍,感觉不是很理解,而且有一些东西也是知道但是比较模糊,所以还是一步一步来,不懂查 ...