并查集+左偏树。。。。。合并的时候用左偏树,合并结束后吧父结点全部定成树的根节点,保证任意两个猴子都可以通过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. POJ2456 Aggressive cows

    Aggressive cows 二分,关键是转化为二分! #include <cstdio> #include <algorithm> ; ; int N, C; int a[ ...

  2. 解决Cookie乱码问题

    写了一个cookie的定义和获取,结果我输入中文后,页面报错 报错信息如下: type Exception report message An exception occurred processin ...

  3. [TYVJ]1519 博彩

    传送门 AC自动机模板题,好吧我只是单纯的搞个AC自动机的模板. //TYVJ 1519 //by Cydiater //2016.10.18 #include <iostream> #i ...

  4. JS-纯js制作动态成绩表(流程控制语句+js内置对象)

    流程控制for循环+if判断+Math对象+Array对象+Date对象制作成绩表 <!DOCTYPE html><html> <head> <meta ch ...

  5. JAVA jdbc获取数据库连接

    JDBC获取数据库连接的帮助类 import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManage ...

  6. JavaWeb学习笔记——DAO设计模式

  7. JavaWeb学习笔记——XML和SAX解析区别

  8. texlive2015+texstudio

    编译器 texlive2015 编辑器 texstudio

  9. Photoshop 融合属性 Unity Shader

    http://forum.unity3d.com/threads/free-photoshop-blends.121661/

  10. 防SQL注入代码(ASP版)

    <% Dim Fy_Url,Fy_a,Fy_x,Fy_Cs(),Fy_Cl,Fy_Ts,Fy_Zx '---定义部份 头------ Fy_Cl = 1 '处理方式:1=提示信息,2=转向页面, ...