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. c++矩阵运算

    优化了一些算法 #pragma once #include <iostream> #include <iomanip> #include <string> #def ...

  2. [Redis] C#中使用redis

    C#中使用redis 首先打开Visual Studio建立一个简单的控制台应用程序,我这里暂时使用的VS2013的版本. 然后通过Nuget进行安装Redis常用组件ServiceStack.Red ...

  3. java之log4j的配置

    java之log4j的配置 log4j有很多的优点,用起来很方便,就是配置起来有些麻烦,下面我介绍一下log4j的配置方法. log4j是用来记录日志的. 软件的运行过程离不开日志.日志主要用来记录系 ...

  4. Java-分页实例

    1.PageModel.java package com.javaweb; import java.util.List; public class PageModel<E> { priva ...

  5. Axiom3D学习日记 2.介绍SceneManager,SceneNode,Entity

    SceneManager(场景管理类) 所有出现在屏幕里的东西都受SceneManager管理(最好是这样),当你放置对象在场景里,SceneManager就会跟踪他们的位置,当你为场景创建一个相机, ...

  6. RegistryKey 类

    表示 Windows 注册表中的项级节点. 此类是注册表封装. 继承层次结构 System.Object   System.MarshalByRefObject    Microsoft.Win32. ...

  7. Netbeans IDE配置

  8. PageMapAdapter MapAdapter (续webServices)

    public class PageMapAdapter extends XmlAdapter<PageMapConverter, IPage<Map<String, Object&g ...

  9. 删除我的电脑中360随身WiFi云U盘的图标

    可通过删除注册表的方法 运行-regedit 找到这个项 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\My ...

  10. 谈谈oracle中的临时表

    --------------------创建临时表 临时保存从xml字符串解析来的数据--------------------------- 会话级别临时表SQL> create global ...