并查集+左偏树。。。。。合并的时候用左偏树,合并结束后吧父结点全部定成树的根节点,保证任意两个猴子都可以通过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. HDU 1874 畅通工程续(最短路/spfa Dijkstra 邻接矩阵+邻接表)

    题目链接: 传送门 畅通工程续 Time Limit: 1000MS     Memory Limit: 65536K Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路. ...

  2. TortoiseSVN 过滤文件(包括已提交和未提交)

    一:svn 设置过滤文件方式 1.选中需要过滤的文件夹或者文件---右键---TortoiseSVN---Add to Ignore list(如果不显示说明该目录已经被添加) 2.在当前工作区域 不 ...

  3. Visual Studio 当前上下文中不存在名称“ConfigurationManager”

    Visual Studio调试出现错误:当前上下文中不存在名称“ConfigurationManager” 解决方法: 1.System.Configuration引用这个dll参考:http://k ...

  4. 利用mybatis的分页插件实现商品列表的显示

    分析思路: 当我们点击查询商品的时候,会出现商品的列表,并按上下页可以实现分页的查询的功能. 首先首先我们先找到商品查询商品的按钮在jsp的那个页面,即首页index.jsp 这里有个url即显示商品 ...

  5. android webview里获取和设置cookie

    private class MyWebViewClient extends WebViewClient { public boolean shouldOverrideUrlLoading(WebVie ...

  6. uC/OS-II时间(OS_time)块

    /*************************************************************************************************** ...

  7. nginx查看安装了哪些模块

    查看安装了哪些模块命令: [root@RG-PowerCache-X xcache]# nginx/sbin/nginx -Vnginx version: nginx/1.2.3built by gc ...

  8. PHP责任链设计模式

    <?php //责任链设计模式 /** * 每个对象,储存着对自己上级的引用, * 如果自己处理不了,交给上一级. */ class board{ protected $power=1; pro ...

  9. hibernate实现有两种配置,xml配置与注释配置。

    (1):xml配置:hibernate.cfg.xml (放到src目录下)和实体配置类:xxx.hbm.xml(与实体为同一目录中) <?xml version='1.0' encoding= ...

  10. CSS 中背景图片定位方法

    三种: 关键字:background-position: top left; 像素:background-position: 0px 0px; 百分比:background-position: 0% ...