题解:

和猴子那一题差不多

每一次删掉一个点,合并

代码:

#include<bits/stdc++.h>
const int N=1000005;
using namespace std;
int n,m,fa[N],x,y,l[N],r[N],d[N],v[N],over[N];
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
int merge(int x,int y)
{
    if (!x||!y)return x+y;
    if (v[x]>v[y])swap(x,y);
    r[x]=merge(r[x],y);
    if (d[r[x]]>d[l[x]])swap(l[x],r[x]);
    d[x]=d[r[x]]+1;
    return x;
}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)scanf("%d",&v[i]);
    for (int i=1;i<=n;i++)fa[i]=i;
    d[0]=-1;
    char ch[10];
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
     {
        scanf("%s",ch);
        if(ch[0]=='M')
         {
            scanf("%d%d",&x,&y);
            if (over[x]||over[y])continue;
            int p=find(x),q=find(y);
            if(p!=q)
             { 
                int t=merge(p,q);
                fa[p]=fa[q]=t;
              }
         }
        else 
         {
            scanf("%d",&x);
            if(over[x])puts("0");
            else 
             {
                int p=find(x);over[p]=1;
                printf("%d\n",v[p]);
                fa[p]=merge(l[p],r[p]);
                fa[fa[p]]=fa[p];
             }
         }
     }
    return 0;
}

 

posted on 2017-12-18 19:45  宣毅鸣  阅读(97)  评论(0编辑  收藏  举报