bzoj2049 [Sdoi2008]Cave 洞穴勘测(lct)
2018-01-31 16:40
369 查看
只有link和cut的裸题。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define ll long long #define inf 0x3f3f3f3f #define N 10010 inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*f; } int n,m,fa ,c [2],q ;bool rev ; inline bool isroot(int x){ return x!=c[fa[x]][0]&&x!=c[fa[x]][1]; } inline void pushdown(int p){ if(!rev[p]) return;rev[p]=0; int l=c[p][0],r=c[p][1]; rev[l]^=1;swap(c[l][0],c[l][1]); rev[r]^=1;swap(c[r][0],c[r][1]); } inline void rotate(int x){ int y=fa[x],z=fa[y],l=x==c[y][1],r=l^1; if(!isroot(y)) c[z][y==c[z][1]]=x; fa[c[x][r]]=y;fa[y]=x;fa[x]=z; c[y][l]=c[x][r];c[x][r]=y; } inline void splay(int x){ int top=0;q[++top]=x; for(int xx=x;!isroot(xx);xx=fa[xx]) q[++top]=fa[xx]; while(top) pushdown(q[top--]); while(!isroot(x)){ int y=fa[x],z=fa[y]; if(!isroot(y)){ if(x==c[y][1]^y==c[z][1]) rotate(x); rotate(y); }rotate(x); } } inline void access(int x){ int y=0; while(x){ splay(x);c[x][1]=y;y=x;x=fa[x]; } } inline void makeroot(int x){ access(x);splay(x);rev[x]=1;swap(c[x][0],c[x][1]); } inline void link(int x,int y){ makeroot(x);fa[x]=y; } inline void cut(int x,int y){ makeroot(x);access(y);splay(y); c[y][0]=fa[x]=0; } inline int find(int x){ access(x);splay(x);while(c[x][0]) x=c[x][0];return x; } inline void query(int x,int y){ if(find(x)==find(y)) puts("Yes"); else puts("No"); } int main(){ // freopen("a.in","r",stdin); n=read();m=read(); while(m--){ char op[10];scanf("%s",op+1); int x=read(),y=read(); if(op[1]=='C') link(x,y); else if(op[1]=='D') cut(x,y); else query(x,y); }return 0; }
相关文章推荐
- [bzoj2049][Sdoi2008]Cave 洞穴勘测——lct
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 LCT題解
- BZOJ-2049 2049: [Sdoi2008]Cave 洞穴勘测 LCT||并查集||可撤销并查集+时间分治
- BZOJ_2049_[Sdoi2008]Cave 洞穴勘测_LCT
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测(LCT)
- bzoj2049 [Sdoi2008]Cave 洞穴勘测 lct
- 【BZOJ2049】【SDOI2008】Cave 洞穴勘测 LCT裸题 模版题 数组版
- 【LCT】【SDOI 2008】【bzoj 2049】Cave 洞穴勘测
- _bzoj2049 [Sdoi2008]Cave 洞穴勘测【LCT】
- 【BZOJ】2049 [Sdoi2008]Cave 洞穴勘测 LCT
- BZOJ 2049 [Sdoi2008]Cave 洞穴勘测 LCT
- 【lct】bzoj2049 [Sdoi2008]Cave 洞穴勘测
- 【BZOJ】2049 [Sdoi2008]Cave 洞穴勘测 LCT入门题
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测(LCT裸题)
- [BZOJ2049][[Sdoi2008]Cave 洞穴勘测][LCT]
- 【LCT】BZOJ2049 [SDOI2008]Cave 洞穴勘测
- bzoj 2049: [Sdoi2008]Cave 洞穴勘测 LCT
- 【LCT】BZOJ2049 [Sdoi2008]Cave 洞穴勘测
- bzoj 2049: [Sdoi2008]Cave 洞穴勘测(LCT模板)
- BZOJ 2049([Sdoi2008]Cave 洞穴勘测-LCT)[Template:LCT]