洛谷 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 ...
随机推荐
- jsp页面String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";作用!!!!!
转自:https://blog.csdn.net/kiwangruikyo/article/details/81130311 <%String path = request.getContext ...
- 人生苦短,请用 Chrome!
在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...
- 解决win8.1下sql配置iis的问题
在配置iis8.5时,ISAPI和CGI限制中没有ASP.NET v4.0.30319, 所以要注册.net 4.0 注册方法为在“运行”中输入cmd,然后在命令行中输入: C:\WINDOWS\Mi ...
- lua_pcall与lua_call之间的区别
lua_pcall与lua_call之间的区别 定义: void lua_call (lua_State *L, int nargs, int nresults); int lua_pcall (lu ...
- Matlab 从入门到精通 Chapter11 文件读取I/O
11.1 工作空间数据读取 将工作空间的变量保存为文件,可以使用save命令. save('filename') 将文件保存在当前目录下,文件名为filename.mat save('filenam ...
- c#获取DataTable某一列不重复的值,或者获取某一列的所有值
实现该功能是用了DataView的筛选功能,DataView表示用于排序.筛选.搜索.编辑和导航的 DataTable 的可绑定数据的自定义视图. 这里做了一个简单易懂的Demo来讲述该方法. 1.建 ...
- tigergao
互联网从业 6 年.前码农&DBA,现运维&电商创业者,也在做自媒体.终生学习者. 运营微信公众号:高哥咋么看 感兴趣的朋友们可以订阅.
- 【Paper Reading】Bayesian Face Sketch Synthesis
Contribution: 1) Systematic interpretation to existing face sketch synthesis methods. 2) Bayesian fa ...
- HDU 3046 Pleasant sheep and big big wolf
Pleasant sheep and big big wolf Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged ...
- ie11 .pac代理脚本无法使用的问题
参考: http://blogs.msdn.com/b/ieinternals/archive/2013/10/11/web-proxy-configuration-and-ie11-changes. ...