并查集+左偏树。。。。。合并的时候用左偏树,合并结束后吧父结点全部定成树的根节点,保证任意两个猴子都可以通过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
 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; const int maxn=; int father[maxn]; struct leafistreenode
{
int d,v,l,r;
}heap[maxn]; int merge(int x,int y)
{
if(!x) return y; if(!y) return x;
if(heap[x].v<heap[y].v) swap(x,y); ///big root heap
heap[x].r=merge(heap[x].r,y);
int l=heap[x].l,r=heap[x].r;
if(heap[l].d<heap[r].d) swap(heap[x].l,heap[x].r);
if(!heap[x].r) heap[x].d=;
else heap[x].d=heap[r].d+;
return x;
} int N,M; int Find(int x)
{
if(father[x]!=x) return father[x]=Find(father[x]);
} int solve(int a,int b)
{
int x=Find(a),y=Find(b);
if(x==y) return -; int p,xx,yy,temp; heap[x].v/=;
temp=merge(heap[x].l,heap[x].r);
heap[x].d=heap[x].l=heap[x].r=;
xx=merge(temp,x); heap[y].v/=;
temp=merge(heap[y].l,heap[y].r);
heap[y].d=heap[y].l=heap[y].r=;
yy=merge(temp,y); p=merge(xx,yy); father[xx]=father[yy]=p=father[x]=father[y]=father[a]=father[b]=p; return heap[p].v;
} int main()
{
while(scanf("%d",&N)!=EOF)
{
memset(heap,,sizeof(heap));
for(int i=;i<=N+;i++) father[i]=i;
for(int i=;i<=N;i++)
{
scanf("%d",&heap[i].v);
}
scanf("%d",&M);
while(M--)
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",solve(a,b));
}
}
return ;
}

ZOJ 2334 Monkey King的更多相关文章

  1. zoj 2334 Monkey King/左偏树+并查集

    原题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1389 大致题意:N只相互不认识的猴子(每只猴子有一个战斗力值) 两只 ...

  2. ZOJ 2334(Monkey King-左偏树第一题)

    Monkey King Time Limit: 10 Seconds      Memory Limit: 32768 KB Once in a forest, there lived N aggre ...

  3. 数据结构(左偏树):HDU 1512 Monkey King

    Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  4. ZOJ 1093 Monkey and Banana (LIS)解题报告

    ZOJ  1093   Monkey and Banana  (LIS)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  5. P1456 Monkey King

    题目地址:P1456 Monkey King 一道挺模板的左偏树题 不会左偏树?看论文打模板,完了之后再回来吧 然后你发现看完论文打完模板之后就可以A掉这道题不用回来了 细节见代码 #include ...

  6. HDU 1069 Monkey and Banana / ZOJ 1093 Monkey and Banana (最长路径)

    HDU 1069 Monkey and Banana / ZOJ 1093 Monkey and Banana (最长路径) Description A group of researchers ar ...

  7. HDU - 5201 :The Monkey King (组合数 & 容斥)

    As everyone known, The Monkey King is Son Goku. He and his offspring live in Mountain of Flowers and ...

  8. Monkey King(左偏树 可并堆)

    我们知道如果要我们给一个序列排序,按照某种大小顺序关系,我们很容易想到优先队列,的确很方便,但是优先队列也有解决不了的问题,当题目要求你把两个优先队列合并的时候,这就实现不了了 优先队列只有插入 删除 ...

  9. 1512 Monkey King

    Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

随机推荐

  1. 加州大学伯克利分校Stat2.2x Probability 概率初步学习笔记: Section 2 Random sampling with and without replacement

    Stat2.2x Probability(概率)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...

  2. 加州大学伯克利分校Stat2.3x Inference 统计推断学习笔记: Section 4 Dependent Samples

    Stat2.3x Inference(统计推断)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...

  3. jQuery焦点不在输入框内判断不能为空

    我能说JS和jquery有时候都有病吗?同样的代码,重敲一遍可以了,再过一会不行了.再试一下重敲,一模一样的代码,也不报错.就是不行.反复折腾.... 我帖上来的是经过了1个小时同等功能的测试OK的, ...

  4. jsf primefaces note

    对应cdi,inject的变量,必须使用getter才能访问的值. @Inject DateView dateView; dateView.getFromDate1() 这样是娶不到的: dateVi ...

  5. CSS--值和单位

    等价颜色表 什么叫Web安全色 所谓的“web安全”颜色是指在256色计算机系统上总能避免抖动的颜色. Web安全颜色可以表示为RGB值为20%和51(相应的十六进制值为33)的倍数. 如果使用RGB ...

  6. Objective-C学习笔记之block

    //定义一个函数,传入block类型参数myBlock5 int fun(int (^myBlock5)(int a,int b)) { return myBlock5(10,20); } int ( ...

  7. 【原】React操作表单

    最近的项目中开发中都是用react,其中有用到react去操纵表单.然后自己就在每个表单元素中添加 ref,  然后再像jquery操作dom一样去操纵这个ref, 代码如下: 首先我在每个表单元素那 ...

  8. wpf arcgis engine 当前没有或未启用Spatial Analyst许可解决办法

    用wpf 在做叠加分析时 遇到了一个错误:“ERROR 010096:当前没有或未启用Spatial Analyst许可”:在环境中把这个Spatial Analyst扩展功能给勾了也不能解决,现在把 ...

  9. Spring MVC学习笔记——Controller接口

  10. yourphp读取不到hits

    源代码 <YP:list name="Article" order="id desc" catid="37" limit=" ...