# 【数据结构】bzoj1455罗马游戏

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 <bits/stdc++.h>
2 #define Maxn 1000007
3 using namespace std;
5 {
6     int x=0,f=1;char ch=getchar();
7     while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
8     while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
9     return x*f;
10 }
11 int n,m;
12 int ls[Maxn],rs[Maxn];
13 int v[Maxn];
14 int fa[Maxn],d[Maxn];
15 bool vis[Maxn];
16 char ch[10];
17 int find(int x)
18 {
19     if (x==fa[x]) return fa[x];
20     fa[x]=find(fa[x]);
21     return fa[x];
22 }
23 int merge(int x,int y)
24 {
25     if (x==0) return y;
26     if (y==0) return x;
27     if (v[x]>v[y]) swap(x,y);
28     rs[x]=merge(rs[x],y);
29     if (d[rs[x]]>d[ls[x]]) swap(ls[x],rs[x]);
30     d[x]=d[rs[x]]+1;
31     return x;
32 }
33 int main()
34 {
36     for (int i=1;i<=n;i++)
38     for (int i=1;i<=n;i++)
39     {
40         fa[i]=i;
41         d[i]=1;
42     }
43     memset(vis,true,sizeof(vis));
45     while (m--)
46     {
47         scanf("%s",ch);
48         if (ch[0]=='M')
49         {
51             if ((!vis[x])||(!vis[y])) continue;
52             int p=find(x),q=find(y);
53             if (p!=q) fa[p]=fa[q]=merge(p,q);
54         } else
55         {
57             if (!vis[x])
58             {
59                 printf("%d\n",0);
60                 continue;
61             }
62             int p=find(x);
63             vis[p]=false;
64             printf("%d\n",v[p]);
65             fa[p]=merge(ls[p],rs[p]);
66             fa[fa[p]]=fa[p];
67         }
68     }
69     return 0;
70 }

posted @ 2017-06-13 14:20  Tommyr7  阅读(212)  评论(0编辑  收藏  举报