hdu 1512 Monkey King(坑)

来源:互联网 发布:魔法王座升级数据 编辑:程序博客网 时间:2024/04/30 14:46

Monkey King

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6036    Accepted Submission(s): 2586


Problem 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 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
520161010452 33 43 54 51 5
 

Sample Output
855-110
 

Author
JIANG, Yanyan
 

Source
ZOJ 3rd Anniversary Contest
 

Recommend
linle   |   We have carefully selected several similar problems for you:  1509 1710 1504 1510 1500 



【分析】
MLE了。
代码借鉴licone...
根本不知道怎么回事(挠头)
(为啥左偏树就A掉了QAQ)
又挖了个坑。
滚粗辣



【代码】
//hdu 1512 Monkey King#include<iostream>#include<cstring>#include<cstdio>#define ll long long#define M(a) memset(a,0,sizeof a)#define fo(i,j,k) for(i=j;i<=k;i++)using namespace std;const int mxn=100001;int father[mxn];int n,m,top;struct HEAP{int l,r,key;}h[mxn];inline int find(int x){if(x==father[x]) return father[x];return father[x]=find(father[x]);}inline int merge(int p,int q){if(!p) return q;if(!q) return p;if(h[p].key<h[q].key) swap(p,q);h[p].r=merge(h[p].r,q);swap(h[p].l,h[p].r);return p;}int main(){int i,j,k,x,y;while(scanf("%d",&n)!=EOF){fo(i,1,n) father[i]=i;fo(i,1,n){scanf("%d",&h[i].key);h[i].l=h[i].r=0;}scanf("%d",&m);while(m--){scanf("%d%d",&x,&y);int p=find(x),q=find(y);if(p==q){printf("-1\n");continue;}h[p].key/=2,h[q].key/=2;k=merge(h[p].l,h[p].r);h[p].l=h[p].r=0;p=merge(p,k);k=merge(h[q].l,h[q].r);h[q].l=h[q].r=0;q=merge(q,k);father[p]=father[q]=merge(p,q);printf("%d\n",h[father[p]].key);}}return 0;}


原创粉丝点击