ZOJ 2334 Monkey King

并查集+左偏树。。。。。合并的时候用左偏树,合并结束后吧父结点全部定成树的根节点,保证任意两个猴子都可以通过Find找到最厉害的猴子

                      Monkey King
Time Limit: 10000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]  

Description

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 their 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

 

Source

ZOJ 3rd Anniversary Contest



 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 
 5 using namespace std;
 6 
 7 const int maxn=100100;
 8 
 9 int father[maxn];
10 
11 struct leafistreenode
12 {
13     int d,v,l,r;
14 }heap[maxn];
15 
16 int merge(int x,int y)
17 {
18     if(!x) return y; if(!y) return x;
19     if(heap[x].v<heap[y].v) swap(x,y); ///big root heap
20     heap[x].r=merge(heap[x].r,y);
21     int l=heap[x].l,r=heap[x].r;
22     if(heap[l].d<heap[r].d) swap(heap[x].l,heap[x].r);
23     if(!heap[x].r) heap[x].d=0;
24     else heap[x].d=heap[r].d+1;
25     return x;
26 }
27 
28 int N,M;
29 
30 int Find(int x)
31 {
32     if(father[x]!=x) return father[x]=Find(father[x]);
33 }
34 
35 int solve(int a,int b)
36 {
37     int x=Find(a),y=Find(b);
38     if(x==y) return -1;
39 
40     int p,xx,yy,temp;
41 
42     heap[x].v/=2;
43     temp=merge(heap[x].l,heap[x].r);
44     heap[x].d=heap[x].l=heap[x].r=0;
45     xx=merge(temp,x);
46 
47     heap[y].v/=2;
48     temp=merge(heap[y].l,heap[y].r);
49     heap[y].d=heap[y].l=heap[y].r=0;
50     yy=merge(temp,y);
51 
52     p=merge(xx,yy);
53 
54     father[xx]=father[yy]=p=father[x]=father[y]=father[a]=father[b]=p;
55 
56     return heap[p].v;
57 }
58 
59 int main()
60 {
61     while(scanf("%d",&N)!=EOF)
62     {
63         memset(heap,0,sizeof(heap));
64         for(int i=0;i<=N+10;i++) father[i]=i;
65         for(int i=1;i<=N;i++)
66         {
67             scanf("%d",&heap[i].v);
68         }
69         scanf("%d",&M);
70         while(M--)
71         {
72             int a,b;
73             scanf("%d%d",&a,&b);
74             printf("%d\n",solve(a,b));
75         }
76     }
77     return 0;
78 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值