标签:
F.A.Qs | Home | Discuss | ProblemSet | Status | Ranklist | Contest | ModifyUser gryz2016 | Logout | 捐赠本站 |
---|
1 #include<cstdio> 2 #include<algorithm> 3 #define N 1000005 4 5 using namespace std; 6 7 struct zpd 8 { 9 int n,m,fa[N],ls[N],rs[N],d[N],v[N],cnt; 10 bool vis[N]; 11 int find(int x){if(fa[x]==x) return x; else fa[x]=find(fa[x]);return fa[x];} 12 int merge(int x,int y) 13 { 14 if(!x)return y; 15 if(!y)return x; 16 if(v[x]>v[y])swap(x,y); 17 rs[x]=merge(rs[x],y); 18 if(d[rs[x]]>d[ls[x]])swap(ls[x],rs[x]); 19 d[x]=d[rs[x]]+1; 20 return x; 21 } 22 void insert() 23 { 24 scanf("%d",&n); 25 for (int i=1;i<=n;i++) 26 scanf("%d",&v[i]); 27 for (int i=1;i<=n;i++) fa[i]=i; 28 d[0]=-1; 29 scanf("%d",&m); 30 char ch[10]; 31 for (int i=1;i<=m;i++) 32 { 33 scanf("%s",ch);int x,y; 34 if(ch[0]==‘M‘) 35 { 36 scanf("%d%d",&x,&y); 37 if(vis[x]||vis[y])continue; 38 int p=find(x),q=find(y); 39 if(p!=q) 40 { 41 int t=merge(p,q); 42 fa[p]=fa[q]=t; 43 } 44 } 45 else 46 { 47 scanf("%d",&x); 48 if(vis[x])printf("0\n"); 49 else 50 { 51 int p=find(x);vis[p]=1; 52 printf("%d\n",v[p]); 53 fa[p]=merge(ls[p],rs[p]); 54 fa[fa[p]]=fa[p]; 55 } 56 } 57 } 58 } 59 }zpd; 60 int main() 61 { 62 zpd.insert(); 63 return 0; 64 }
标签:
原文地址:http://www.cnblogs.com/grhyxzc/p/5151763.html