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. windows10UWP:Segoe MDL2 图标指南

    windows10 UWP 开发中,图标的使用非常广泛.为此,微软建议大家使用 Segoe MDL2 Assets 字体来获取图标.Segoe MDL2 Assets 包括了哪里图标,微软在 http ...

  2. 过滤掉html 标签

    /// <summary> /// 过滤掉html标签 /// </summary> /// <param name="Htmlstring"> ...

  3. VS2015 Cordova Ionic移动开发(五)

    一.创建侧边菜单和导航项目 1.使用VS创建一个Ionic空项目,同时创建一个Ionic SideMenu和Ionic Tabs项目.将SideMenu和Tabs项目里的templates和js文件合 ...

  4. postgre sql 字符串转为integer类型

    select cast(setting_value as integer) from ud_organization_setting. select cast('123123' as integer) ...

  5. sql-从查询结果创建一个永久表

    语法: select x into new_tableName  from ori_tableName 例如: SELECT [site] ,[day] ,[val]/31.4 [val] into ...

  6. javascript DOM小结

    一:定义 dom:文档对象模型. dom是针对HTML和XML文档的一个API.dom描绘了一个层次化的节点树,允许开发人员添加.移除.修改页面的某一部分. 1:childNodes(返回当前节点的子 ...

  7. 文字排版--下划线(text-decoration:underline)

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  8. 用arm-linux-gcc v4.3.4交叉编译Qt4.8.3

    1.解压缩 #tar zxvf  qt-everywhere-opensource-src-4.8.3.tar.gz 2. configure #mkdir buildarm-static #cd b ...

  9. Mac使用rz、sz远程上传下载文件

    习惯了 在windows下的securecrt和xshell的rz 和sz. rz 可以很方便的从客户端传文件到服务器,sz也可以很方便的从服务器传文件到客户端,就算中间隔着跳板机也不影响.在mac下 ...

  10. linux 监控系统缓存和cpu

    a=`free |head -n 2 |tail -n 1 |awk '{print $7}'`if [ $a -ge 900000 ];then     sync && echo 1 ...