Monkey King

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4714    Accepted Submission(s): 2032

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.

 
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
 
 
  注意初始值:dis[0]=-1……
  因为delete掉的点还要在用,所以在delete时要清空ls和rs……
  
  这两个细节是致命的。
 #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的更多相关文章

  1. 【左偏树】[LuoguP1456] Monkey King

    多...多组数据... awsl 死命的MLE,原来是忘记清空数组了.... 左偏树模板? 对于每一个操作,我们把两个节点$x,y$的祖先$fx,fy$找到,然后把他们的左右儿子分别合并 最后把$v[ ...

  2. hdu 1512 Monkey King 左偏树

    题目链接:HDU - 1512 Once in a forest, there lived N aggressive monkeys. At the beginning, they each does ...

  3. HDU 1512 Monkey King(左偏树+并查集)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1512 [题目大意] 现在有 一群互不认识的猴子,每个猴子有一个能力值,每次选择两个猴子,挑出他们所 ...

  4. HDU 1512 Monkey King(左偏树模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1512 题意: 有n只猴子,每只猴子一开始有个力量值,并且互相不认识,现有每次有两只猴子要决斗,如果认识,就不打了 ...

  5. HDU 1512 Monkey King(左偏树)

    Description Once in a forest, there lived N aggressive monkeys. At the beginning, they each does thi ...

  6. hdu 1512 Monkey King —— 左偏树

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1512 很简单的左偏树: 但突然对 rt 的关系感到混乱,改了半天才弄对: 注意是多组数据! #includ ...

  7. HDU 1512 Monkey King (左偏树+并查集)

    题意:在一个森林里住着N(N<=10000)只猴子.在一开始,他们是互不认识的.但是随着时间的推移,猴子们少不了争斗,但那只会发生在互不认识 (认识具有传递性)的两只猴子之间.争斗时,两只猴子都 ...

  8. HDU 1512 Monkey King(左偏堆)

    爱争吵的猴子 ★★☆ 输入文件:monkeyk.in 输出文件:monkeyk.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 在一个森林里,住着N只好斗的猴子.开始,他们各 ...

  9. HDU 1512 Monkey King

    左偏树.我是ziliuziliu,我是最强的 #include<iostream> #include<cstdio> #include<cstring> #incl ...

  10. 数据结构(左偏树,可并堆):BNUOJ 3943 Safe Travel

    Safe Travel Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class ...

随机推荐

  1. Json字符串与字典互转

    #pragma mark 转换json字符串 +(NSString *)toJSON:(id)aParam { NSData   *jsonData=[NSJSONSerialization data ...

  2. iptables学习笔记

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/3812510.html ...

  3. ASP.NET JSON的序列化和反序列化 之 Newtonsoft.Json

    我们用到的类库为:Newtonsoft.Json,通过VS工具中NuGet程序包可以下载. 一:对象转json-序列化 public class Student { public int ID { g ...

  4. 重新看php数组

    闲来有空,最近看php手册数组这块,对于array_values() 还是第一次接触,array_values是不保留键名,只有键值的函数,还有一个作用就是  重新索引. unset() 函数,是删除 ...

  5. 版本控制-cvs

    我们实训用的是cvs. 团队协作: 代码版本控制软件:CVS.SVN.GIT(Git@开源中国) FTP:服务端.客户端 CVS: 服务端(源码仓库).客户端

  6. WPF 媒体播放器(MediaElement)实例,实现进度和音量控制

    WPF 视频音频播放控件MediaElement实现进度控制,音量控制实例 说明: 1.Volume控制音量的大小,double类型,并且实现了属性依赖,可以用来双向绑定:在 0 和 1. 之间的线性 ...

  7. Android 开发笔记——通过 Intent 传递类对象

    Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象. 要求被传递的对象必须实现上述2种接口中的一种 ...

  8. phpstrom+xdebug调试PHP代码

    众所周知开发PHP的IDE种类繁多,然而开发PHP并不能像开发其他语言一样,调试PHP代码对诸多新手来说,搭建调试环境就比较麻烦!其实哈,我发现NuSphere-phped-16.0很强大,集成了很强 ...

  9. java集合之链式操作

    如果用过js/jquery.groovy等语言,大概对这样的代码比较熟悉: [1,2,3].map(function(d){...}).grep(function(d){...}).join(',') ...

  10. OC - 19.GCD

    简介 GCD(Grand Center Dispatch)是Apple为多核的并行运算提出的解决方案,纯C语言 更加适配多核处理器,且自动管理线程的生命周期,使用起来较为方便 GCD通过任务和队列实现 ...