hdu 2475 box LCT
来源:互联网 发布:正态分布的随机矩阵 编辑:程序博客网 时间:2024/04/27 22:28
题意是箱子套箱子,每次移动箱子x到箱子y里,或询问x最外层的箱子。
这一道题根是固定的,不能换根。
#include<iostream>#include<cstdio>#include<cstring>#define maxn 50005using namespace std;int fa[maxn],ch[maxn][2];int f[maxn];bool isroot(int x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}bool dir(int x){return ch[fa[x]][1]==x;}void rotate(int x){ int y=fa[x],z=fa[y]; bool b=dir(x); int a=ch[x][!b]; if(!isroot(y)) ch[z][dir(y)]=x; fa[x]=z;ch[x][!b]=y; fa[y]=x;ch[y][b]=a; if(a) fa[a]=y;}void splay(int x){ while(!isroot(x)) { int y=fa[x]; if(isroot(y)) rotate(x); else { bool b=dir(x),c=dir(y); if(b^c) {rotate(y);rotate(x);} else {rotate(x);rotate(x);} } }}void access(int x){ for(int t=0;x;t=x,x=fa[x]) splay(x),ch[x][1]=t;}int find_root(int x){ access(x);splay(x); while(ch[x][0]) x=ch[x][0]; return x;}void cut(int x){ access(f[x]); splay(x);fa[x]=0;}void link(int x,int y){ cut(x); if(!y)return ; if(find_root(y)==x) { splay(x); fa[x]=f[x]; return ; } fa[x]=y; f[x]=y;}char s[10];void init(){ memset(f,0,sizeof(f)); memset(fa,0,sizeof(fa)); memset(ch,0,sizeof(ch));}int main(){ int n; bool fst=1; while(scanf("%d",&n)!=EOF) { if(fst) fst=0; else puts(""); init(); for(int i=1;i<=n;i++) { scanf("%d",&fa[i]); f[i]=fa[i]; } int m,x,y; scanf("%d",&m); for(int i=1;i<=m;i++) { scanf("%s%d",s,&x); if(s[0]=='Q') printf("%d\n",find_root(x)); else { scanf("%d",&y); link(x,y); } } } return 0;}
1 0
- hdu 2475 box LCT
- hdu 2475 Box splay, lct
- hdu-2475-Box-splay
- HDU 2475Box
- hdu 2475 Box (splay tree)
- hdu 2475 Box(伸展树)
- hdu 5002 Tree (LCT)
- hdu 3966 LCT
- hdu 5052 (LCT)
- HDU 4010 LCT
- HDU 5052 LCT
- hdu 4010 LCT
- hdu 4010 LCT
- hdu 5002 Tree (LCT)
- HDU 2475 Box 树型转线型 + 伸展树
- HDU 2475 Box [dfs序+splay]
- hdu 5002 (LCT模板)
- hdu 4010 (LCT模板)
- Android a手机扫描b手机二维码,a手机跳转到本App评分
- liunx pear安装
- intellj idea 如何设置类头注释和方法注释
- Fn+F3开关触摸板
- 回溯算法:找数
- hdu 2475 box LCT
- 三种求解最短路径的情况
- ASP指定来路如何指定。有代码参考!只需指定!
- 中科大镜像站
- WINGIDE5 支持中文设置
- BZOJ1097: [POI2007]旅游景点atr
- 此电脑IP
- public.xml 用法的请教
- LeetCode448. Find All Numbers Disappeared in an Array新年第一篇