题目大意:维护一个森林,支持边的断,连,以及查询连通性

LCT裸题 洛谷P2147传送门

1A了,给自己鼓鼓掌

 #include <cstdio>
#include <algorithm>
#include <cstring>
#define il inline
#define inf 500000
#define N 10100
using namespace std; int n,m,tp;
int stk[N];
struct LinkCutTree{
int fa[N],ch[N][],rv[N];
il int idf(int x){return ch[fa[x]][]==x?:;}
il void con(int x,int ff,int p){fa[x]=ff,ch[ff][p]=x;}
il int isroot(int x){return (ch[fa[x]][]!=x&&ch[fa[x]][]!=x)?:;}
il void rev(int x){swap(ch[x][],ch[x][]),rv[x]^=;}
il void pushdown(int x)
{
if(!rv[x]) return;
if(ch[x][]) rev(ch[x][]);
if(ch[x][]) rev(ch[x][]);
rv[x]=;
}
il void rot(int x)
{
int y=fa[x];int ff=fa[y];int px=idf(x);int py=idf(y);
if(!isroot(y)) ch[ff][py]=x;
fa[x]=ff;con(ch[x][px^],y,px),con(y,x,px^);
//pushup(y),pushup(x);
}
void splay(int x)
{
int y=x;stk[++tp]=x;
while(!isroot(y)){stk[++tp]=fa[y],y=fa[y];}
while(tp){pushdown(stk[tp--]);}
while(!isroot(x))
{
y=fa[x];
if(isroot(y)) rot(x);
else if(idf(y)==idf(x)) rot(y),rot(x);
else rot(x),rot(x);
}
}
void access(int x){for(int y=;x;y=x,x=fa[x])splay(x),ch[x][]=y;/*pushup(x)*/}
il void mkroot(int x){access(x),splay(x),rev(x);}
il void split(int x,int y){mkroot(x),access(y),splay(y);}
int findrt(int x){access(x),splay(x);while(ch[x][])pushdown(x),x=ch[x][];return x;}
il void link(int x,int y){mkroot(x);if(findrt(y)!=x)fa[x]=y;}
il void cut(int x,int y){mkroot(x);if(findrt(y)==x&&fa[x]==y&&!ch[x][])fa[x]=ch[y][]=;/*pushup(y)*/}
}lct;
int gint()
{
int rett=,fh=;char c=getchar();
while(c<''||c>''){if(c=='-')fh=-;c=getchar();}
while(c>=''&&c<=''){rett=(rett<<)+(rett<<)+c-'';c=getchar();}
return rett*fh;
} int main()
{
n=gint(),m=gint();
int fl,x,y;char str[];
for(int i=;i<=m;i++)
{
scanf("%s",str);
x=gint(),y=gint();
if(str[]=='C'){lct.link(x,y);}
if(str[]=='D'){lct.cut(x,y);}
if(str[]=='Q'){
if(lct.findrt(x)==lct.findrt(y))
printf("Yes\n");
else printf("No\n");}
}
return ;
}

BZOJ 2049 [SDOI2008]洞穴勘测 (LCT)的更多相关文章

  1. BZOJ 2049 SDOI2008 洞穴勘测 LCT板子

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2049 题意概述:给出N个点,一开始不连通,M次操作,删边加边,保证图是一个森林,询问两点连 ...

  2. bzoj 2049 Cave 洞穴勘测(LCT)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 动态树入门题,不需要维护任何信息. 我用的是splay,下标实现的lct. #in ...

  3. BZOJ2049[Sdoi2008]洞穴勘测——LCT

    题目描述 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假如 ...

  4. 洛谷P2147[SDOI2008]洞穴勘测(lct)

    题目描述 辉辉热衷于洞穴勘测. 某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假 ...

  5. [SDOI2008] 洞穴勘测 (LCT模板)

    bzoj 2049 传送门 洛谷P2147 传送门 这个大佬的LCT详解超级棒的! Link-Cut Tree的基本思路是用splay的森林维护一条条树链. splay的森林,顾名思义,就是若干spl ...

  6. [LuoguP2147] [SDOI2008]洞穴勘测 (LCT维护连通性)

    题面 传送门:https://www.luogu.org/problemnew/show/P2147 Solution 这题...... 我们可以发现题目要求我们维护一个动态森林,而且只查询连通性.. ...

  7. 洛谷 P2147 [SDOI2008]洞穴勘测 LCT

    Code: #include <cstdio> #include <algorithm> #include <string> #include <cstrin ...

  8. P2147 [SDOI2008]洞穴勘测(LCT)

    P2147 [SDOI2008]洞穴勘测 裸的LCT. #include<iostream> #include<cstdio> #include<cstring> ...

  9. P2147 [SDOI2008]洞穴勘测

    P2147 [SDOI2008]洞穴勘测 思路 没办法,我就是喜欢板子都想发的人 都是基础操作,不多说了 代码 #include <bits/stdc++.h> #define ls ch ...

随机推荐

  1. vue-cli 中eslint代码检查之过滤指定文件

    在需要过滤的文件头部末尾分别添加:/* eslint-disable */,/* eslint-disable no-new */

  2. DNS解析流程原理(图例)

    13台根服务器的dns: 1.root-servers.net198.41.0.4美国2.root-servers.net192.228.79.201美国(另支持IPv6)3.root-servers ...

  3. Elasticsearch 入门 - Modifying Your Data

    index/update/delete 均有大概1秒的缓存时间 Indexing/Replacing Documents curl -X PUT "localhost:9200/custom ...

  4. php循环练习题

    1.通过for循环将数组中值求和.求平均值 <?php $num=[1,2,3,4,5,6,7,8,9]; $sum = 0; for ($i=0,$n=count($num); $i < ...

  5. Tensorflow MNIST 数据集测试代码入门

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50614444 测试代码已上传至GitH ...

  6. C#-WinForm中文本框的中文乱码问题

    上面这句话可以解决textbox中的中文乱码问题   来自为知笔记(Wiz) 附件列表 QQ图片20151218124007.png

  7. 记录magento通过csv文件与zip(图片压缩)上传产品到数据库的过程

    1,前台使用input-file type按钮提交文件到magento指定的控制器,controllers获取.csv文件,因为magento是在zend框架上实现的,可以使用如下代码获取文件的上传信 ...

  8. UVA 10196 Morning Walk(欧拉回路)

    Problem H Morning Walk Time Limit 3 Seconds Kamalis a Motashotaguy. He has got a new job in Chittago ...

  9. Web Service学习-CXF开发Web Service实例demo(一)

    Web Service是什么? Web Service不是框架.更甚至不是一种技术. 而是一种跨平台,跨语言的规范 Web Service解决什么问题: 为了解决不同平台,不同语言所编写的应用之间怎样 ...

  10. httpurlconnection get String

    public static String getJSON(String url, int timeout) { HttpURLConnection c = null; try { long reque ...