hdu 1512 Monkey King (可并堆)
来源:互联网 发布:阿里云深圳机房地址 编辑:程序博客网 时间:2024/05/17 08:52
Monkey King
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5405 Accepted Submission(s): 2326
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.
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.
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
题解:可并堆
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#define N 100003using namespace std;int fa[N],a[N],lch[N],rch[N],d[N];int n,m;int find(int x){if (fa[x]==x) return x;fa[x]=find(fa[x]);return fa[x];}int merge(int x,int y){if (!x) return y;if (!y) return x;if (a[x]<a[y]) swap(x,y);rch[x]=merge(rch[x],y);if (d[lch[x]]<d[rch[x]]) swap(lch[x],rch[x]);d[x]=d[rch[x]]+1;return x;}int solve(int x){if (!lch[x]&&!rch[x]) {a[x]=a[x]/2;return x;}int t=merge(lch[x],rch[x]);fa[t]=t;fa[x]=x; lch[x]=0; rch[x]=0; a[x]=a[x]/2;int t1=merge(t,x);fa[t]=fa[x]=t1;return t1;}int main(){freopen("a.in","r",stdin);freopen("my.out","w",stdout);while (scanf("%d",&n)!=EOF) {memset(a,0,sizeof(a));memset(lch,0,sizeof(lch));memset(rch,0,sizeof(rch));memset(d,0,sizeof(d));for (int i=1;i<=n;i++) scanf("%d",&a[i]),fa[i]=i; scanf("%d",&m); for (int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); int r1=find(x); int r2=find(y); if (r1==r2) { printf("-1\n"); continue;}r1=solve(r1); r2=solve(r2);int t=merge(r1,r2);fa[r1]=fa[r2]=t;printf("%d\n",a[t]);}}}
0 0
- HDU 1512 Monkey King [可并堆]
- hdu 1512 Monkey King (左偏树 可并堆)
- hdu 1512 Monkey King (可并堆)
- hdu 1512 Monkey King 左偏树(可并堆)
- |Hdu 1512|可并堆|并查集|Monkey King
- HDU 1512 Monkey King 可并堆,左偏树
- [HDU1512]Monkey King(可并堆)
- hdu1512 Monkey King(可并堆插入+删除)
- hdu 1512 Monkey King (左偏树可并堆 并查集)
- zoj2334 Monkey King , 并查集,可并堆,左偏树
- HDU 1512 Monkey King(左偏树+并查集)
- HDU 1512 Monkey King(左偏树+并查集)
- Hdu 1512 Monkey King(左偏树+并查集)
- hdu 1512 Monkey King
- 【HDU 1512】Monkey King
- hdu 1512 Monkey King
- HDU 1512 Monkey King
- HDU 1512 Monkey King
- PHP中使用eAccelerator
- Linux常用命令发布啦!
- android 状态保存onSaveInstanceState
- 海康威视采集卡结合opencv使用(两种方法)-转
- leetcode 59. Spiral Matrix II
- hdu 1512 Monkey King (可并堆)
- Android 系列 3.2在大量不同设备上进行云测试
- memcached安装以及tomcat配置名称和session一致性配置
- Xcode8.0 去除控制台多余打印
- jQuery学习笔记(一) 基本概念及元素选择
- 工作压力太大,离职就能解决了?
- 感谢老婆早上给我挤的牙膏
- Nginx 性能调优
- mybatis学习之全局配置文件(五)