题意:

猴子们打架  认识的猴子不会打架  两仅仅猴子打完以后就认识了  A认识B B认识C A也认识C  每次打架由两伙猴子进行  分别选出自己的最高战斗力  在战斗之后两仅仅猴子战斗力减半  给出m次打架  输出打架后这一伙猴子里的最强战斗力

思路:

推断两仅仅猴子是不是一伙的  用到并查集

高速找出一伙猴子中的最强战斗力用到堆  但打完架两伙猴子合并时堆须要nlogn复杂度  因此用左偏树取代堆

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 100010 int n,m,tot;
int fa[N];
struct node
{
int v,l,r,dis;
}nd[N]; int getf(int x)
{
if(x!=fa[x]) fa[x]=getf(fa[x]);
return fa[x];
} int newnode(int x)
{
nd[tot].v=x;
nd[tot].l=0;
nd[tot].r=0;
nd[tot].dis=0;
tot++;
return tot-1;
} int merge(int x,int y)
{
if(!x) return y;
if(!y) return x;
if(nd[x].v<nd[y].v) swap(x,y);
nd[x].r=merge(nd[x].r,y);
if(nd[nd[x].l].dis<nd[nd[x].r].dis) swap(nd[x].l,nd[x].r);
nd[x].dis=nd[nd[x].r].dis+1;
return x;
} int pop(int x)
{
int tmp=merge(nd[x].l,nd[x].r);
nd[x].dis=nd[x].l=nd[x].r=0;
return tmp;
} int main()
{
int i,j,fi,fj,x;
while(~scanf("%d",&n))
{
tot=1;
for(i=1;i<=n;i++)
{
scanf("%d",&j);
fa[i]=newnode(j);
}
scanf("%d",&m);
while(m--)
{
scanf("%d%d",&i,&j);
fi=getf(i);
fj=getf(j);
if(fi==fj) puts("-1");
else
{
i=pop(fi);
nd[fi].v/=2;
j=pop(fj);
nd[fj].v/=2;
i=merge(i,j);
i=merge(i,fi);
i=merge(i,fj);
fa[i]=fa[fi]=fa[fj]=i;
printf("%d\n",nd[i].v);
}
}
}
return 0;
}

ZOJ 2334 HDU 1512 Monkey King的更多相关文章

  1. hdu 1512 Monkey King 左偏树

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

  2. 数据结构(左偏树):HDU 1512 Monkey King

    Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  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 —— 左偏树

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

  6. HDU 1512 Monkey King(左偏树)

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

  7. HDU 1512 Monkey King ——左偏树

    [题目分析] 也是堆+并查集. 比起BZOJ 1455 来说,只是合并的方式麻烦了一点. WA了一天才看到是多组数据. 盲人OI (- ̄▽ ̄)- Best OI. 代码自带大常数,比启发式合并都慢 [ ...

  8. HDU 1512 Monkey King

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

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

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

随机推荐

  1. proxool数据库连接池用法

    今天给大家介绍一种新的数据连接池实现方式--proxool数据库连接池,这是一个健壮.易用的连接池.以下通过一个Demo说明一下怎样使用: 项目结构例如以下: DBLink.java文件里的代码: p ...

  2. [Angular 2] Share Template Content In Another Template With Content Projection <ng-content>

    Angular 1 provided a mechanism to place content from your template inside of another template called ...

  3. POJ 1163 The Triangle 简单DP

    看题传送门门:http://poj.org/problem?id=1163 困死了....QAQ 普通做法,从下往上,可得状态转移方程为: dp[i][j]= a[i][j] + max (dp[i+ ...

  4. ARM+linux学习过程(1)虚拟机下ubuntu上网

    总结:(1)通过bridge方式也可以实现ubuntu上网(只要PC物理网卡能上网),可以实现ping通主机和开发板 (2)要想上网简单上网,可以通过nat方式,在vmware中设置为nat方式,选择 ...

  5. spring(3)------控制反转(IOC)/依赖注入(DI)

    一.spring核心概念理解 控制反转: 控制反转即IoC (Inversion of Control).它把传统上由程序代码直接操控的对象的调用权交给容器.通过容器来实现对象组件的装配和管理. 所谓 ...

  6. 【27.77%】【BZOJ 4066】简单题

    Time Limit: 50 Sec  Memory Limit: 20 MB Submit: 1919  Solved: 533 [Submit][Status][Discuss] Descript ...

  7. php课程 5-18 数组排序和合并拆分函数有哪些

    php课程  5-18   数组排序和合并拆分函数有哪些 一.总结 一句话总结:分类来记.这些函数自己都可以写,费点时间而已. 1.array_combine()和array_merge()的区别是什 ...

  8. Windows Phone 8.1 控件

    如果你已经开始了 Windows Phone 8.1 的学习,就会发现许多在 8.0 下的控件在 8.1 中都发生了变化,以下就谈谈几个 8.1 下的新控件以及与 8.0 控件的改变. 1. Text ...

  9. Snmp linux

    http://blog.csdn.net/youngqj/article/details/7311849 http://blog.csdn.net/howema/article/details/418 ...

  10. [Angular] Implementing a ControlValueAccessor

    So when you need to create a ControlValueAccessor? When you want to use a custom component as form c ...