数据结构(左偏树):HDU 1512 Monkey King
Monkey King
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4714 Accepted Submission(s): 2032
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.
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.
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.
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5
5
5
-1
10
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
int ch[maxn][],dis[maxn],key[maxn];
int fa[maxn],rt[maxn],n,m;
void Init(){
dis[]=-;
for(int i=;i<=n;i++){
fa[i]=i;rt[i]=i;dis[i]=;
ch[i][]=ch[i][]=;
}
} int Merge(int x,int y){
if(!x||!y)return x|y;
if(key[x]<key[y])swap(x,y);
ch[x][]=Merge(ch[x][],y);
if(dis[ch[x][]]>dis[ch[x][]])
swap(ch[x][],ch[x][]);
dis[x]=dis[ch[x][]]+;
return x;
} void Delete(int x){
int tmp=rt[x];
rt[x]=Merge(ch[rt[x]][],ch[rt[x]][]);
ch[tmp][]=ch[tmp][]=;
} int Find(int x){
return x==fa[x]?x:fa[x]=Find(fa[x]);
} int main(){
while(scanf("%d",&n)!=-){
Init();
for(int i=;i<=n;i++)
scanf("%d",&key[i]);
int x,y;
scanf("%d",&m);
while(m--){
scanf("%d%d",&x,&y);
x=Find(x);y=Find(y);
if(x==y)
printf("-1\n");
else{
int ta=rt[x],tb=rt[y];
Delete(x);key[ta]/=;rt[x]=Merge(rt[x],ta);
Delete(y);key[tb]/=;rt[y]=Merge(rt[y],tb);
fa[y]=x;rt[x]=Merge(rt[x],rt[y]);
printf("%d\n",key[rt[x]]);
}
}
}
return ;
}
数据结构(左偏树):HDU 1512 Monkey King的更多相关文章
- 【左偏树】[LuoguP1456] Monkey King
多...多组数据... awsl 死命的MLE,原来是忘记清空数组了.... 左偏树模板? 对于每一个操作,我们把两个节点$x,y$的祖先$fx,fy$找到,然后把他们的左右儿子分别合并 最后把$v[ ...
- hdu 1512 Monkey King 左偏树
题目链接:HDU - 1512 Once in a forest, there lived N aggressive monkeys. At the beginning, they each does ...
- HDU 1512 Monkey King(左偏树+并查集)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1512 [题目大意] 现在有 一群互不认识的猴子,每个猴子有一个能力值,每次选择两个猴子,挑出他们所 ...
- HDU 1512 Monkey King(左偏树模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1512 题意: 有n只猴子,每只猴子一开始有个力量值,并且互相不认识,现有每次有两只猴子要决斗,如果认识,就不打了 ...
- HDU 1512 Monkey King(左偏树)
Description Once in a forest, there lived N aggressive monkeys. At the beginning, they each does thi ...
- hdu 1512 Monkey King —— 左偏树
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1512 很简单的左偏树: 但突然对 rt 的关系感到混乱,改了半天才弄对: 注意是多组数据! #includ ...
- HDU 1512 Monkey King (左偏树+并查集)
题意:在一个森林里住着N(N<=10000)只猴子.在一开始,他们是互不认识的.但是随着时间的推移,猴子们少不了争斗,但那只会发生在互不认识 (认识具有传递性)的两只猴子之间.争斗时,两只猴子都 ...
- HDU 1512 Monkey King(左偏堆)
爱争吵的猴子 ★★☆ 输入文件:monkeyk.in 输出文件:monkeyk.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 在一个森林里,住着N只好斗的猴子.开始,他们各 ...
- HDU 1512 Monkey King
左偏树.我是ziliuziliu,我是最强的 #include<iostream> #include<cstdio> #include<cstring> #incl ...
- 数据结构(左偏树,可并堆):BNUOJ 3943 Safe Travel
Safe Travel Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java class ...
随机推荐
- Python开发【第二十二篇】:Web框架之Django【进阶】
Python开发[第二十二篇]:Web框架之Django[进阶] 猛击这里:http://www.cnblogs.com/wupeiqi/articles/5246483.html 博客园 首页 ...
- 解决ld: warning: directory not found for option警告
去掉警告的办法如下: 1选择工程, 编译的 (targets) 2选择 Build Settings 菜单 3查找 Library Search Paths 和 Framework Search Pa ...
- SGU 221.Big Bishops(DP)
题意: 给一个n*n(n<=50)的棋盘,放上k个主教(斜走),求能放置的种类总数. Solution : 同SGU 220,加个高精度就好了. code #include <iostre ...
- IOS下双击背景, touchmove, 阻止页面背景scroll.
ios prevent dblclick(tap) page scrollhtml add:("minimal-ui" is very important) <meta na ...
- 【转】 C语言自增自减运算符深入剖析
转自:http://bbs.csdn.net/topics/330189207 C语言的自增++,自减--运算符对于初学者来说一直都是个难题,甚至很多老手也会产生困惑,最近我在网上看到一个问题:#in ...
- 关于Linux 交互(用户操作接口)
Linux 系统提供两种基本接口给用户操作:命令行,图形界面. 不同接口也有相应的访问终端. 一.命令行 Command Line Linux系统命令行,一般指 Shell. Shell 接受经键盘输 ...
- IIS 服务器用OFFIC 2007 组件 WORD转PDF配置记录
<system.web> <identity impersonate="true" userName="accountname" passwo ...
- 从SQL2008R2导入数据到SQL2005
从SQL2008R2导入数据到SQL2005,数据很大,数据文件大概有120G. 尝试过直接离线附加,失败 尝试过备份还原,失败 最后找到了 1.先执行 exec sp_msforeachtable ...
- php中判断变量是否为空
从数据库中取出值后判断是否为空,这个看起来很简单,只要和null比较一下就可以了,其实不然, if($obj==null){ } 这样写会报错的:Notice: Trying to get prope ...
- 除法(Division ,UVA 725)-ACM集训
参考:http://www.cnblogs.com/xiaobaibuhei/p/3301110.html 算法学到很弱,连这么简单个问题都难到我了.但我偏不信这个邪,终于做出来了.不过,是参照别人的 ...