题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1512

题目大意:有n个猴子,一开始每个猴子只认识自己。每个猴子有一个力量值,力量值越大表示这个猴子打架越厉害。如果2个猴子不认识,他们就会找他们认识的猴子中力量最大的出来单挑,单挑不论输赢,单挑的2个猴子力量值减半,这2拨猴子就都认识了,不打不相识嘛。现在给m组询问,如果2只猴子相互认识,输出-1,否则他们各自找自己认识的最牛叉的猴子单挑,求挑完后这拨猴子力量最大值。

/*
每次给出要争吵的猴子a和b,用并查集判断如果他们是朋友输出-1
如果不是,找出a,b在的堆的根A,B,分别合并A,B的左右孩子,再合并一下。
之后把A,B的数据更改一下:权值除以2,左右孩子设为0,再插入到堆中即可。
最后输出堆顶。
*/
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = ;
int m,n;
int set[N];
struct node
{
int l,r,dis,key;
}tree[N]; int find(int x) {return set[x] == x ? x : set[x] = find(set[x]);} int merge(int a,int b)
{
if(!a)
return b;
if(!b)
return a;
if(tree[a].key < tree[b].key)//大堆
swap(a,b);
tree[a].r = merge(tree[a].r,b);
set[tree[a].r] = a;//并查
if(tree[tree[a].l].dis < tree[tree[a].r].dis)
swap(tree[a].l,tree[a].r);
if(tree[a].r)
tree[a].dis = tree[tree[a].r].dis + ;
else
tree[a].dis = ;
return a;
} int pop(int a)
{
int l = tree[a].l;
int r = tree[a].r;
set[l] = l;//因为要暂时删掉根,所以左右子树先作为根
set[r] = r;
tree[a].l = tree[a].r = tree[a].dis = ;
return merge(l,r);
} int nextint()
{
char c;
int ret = ;
while((c = getchar()) > '' || c < '')
;
ret = c - '';
while((c = getchar()) >= '' && c <= '')
ret = ret * + c - '';
return ret;
} void print(int a)
{
if(!a)
return;
print(a/);
putchar(a% + '');
} int main()
{
int a,b,i;
while(~scanf("%d",&n))
{
for(i = ;i <= n;i ++)
{;
tree[i].key = nextint();
set[i] = i;
tree[i].l = tree[i].r = tree[i].dis = ;
}
m = nextint();
while(m --)
{
a = nextint();
b = nextint();
int ra = find(a);
int rb = find(b);
if(ra == rb)
printf("-1\n");
else
{
int rra = pop(ra);//ra左右子树合并
tree[ra].key /= ;
ra = merge(rra,ra);//重新插入ra 找到合适的位置
int rrb = pop(rb);
tree[rb].key /= ;
rb = merge(rrb,rb);
print(tree[merge(ra,rb)].key);
putchar();
}
}
}
return ;
}

心若向阳,无谓悲伤

hdu1512 Monkey King(并查集,左偏堆)的更多相关文章

  1. ZOJ2334 Monkey King 并查集 STL

    题意:两家原始人(猴)打交道后成为一家猴,打交道时两家分别派出最帅的两位猴子,颜值各自减半,问每次打交道后新家族最帅的猴子的颜值.当然,已经是一家子就没有必要打交道了,因为没有猴希望颜值降低,毕竟还得 ...

  2. HDU 1512 Monkey King(左偏堆)

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

  3. 二叉堆&&左偏堆 代码实现

    今天打算学习左偏堆,可是想起来自己二叉堆都没有看懂,于是就跑去回顾二叉堆了.发现以前看不懂的二叉堆,今天看起来特简单,随手就写好了一个堆了. 简单的说一下我对二叉堆操作的理解.我不从底层函数说上去,相 ...

  4. [HDU1512]Monkey King(左偏树)

    用并查集维护猴子们的关系,强壮值用左偏树维护就行了 Code #include <cstdio> #include <algorithm> #include <cstri ...

  5. hdu1512 Monkey King

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

  6. BZOJ 2333 SCOI2011 棘手的操作 并查集+可并堆

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2333 ..题意概述就不写了,各位老爷如果是看着玩的可以去搜一下,如果是做题找来的也知道题干 ...

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

    Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its o ...

  8. 洛谷 - P3377 - 【模板】左偏树(可并堆) - 左偏树 - 并查集

    https://www.luogu.org/problemnew/show/P3377 左偏树+并查集 左偏树维护两个可合并的堆,并查集维护两个堆元素合并后可以找到正确的树根. 关键点在于删除一个堆的 ...

  9. ZOJ 2334 Monkey King

    并查集+左偏树.....合并的时候用左偏树,合并结束后吧父结点全部定成树的根节点,保证任意两个猴子都可以通过Find找到最厉害的猴子                       Monkey King ...

随机推荐

  1. Codeforces 631B Print Check【模拟】

    题意: 按顺序给定列和行进行涂色,输出最终得到的方格颜色分布. 分析: 记录下涂的次序,如果某个元素的横和列都被涂过,那么就选择次序最大的颜色. 代码: #include<iostream> ...

  2. POJ1328 Radar Installation 解题报告

    Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...

  3. Search Insert Position(二分查找)

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. [HDU5306]Gorgeous Sequence(标记回收线段树)

    题意:维护一个序列,支持区间与一个数取min,询问区间最大,询问区间和(序列长度<=1e6) 分析: http://www.shuizilong.com/house/archives/hdu-5 ...

  5. Nginx/Spring:增加上传文件尺寸限制

    Nginx: 1. vi /etc/nginx/nginx.config 2. 在server中增加如下行 # set client body size to 20M # client_max_bod ...

  6. 条款十七: 在operator=中检查给自己赋值的情况

    在赋值运算符中要特别注意可能出现别名的情况,其理由基于两点.其中之一是效率.如果可以在赋值运算符函数体的首部检测到是给自己赋值,就可以立即返回,从而可以节省大量的工作,否则必须去实现整个赋值操作. 另 ...

  7. Linux系统调用过程分析

    參考: <Linux内核设计与实现> 0 摘要 linux的系统调用过程: 层次例如以下: 用户程序------>C库(即API):INT 0x80 ----->system_ ...

  8. 数学之路-python计算实战(21)-机器视觉-拉普拉斯线性滤波

    拉普拉斯线性滤波,.边缘检測  . When ksize == 1 , the Laplacian is computed by filtering the image with the follow ...

  9. jmeter获取时间_time 函数

    原始时间戳13位精确到毫秒:${__time(,)} 时间戳精确到秒10位:${__time(/1000,)} 时间日期到年月日2019-04-21:${__time(yyyy-MM-dd,)} 时间 ...

  10. 通讯编程入门--WEBSOCKET

    C#通讯编程入门--WEBSOCKET WebSocket服务端 C#示例代码 using System; using System.Collections.Generic; using System ...