标签:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<ctime> 6 #include<cstdlib> 7 #include<algorithm> 8 using namespace std; 9 int n,m,a[1000100],f[1000100],die[1000100],r[1000100],l[1000100],dist[1000100]; 10 inline int read() 11 { 12 int x=0,f=1; char ch=getchar(); 13 while(!isdigit(ch)) {if(ch==‘-‘) f=-1; ch=getchar();} 14 while(isdigit(ch)) {x=x*10+ch-‘0‘; ch=getchar();} 15 return x*f; 16 } 17 int find(int x){return f[x]==x?x:f[x]=find(f[x]);} 18 int merge(int x,int y) 19 { 20 if(!x) return y; 21 if(!y) return x; 22 if(a[x]>a[y]) swap(x,y); 23 r[x]=merge(r[x],y); 24 if(dist[r[x]]>dist[l[x]]) swap(r[x],l[x]); 25 dist[x]=dist[r[x]]+1; 26 return x; 27 } 28 int main() 29 { 30 freopen("cin.in","r",stdin); 31 freopen("cout.out","w",stdout); 32 n=read(); 33 for(int i=1;i<=n;i++) {a[i]=read(); f[i]=i;} 34 m=read(); dist[0]=-1; 35 for(int i=1;i<=m;i++) 36 { 37 char ch=getchar(); 38 if(ch==‘M‘) 39 { 40 int x=read(),y=read(); 41 if(die[x]||die[y]) continue; 42 x=find(x); y=find(y); 43 if(x!=y) 44 { 45 int t=merge(x,y); 46 f[x]=f[y]=t; 47 } 48 } 49 else 50 { 51 int x=read(); 52 if(die[x]) printf("0\n"); 53 else 54 { 55 x=find(x); die[x]=1; 56 printf("%d\n",a[x]); 57 f[x]=merge(l[x],r[x]); 58 f[f[x]]=f[x]; 59 } 60 } 61 } 62 return 0; 63 }
标签:
原文地址:http://www.cnblogs.com/chty/p/5826960.html