博客
关于我
【LCT】P2147 [SDOI2008]洞穴勘测
阅读量:293 次
发布时间:2019-03-03

本文共 1823 字,大约阅读时间需要 6 分钟。

第一道LCT的题目

就是模板

 

#include
using namespace std;const int maxn=2e5;int n,m;struct lct{ struct node { int fa,ch[2],rev; node() {fa=ch[0]=ch[1]=rev=0;} }tree[maxn]; int top,res[maxn]; #define ls(x) tree[x].ch[0] #define rs(x) tree[x].ch[1] int isroot(int x) {return tree[x].fa && (ls(tree[x].fa)==x || rs(tree[x].fa)==x);} void rev(int x) { swap(ls(x),rs(x)); tree[x].rev^=1; } void pushdown(int x) { if(!tree[x].rev) return ; if(ls(x)) rev(ls(x)); if(rs(x)) rev(rs(x)); tree[x].rev^=1; } void rotate(int x) { int y=tree[x].fa,z=tree[y].fa; int k=(x==ls(tree[x].fa)),w=tree[x].ch[k]; if(isroot(y)) tree[z].ch[y==rs(tree[y].fa)] =x; tree[x].ch[k]=y; tree[y].ch[!k]=w; tree[y].fa=x; tree[x].fa=z; if(w) tree[w].fa=y; } void splay(int x) { int tmp=x; res[top=1]=tmp; while(isroot(tmp)) res[++top]=tree[tmp].fa,tmp=tree[tmp].fa; while(top) pushdown(res[top]),top--; while(isroot(x)) { if(isroot(tree[x].fa)) rotate((x==rs(tree[x].fa))^(tree[x].fa==rs(tree[tree[x].fa].fa))?x:tree[x].fa); rotate(x); } } void access(int x) { for(int y=0;x;y=x,x=tree[x].fa) { splay(x); rs(x)=y; } } void makeroot(int x) { access(x); splay(x); rev(x); } int findroot(int x) { access(x); splay(x); pushdown(x); while(ls(x)) { x=ls(x); pushdown(x); } return x; } void link(int x,int y) { if(findroot(x)==findroot(y)) return; makeroot(x); tree[x].fa=y; } void cut(int x,int y) { makeroot(x); access(y); splay(y); if(ls(y)==x) tree[x].fa=ls(y)=0; }}LCT;int main(){ freopen("a.in","r",stdin); freopen("a.out","w",stdout); scanf("%d%d",&n,&m); char op[20]; int x,y; for(int i=1;i<=m;i++) { scanf("%s",op); scanf("%d%d",&x,&y); if(op[0]=='C') LCT.link(x,y); if(op[0]=='D') LCT.cut(x,y); if(op[0]=='Q') { if(LCT.findroot(x)==LCT.findroot(y)) printf("Yes\n"); else printf("No\n"); } } return 0;}

 

转载地址:http://idwm.baihongyu.com/

你可能感兴趣的文章
mysqldump数据库备份无法进行操作只能查询 --single-transaction
查看>>
mysqldump的一些用法
查看>>
mysqli
查看>>
MySQLIntegrityConstraintViolationException异常处理
查看>>
mysqlreport分析工具详解
查看>>
MySQLSyntaxErrorException: Unknown error 1146和SQLSyntaxErrorException: Unknown error 1146
查看>>
Mysql_Postgresql中_geometry数据操作_st_astext_GeomFromEWKT函数_在java中转换geometry的16进制数据---PostgreSQL工作笔记007
查看>>
mysql_real_connect 参数注意
查看>>
mysql_secure_installation初始化数据库报Access denied
查看>>
MySQL_西安11月销售昨日未上架的产品_20161212
查看>>
Mysql——深入浅出InnoDB底层原理
查看>>
MySQL“被动”性能优化汇总
查看>>
MySQL、HBase 和 Elasticsearch:特点与区别详解
查看>>
MySQL、Redis高频面试题汇总
查看>>
MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
查看>>
mysql一个字段为空时使用另一个字段排序
查看>>
MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
查看>>
MYSQL一直显示正在启动
查看>>
MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
查看>>
MySQL万字总结!超详细!
查看>>