# [bzoj 1455]罗马游戏

## 罗马游戏

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 1978  Solved: 857
[Submit][Status][Discuss]

5
100 90 66 99 10
7
M 1 5
K 1
K 1
M 2 3
M 3 4
K 5
K 4

## Sample Output

10
100
0
66

 1 #include<cstdio>
2 #include<algorithm>
3 using namespace std;
4 const int maxn = 1000006;
5 int fa[maxn];
6 int n,data[maxn];
7 int l[maxn],r[maxn],v[maxn],d[maxn],m;
8 bool die[maxn];
9 int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
10 int merge(int x,int y){
11     if(!x)return y;
12     if(!y)return x;
13     if(v[x]>v[y])swap(x,y);
14     r[x]=merge(r[x],y);
15     if(d[l[x]]<d[r[x]])swap(l[x],r[x]);
16     d[x]=d[r[x]]+1;
17     return x;
18 }
19 int main(){
20     scanf("%d",&n);
21     for(int i=1;i<=n;i++)scanf("%d",&v[i]),fa[i]=i;
22     d[0]=-1;
23     char ch[10];
24     scanf("%d",&m);
25     for(int i=1;i<=m;i++)
26     {
27         scanf("%s",ch);int x,y;
28         if(ch[0]=='M')
29         {
30             scanf("%d%d",&x,&y);
31             if(die[x]||die[y])continue;
32             int p=find(x),q=find(y);
33             if(p!=q)
34             {
35                 int t=merge(p,q);
36                 fa[p]=fa[q]=t;
37             }
38         }
39         else
40         {
41             scanf("%d",&x);
42             if(die[x])printf("0\n");
43             else
44             {
45                 int p=find(x);die[p]=1;
46                 printf("%d\n",v[p]);
47                 fa[p]=merge(l[p],r[p]);
48                 fa[fa[p]]=fa[p];
49             }
50         }
51     }
52     return 0;
53     return 0;
54 }

posted @ 2017-05-27 21:16  986yg  阅读(116)  评论(0编辑  收藏  举报