# HDU 1512 Monkey King

## HDU 1512: Monkey King

Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its own way and none of them knows each other. But monkeys can't avoid quarrelling, and it only happens between two monkeys who does not know each other. And when it happens, both the two monkeys will invite the strongest friend of them, and duel. Of course, after the duel, the two monkeys and all of there friends knows each other, and the quarrel above will no longer happens between these monkeys even if they have ever conflicted.
Assume that every money has a strongness value, which will be reduced to only half of the original after a duel(that is, 10 will be reduced to 5 and 5 will be reduced to 2).
And we also assume that every monkey knows himself. That is, when he is the strongest one in all of his friends, he himself will go to duel.
Input
There are several test cases, and each case consists of two parts.
First part: The first line contains an integer N(N<=100,000), which indicates the number of monkeys. And then N lines follows. There is one number on each line, indicating the strongness value of ith monkey(<=32768).
Second part: The first line contains an integer M(M<=100,000), which indicates there are M conflicts happened. And then M lines follows, each line of which contains two integers x and y, indicating that there is a conflict between the Xth monkey and Yth.
Output
For each of the conflict, output -1 if the two monkeys know each other, otherwise output the strongness value of the strongest monkey in all friends of them after the duel.
Sample Input
5
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5
Sample Output
8
5
5
-1
10

　　这道题就是一个可并堆+并查集裸题。可并堆很好写，但如何结合并查集，着实让我苦思了很久。实在没有办法，最开始只有用数组下标，但这样很明显是不符合套路的。　　后来发现，结点可以加id。这样就可以就合并查集了。　　但最后，发现可并堆+并查集可以合为一体。　　代码如下（注释为+id版本）：
 1 #include <cstdio>
2 #include <algorithm>
3 template<class T>inline void readin(T &res) {
4     static char ch;T f=1;
5     while((ch=getchar())<'0'||ch>'9')if(ch=='-')f=-1;
6     res=ch-48;while((ch=getchar())>='0'&&ch<='9')res=(res<<1)+(res<<3)+ch-48;res*=f;
7 }
8 const int N = 100005;
9 struct Node {
10     int key, dist;//, id;
11     Node *ls, *rs;
12     Node *fa;
13 }pool[N], *null=pool, *tnode[N];
14 inline Node *find(Node *x) {
15     if(x->fa==x) return x;
16     return x->fa=find(x->fa);
17 }
18 /*int fa[N];
19 inline int find(int x) {
20     if(fa[x]==x) return x;
21     return fa[x]=find(fa[x]);
22 }*/
23 Node *merge(Node *a,Node *b) {
24     if(a==null) return b;
25     if(b==null) return a;
26     if(b->key>a->key) std::swap(a,b);
27     a->rs=merge(a->rs,b);
28     a->rs->fa=a;
29     //fa[a->rs->id]=a->id;
30     if(a->rs->dist>a->ls->dist) std::swap(a->ls,a->rs);
31     if(a->rs==null) a->dist=0;
32     else a->dist=a->rs->dist+1;
33     return a;
34 }
35 Node *pop(Node *nd) {
36     Node *l=nd->ls, *r=nd->rs;
37     l->fa=l;r->fa=r;
38     //fa[l->id]=l->id;
39     //fa[r->id]=r->id;
40     nd->ls=nd->rs=null;nd->dist=0;
41     return merge(l,r);
42 }
43 int main() {
44     int n, q, x, y;
45     null->ls=null->rs=null;
46     while(~scanf("%d",&n)) {
47         for( int i = 1; i <= n; i++ ) {
48             tnode[i]=&pool[i];
50             tnode[i]->ls=tnode[i]->rs=null;tnode[i]->dist=0;
51             tnode[i]->fa=tnode[i];
52             //fa[i]=i;
53         }
55         while(q--) {
57             if(find(tnode[x])==find(tnode[y])) printf("-1\n");
58             //if(find(x)==find(y)) printf("-1\n");
59             else {
60                 Node *ra = tnode[x]->fa, *rb = tnode[y]->fa;
61                 //Node *ra = tnode[fa[x]], *rb = tnode[fa[y]];
62                 Node *ta = pop(ra);
63                 ra->key /= 2;
64                 ra = merge(ta,ra);
65                 Node *tb = pop(rb);
66                 rb->key /= 2;
67                 rb = merge(tb,rb);
68                 printf("%d\n",merge(ra,rb)->key);
69             }
70         }
71     }
72     return 0;
73 }

posted @ 2017-07-17 12:21  Doggu  阅读(...)  评论(...编辑  收藏