BZOJ 2049 [Sdoi2008]Cave 洞穴勘测
来源:互联网 发布:ubuntu麒麟 编辑:程序博客网 时间:2024/05/01 22:25
动态树裸题,直接放代码
/************************************************************** Problem: 2049 User: vermouth Language: C++ Result: Accepted Time:2188 ms Memory:1724 kb****************************************************************/ #include<cstdio>#include<iostream>using namespace std;const int maxn=21000;int n,m,x,y,sz[maxn],fa[maxn],tree[maxn][2],s[maxn];bool rev[maxn];bool isroot(int x){ return tree[fa[x]][0]!=x&&tree[fa[x]][1]!=x;}inline void pushup(int x){ sz[x]=sz[tree[x][0]]+sz[tree[x][1]]+1;}inline void pushdown(int x){ if(rev[x]) { rev[x]^=1;rev[tree[x][0]]^=1;rev[tree[x][1]]^=1; swap(tree[x][0],tree[x][1]); }}void rotate(int x){ int y=fa[x],z=fa[y],l=tree[y][1]==x,r=l^1; if(!isroot(y)) tree[z][tree[z][1]==y]=x; fa[x]=z;fa[y]=x;fa[tree[x][r]]=y; tree[y][l]=tree[x][r];tree[x][r]=y; pushup(y);pushup(x);}void splay(int x){ int top=0;s[++top]=x; for(int i=x;!isroot(i);i=fa[i]) { s[++top]=fa[i]; } for(int i=top;i;i--) pushdown(s[i]); while(!isroot(x)) { int y=fa[x],z=fa[y]; if(!isroot(y)) { if(tree[y][0]==x^tree[z][0]==y) rotate(y);else rotate(x); } rotate(x); }}void access(int x){ int t=0; while(x) { splay(x); tree[x][1]=t; t=x;x=fa[x]; }}void rever(int x){ access(x);splay(x);rev[x]^=1; }void link(int x,int y){ rever(x);fa[x]=y;splay(x); } void cut(int x,int y){ rever(x);access(y);splay(y);tree[y][0]=fa[x]=0;}int find(int x){ access(x);splay(x); int y=x; while(tree[y][0]) y=tree[y][0]; return y;}char op[10];int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) { scanf("%s",op); scanf("%d%d",&x,&y); if(op[0]=='C') link(x,y); else if(op[0]=='Q') { if(find(x)==find(y)) puts("Yes");else puts("No"); } else if(op[0]=='D') cut(x,y); } return 0;}/*200 5Query 123 127Connect 123 127Query 123 127Destroy 127 123Query 123 127*/
0 0
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 LCT
- 【BZOJ 2049】 [Sdoi2008]Cave 洞穴勘测
- BZOJ 2049 [Sdoi2008]Cave 洞穴勘测 LCT
- 【LCT】BZOJ 2049:[Sdoi2008]Cave洞穴勘测
- bzoj 2049: [Sdoi2008]Cave 洞穴勘测
- BZOJ 2049 [Sdoi2008]Cave 洞穴勘测
- BZOJ 2049 [Sdoi2008]Cave 洞穴勘测
- BZOJ 2049 [Sdoi2008]Cave 洞穴勘测
- BZOJ 2049 [Sdoi2008]Cave 洞穴勘测
- Bzoj 2049: [Sdoi2008]Cave 洞穴勘测(LCT)
- bzoj 2049 [Sdoi2008]Cave 洞穴勘测
- BZOJ 2049 [Sdoi2008] Cave 洞穴勘测
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测(LCT)
- bzoj 2049: [Sdoi2008]Cave 洞穴勘测
- bzoj 2049: [Sdoi2008]Cave 洞穴勘测
- bzoj 2049 [Sdoi2008]Cave 洞穴勘测 LCT
- [bzoj-2049][Sdoi2008]Cave 洞穴勘测 题解
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测
- arm-linux安排开发环境(一)安装ssh server
- 小米手机连接USB安装应用出错
- Reporting services匿名访问配置
- LinuxWorld大派奖
- 第一天,我会坚持的
- BZOJ 2049 [Sdoi2008]Cave 洞穴勘测
- 【GStreamer开发】GStreamer基础教程12——流
- Uva11214 Guarding the Chessboard【dfs回溯】【习题7-10】
- 字符设备驱动、平台设备驱动、设备驱动模型、sysfs的比较和关联
- 移动WEB基础知识
- 深度估计软件DERS6.1使用方法
- gym100796C(想法题/二分+树形dp)
- SSM框架下的用户登录系统,并实现增删改
- 机器学习实践系列之9 - 视频结构化