ZOJ 2334 Monkey King
并查集+左偏树。。。。。合并的时候用左偏树,合并结束后吧父结点全部定成树的根节点,保证任意两个猴子都可以通过Find找到最厉害的猴子
Time Limit: 10000MS | Memory Limit: 32768KB | 64bit IO Format: %lld & %llu |
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
#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的更多相关文章
- zoj 2334 Monkey King/左偏树+并查集
原题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1389 大致题意:N只相互不认识的猴子(每只猴子有一个战斗力值) 两只 ...
- ZOJ 2334(Monkey King-左偏树第一题)
Monkey King Time Limit: 10 Seconds Memory Limit: 32768 KB Once in a forest, there lived N aggre ...
- 数据结构(左偏树):HDU 1512 Monkey King
Monkey King Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- ZOJ 1093 Monkey and Banana (LIS)解题报告
ZOJ 1093 Monkey and Banana (LIS)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- P1456 Monkey King
题目地址:P1456 Monkey King 一道挺模板的左偏树题 不会左偏树?看论文打模板,完了之后再回来吧 然后你发现看完论文打完模板之后就可以A掉这道题不用回来了 细节见代码 #include ...
- 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 ...
- HDU - 5201 :The Monkey King (组合数 & 容斥)
As everyone known, The Monkey King is Son Goku. He and his offspring live in Mountain of Flowers and ...
- Monkey King(左偏树 可并堆)
我们知道如果要我们给一个序列排序,按照某种大小顺序关系,我们很容易想到优先队列,的确很方便,但是优先队列也有解决不了的问题,当题目要求你把两个优先队列合并的时候,这就实现不了了 优先队列只有插入 删除 ...
- 1512 Monkey King
Monkey King Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
随机推荐
- Consuming a RESTful Web Service
本篇文章将介绍使用Spring来建立RESTful的Web Service. 我们通过一个例子来说明这篇文章:这个例子将会使用Spring的RestTemplate来从Facebook的提供的API中 ...
- POJ 2431 Expedition(优先队列、贪心)
题目链接: 传送门 Expedition Time Limit: 1000MS Memory Limit: 65536K 题目描述 驾驶一辆卡车行驶L单位距离.最开始有P单位的汽油.卡车每开1 ...
- 导入.pch文件
Xcode5中创建一个工程的时候,系统会自动创建一个以以工程名为名字的pch(Precompile Prefix Header)文件,开发的过程中可以将广泛使用的头文件以及宏包含在该文件下,编译器就会 ...
- 解决centos7中python-pip模块不存在的问题
centos 7中python-pip模块不存在,是因为像centos这类衍生的发行版,源跟新滞后,或者不存在.即使使用yum去search python-pip也找不到软件包. 为了使用安装滞后或源 ...
- Docker change directory
https://forums.docker.com/t/how-do-i-change-the-docker-image-installation-directory/1169/2 How do I ...
- asp.net js 倒计时总秒数量 和 排序
Edit in JSFiddle JavaScript HTML CSS Result h1 { font-family: "微软雅黑"; font-size: 40px; mar ...
- LDA(主题模型算法)
LDA整体流程 先定义一些字母的含义: 文档集合D,topic集合T D中每个文档d看作一个单词序列< w1,w2,...,wn >,wi表示第i个单词,设d有n个单词.(LDA里面称之为 ...
- JSP 原理
参考文献:http://www.cnblogs.com/xdp-gacl/p/3764991.html 一.什么是JSP? JSP全称是Java Server Pages,它和servle技术一样,都 ...
- centos7.2安装paramiko报error: command 'gcc' failed with exit status 1的解决办法
安装依赖 yum install kernel-devel libxslt-devel libffi-devel python-devel mysql-devel zlib-devel openssl ...
- Quartz.NET总结(三)Quartz 配置
前两篇文章,已经介绍了Quartz.NET的使用和Cron表达式表达式的写法,今天说一说Quartz的配置,Quartz相关的配置有三个quartz.config.quartz_jobs.xml.lo ...