[bzoj-2049][Sdoi2008]Cave 洞穴勘测 题解
来源:互联网 发布:linux配置javahome 编辑:程序博客网 时间:2024/05/18 00:53
题目传送门
这题就是一个dalao告诉我的lct裸题,用来练习模板,然而一次都没写过的我果断去看了hzw学长的代码,以此为模板练练手(然而还是不是很会写)
代码:
#include<iostream>#include<cmath>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#define rep(i,a,n) for (int i=a;i<=n;i++)#define per(i,a,n) for (int i=a;i>=n;i--)#define Clear(a,x) memset(a,x,sizeof(a))#define ll long long#define INF 2000000000#define eps 1e-8using namespace std;ll read(){ ll x=0,f=1; char ch=getchar(); while (ch<'0'||ch>'9') f=ch=='-'?-1:f,ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*f;}const int maxn=10005;int n,m;int fa[maxn],c[maxn][2],q[maxn];bool rev[maxn];char ch[10];bool isroot(int x){ return c[fa[x]][0]!=x&&c[fa[x]][1]!=x; }void down(int x){ int l=c[x][0],r=c[x][1]; if (rev[x]){ rev[x]^=1; rev[l]^=1,rev[r]^=1; swap(c[x][0],c[x][1]); }}void rotate(int x){ int y=fa[x],z=fa[y],l,r; if (c[y][0]==x) l=0; else l=1; r=l^1; if (!isroot(y)) if (c[z][0]==y) c[z][0]=x; else c[z][1]=x; fa[x]=z;fa[y]=x;fa[c[x][r]]=y; c[y][l]=c[x][r]; c[x][r]=y;}void splay(int x){ int top=0; q[++top]=x; for (int i=x;!isroot(i);i=fa[i]) q[++top]=fa[i]; for (int i=top;i;i--) down(q[i]); while (!isroot(x)){ int y=fa[x],z=fa[y]; if (!isroot(y)){ if (c[y][0]==x^c[z][0]==y) rotate(x); else rotate(y); } rotate(x); }}void access(int x){ int t=0; while (x){ splay(x); c[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); c[y][0]=fa[x]=0;}int find(int x){ access(x);splay(x); int y=x; while (c[y][0]) y=c[y][0]; return y;}int main(){ n=read(),m=read(); rep(i,1,m){ scanf("%s",ch); int x=read(),y=read(); if (ch[0]=='C') Link(x,y); else if (ch[0]=='D') Cut(x,y); else if (find(x)==find(y)) puts("Yes"); else puts("No"); } return 0;}
阅读全文
1 0
- [bzoj-2049][Sdoi2008]Cave 洞穴勘测 题解
- 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 洞穴勘测
- POJ3363
- Ubuntu常用操作汇总与问题解决汇总
- 乙胺卮园劳挖诼倩部难乩乓写劳乩
- 基于Tensorflow+Python实现降噪自动编码器(DAE)
- jQuery学习-对象与选择器(锋利的jQuery)
- [bzoj-2049][Sdoi2008]Cave 洞穴勘测 题解
- 点击按钮显示多个框
- 如何向Vive Port中上传htc vive应用
- 2017沈阳网络赛 1008 HDU 6201 transaction transaction transaction(树形dp)
- TTTAttributedLabel 高度计算
- Hello
- hdu 6201 深度优先搜索
- MVP+Retrofit+RxJava简单事例
- Linux串口编程