http://acm.hdu.edu.cn/showproblem.php?pid=2475
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define MAXN 50010 4 struct node 5 { 6 int left,right; 7 }tree[MAXN]; 8 int father[MAXN],rev[MAXN],Stack[MAXN],cc[MAXN]; 9 int read() 10 { 11 int s=0,fh=1;char ch=getchar(); 12 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)fh=-1;ch=getchar();} 13 while(ch>=‘0‘&&ch<=‘9‘){s=s*10+(ch-‘0‘);ch=getchar();} 14 return s*fh; 15 } 16 int isroot(int x) 17 { 18 return tree[father[x]].left!=x&&tree[father[x]].right!=x; 19 } 20 void pushdown(int x) 21 { 22 int l=tree[x].left,r=tree[x].right; 23 if(rev[x]!=0) 24 { 25 rev[x]^=1;rev[l]^=1;rev[r]^=1; 26 swap(tree[x].left,tree[x].right); 27 } 28 } 29 void rotate(int x) 30 { 31 int y=father[x],z=father[y]; 32 if(!isroot(y)) 33 { 34 if(tree[z].left==y)tree[z].left=x; 35 else tree[z].right=x; 36 } 37 if(tree[y].left==x) 38 { 39 father[x]=z;father[y]=x;tree[y].left=tree[x].right;tree[x].right=y;father[tree[y].left]=y; 40 } 41 else 42 { 43 father[x]=z;father[y]=x;tree[y].right=tree[x].left;tree[x].left=y;father[tree[y].right]=y; 44 } 45 } 46 void splay(int x) 47 { 48 int top=0,y,z,i;Stack[++top]=x; 49 for(i=x;!isroot(i);i=father[i])Stack[++top]=father[i]; 50 for(i=top;i>=1;i--)pushdown(Stack[i]); 51 while(!isroot(x)) 52 { 53 y=father[x];z=father[y]; 54 if(!isroot(y)) 55 { 56 if((tree[y].left==x)^(tree[z].left==y))rotate(x); 57 else rotate(y); 58 } 59 rotate(x); 60 } 61 } 62 void access(int x) 63 { 64 int last=0; 65 while(x!=0) 66 { 67 splay(x); 68 tree[x].right=last; 69 last=x;x=father[x]; 70 } 71 } 72 void makeroot(int x) 73 { 74 access(x);splay(x);rev[x]^=1; 75 } 76 void link(int u,int v) 77 { 78 makeroot(u);/*access(u);*/father[u]=v;//splay(u); 79 } 80 void cut(int u,int v) 81 { 82 /*makeroot(u);*/access(u);access(v);splay(v);father[tree[v].left]=0;tree[v].left=0; 83 } 84 int findroot(int x) 85 { 86 access(x);splay(x); 87 while(tree[x].left!=0)x=tree[x].left; 88 return x; 89 } 90 int Findroot(int u,int v) 91 { 92 int pd=0; 93 access(u);splay(u); 94 while(tree[u].left!=0){if(tree[u].left==v){pd=1;break;}u=tree[u].left;} 95 return pd; 96 } 97 int main() 98 { 99 int n,i,m,k,x,y,a,tt=0; 100 char fh[30]; 101 while(scanf("%d",&n)!=EOF) 102 { 103 if(tt)puts(""); 104 tt++; 105 for(i=1;i<=n;i++) 106 { 107 tree[i].left=tree[i].right=father[i]=rev[i]=cc[i]=0; 108 } 109 for(i=1;i<=n;i++) 110 { 111 a=read(); 112 if(a!=0) 113 { 114 link(i,a); 115 cc[i]=a; 116 } 117 } 118 m=read(); 119 for(i=1;i<=m;i++) 120 { 121 scanf("\n%s",fh); 122 if(fh[0]==‘Q‘) 123 { 124 k=read(); 125 printf("%d\n",findroot(k)); 126 } 127 else 128 { 129 x=read();y=read(); 130 if(x!=0&&y!=0) 131 { 132 if(Findroot(y,x)==1)continue; 133 } 134 if(cc[x]!=0) 135 { 136 cut(x,cc[x]); 137 //access(x);access(cc[x]);splay(cc[x]);father[tree[cc[x]].left]=0;tree[cc[x]].left=0;/*splay(cc[x]);*///father[x]=tree[cc[x]].left=0; 138 } 139 cc[x]=y; 140 if(cc[x]!=0) 141 { 142 //link(x,cc[x]); 143 access(x);father[x]=cc[x];//splay(x); 144 } 145 } 146 } 147 //printf("\n"); 148 } 149 return 0; 150 }
时间: 2024-10-12 17:36:45