洛谷 P2147 [SDOI2008]洞穴勘测 LCT
Code:
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring> using namespace std; void setIO(string a){
freopen((a+".in").c_str(),"r",stdin);
} #define maxn 10008 int n,m;
struct Link_Cut_Tree{ int ch[maxn][2],f[maxn];
int tag[maxn],sta[maxn];
int get(int x)
{
return ch[f[x]][1]==x;
}
int which(int x)
{
return ch[f[x]][1]==x;
}
int isRoot(int x)
{
return !(ch[f[x]][1]==x||ch[f[x]][0]==x);
}
int lson(int x)
{
return ch[x][0];
}
int rson(int x)
{
return ch[x][1];
}
void mark(int x)
{
if(!x) return;
swap(ch[x][0],ch[x][1]), tag[x]^=1;
}
void pushdown(int x)
{
if(tag[x]) mark(lson(x)), mark(rson(x)),tag[x]=0;
}
void rotate(int x)
{
int old=f[x],fold=f[old],which=get(x);
if(!isRoot(old)) ch[fold][ch[fold][1]==old]=x;
ch[old][which]=ch[x][which^1],f[ch[old][which]]=old;
ch[x][which^1]=old,f[old]=x,f[x]=fold;
}
void splay(int x)
{
int v=0,u=x;
sta[++v]=u;
while(!isRoot(u)) sta[++v]=f[u],u=f[u];
while(v) pushdown(sta[v--]);
u=f[u];
for(int fa;(fa=f[x])!=u;rotate(x))
if(f[fa]!=u) rotate(get(fa)==get(x)?fa:x);
}
void Access(int x)
{
for(int y=0;x;y=x,x=f[x])
splay(x),ch[x][1]=y;
}
void makeRoot(int x)
{
Access(x), splay(x), mark(x);
}
void link(int a,int b)
{
makeRoot(a), f[a]=b;
}
int findRoot(int a)
{
Access(a),splay(a);
while(ch[a][0]) a=ch[a][0];
return a;
}
void cut(int a,int b)
{
makeRoot(a),Access(b),splay(b);
f[a]=ch[b][0]=0;
}
bool judge(int a,int b)
{
int x=findRoot(a),y=findRoot(b);
if(x!=y)return true;
return false;
}
}tree; int main()
{
//setIO("input");
scanf("%d%d",&n,&m);
char opt[20];
int a,b;
while(m--)
{
scanf("%s%d%d",opt,&a,&b);
switch(opt[0])
{
case 'C':
{
if(tree.judge(a,b)) tree.link(a,b);
break;
}
case 'Q':
{
bool ans=tree.judge(a,b);
if(ans==0) printf("Yes\n");
else printf("No\n");
break;
}
case 'D':
{
tree.cut(a,b);
break;
}
}
}
return 0;
}
洛谷 P2147 [SDOI2008]洞穴勘测 LCT的更多相关文章
- 洛谷P2147[SDOI2008]洞穴勘测(lct)
题目描述 辉辉热衷于洞穴勘测. 某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假 ...
- 洛谷P2147 [SDOI2008] 洞穴勘探 [LCT]
题目传送门 洞穴勘探 题目描述 辉辉热衷于洞穴勘测. 某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道 ...
- [洛谷P2147][SDOI2008]洞穴勘测
题目大意:有$n$个洞穴,$m$条指令,指令有三种 $Connect\;u\;v$:在$u,v$之间连一条边 $Destroy\;u\;v$:切断$u,v$之间的边 $Query\;u\;v$:询问$ ...
- 洛谷 P2147 [SDOI2008]洞穴勘测
以下这个做法应该是叫线段树分治... 根据修改操作预处理出每条边存在的时间区间[l,r](以操作序号为时间),然后把所有形式化后的修改挂到线段树节点上. 处理完修改后,dfs一遍线段树,进入某个节点时 ...
- 洛谷 P2147 [SDOI2008]洞穴勘测 (线段树分治)
题目链接 题解 早就想写线段树分治的题了. 对于每条边,它存在于一段时间 我们按时间来搞 我们可把一条边看做一条线段 我们可以模拟线段树操作,不断分治下去 把覆盖\(l-r\)这段时间的线段筛选出来, ...
- 【洛谷P2147】洞穴勘测
题目大意:维护 N 个点的无向图,支持动态加边和删边,回答两点的连通性. 题解:线段树分治 + 可撤销并查集 询问可以离线,这是线段树分治的基础. 建立在操作时间轴上的线段树称为线段树分治算法. 本题 ...
- P2147 [SDOI2008]洞穴勘测(LCT)
P2147 [SDOI2008]洞穴勘测 裸的LCT. #include<iostream> #include<cstdio> #include<cstring> ...
- P2147 [SDOI2008]洞穴勘测
P2147 [SDOI2008]洞穴勘测 思路 没办法,我就是喜欢板子都想发的人 都是基础操作,不多说了 代码 #include <bits/stdc++.h> #define ls ch ...
- BZOJ 2049 [SDOI2008]洞穴勘测 (LCT)
题目大意:维护一个森林,支持边的断,连,以及查询连通性 LCT裸题 洛谷P2147传送门 1A了,给自己鼓鼓掌 #include <cstdio> #include <algorit ...
随机推荐
- 官方文档 Upgrading Elasticsearch
Upgrading Elasticsearch Before upgrading Elasticsearch: Consult the breaking changes docs. Use the E ...
- MySQL循环语句之while循环测试
转自:http://www.nuoweb.com/database/7614.html MySQL有循环语句操作,while 循环.loop循环和repeat循环,目前我只测试了 while 循环,下 ...
- js小知识 delete操作符
说明:delete操作符用于删除对象的某个属性. 语法: delete object.property //删除 对象.属性 delete object['property'] //删除 对象['属性 ...
- vmware workstation pro 14 虚拟机无法开启、黑屏的解决方案汇总
方案1:卸载鲁大师,重启. 方案2:管理员命令行,输入netsh winsock reset,重启. 方案3:360安全管家修复LSP,重启. 方案4:卸载14.0,安装12.0,手动导入虚拟机.
- 【原创】ApacheTomcat集群在Linux下的搭建步骤
在RedHat5(以下简称RH)上搭建ApacheTomcat的集群环境,有以下步骤: 1.首先安装apr和apr-util apr-util需要依赖于apr包,所以先安装apr, http://fi ...
- POJ 1664 放苹果【DFS】
题意:给出n个苹果,m个盘子,问有多少种不同的苹果放置方法 可以把它抽象为把一个数n,拆分成a1,a2,a3,---,am,使得它们的和为n, 话说这一题是学习的ppt里面的,它的思路就是搜索 搜索条 ...
- seq去除重复数据
DELETE FROM temp_fjh_2 a WHERE a.rowid!=(SELECT MAX(b.rowid) FROM temp_fjh_2 b WHERE a.a=b.a); 表名和列名 ...
- [NOI2018]你的名字(68pts) 后缀自动机
讲解在满分做法的博客中 Code: #include <cstdio> #include <algorithm> #include <cstring> #defin ...
- 微软的鼠标 Microsoft mouse
微软是做软件出身的厂商, 所以微软开发的软件质量毋庸置疑,Windows操作系统还有诸如Office的办公软件拥有庞大的用户群. 微软家的Visual Studio也被号称宇宙最强IDE,我个人也每天 ...
- 【BZOJ4444】国旗计划 - 决策单调性
Description A国正在开展一项伟大的计划——国旗计划.这项计划的内容是边防战士手举国旗环绕边境线奔袭一圈.这项计划需要多名边防战士以接力的形式共同完成,为此,国土安全局已经挑选了N名优秀的边 ...