我又傻了……竟然忘了区别大根堆和小根堆的性质,以至于一个符号打错,debug了半天……(我真是太菜了……)

题目描述

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.

一开始有n只孤独的猴子,然后他们要打m次架,每次打架呢,都会拉上自己朋友最牛叉的出来跟别人打,打完之后战斗力就会减半,每次打完架就会成为朋友(正所谓不打不相识o(∩_∩)o )。问每次打完架之后那俩猴子最牛叉的朋友战斗力还有多少,若朋友打架就输出-1.

输入输出格式

输入格式:

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.

有多组数据

输入样例:


输出样例:


-

解:

每次打完架成为朋友,告诉了我们要合并;查找两人是不是朋友,告诉我们要用路径压缩并查集;找最大值,告诉了我们要用大根堆。

这题做完了。注意merge的符号,以及多重数据的提醒。

题目中要求把堆顶的值减半。然而不方便直接减,于是我们先把它与它的子树断掉,然后合并它的两颗子树,再把它合并进去即可。

注意合并两个堆,最终要把它们全部合并并且输出堆顶。

Code:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#define ll long long
#define lc tr[x].l
#define rc tr[x].r
using namespace std;
const int MAXN=;
inline void swap(int &x,int &y){x^=y^=x^=y;}
inline int read(){
char ch=getchar();
int s=,w=;while(ch<''||ch>''){if(ch=='-')w=-;ch=getchar();}
while(ch>=''&&ch<=''){s=(s<<)+(s<<)+(ch^);ch=getchar();}return s*w;
}
struct node{
int l,r,dis,w,fa;
}tr[MAXN];
int merge(int x,int y){
if(!x||!y)return x+y;
if(tr[x].w<tr[y].w||(tr[x].w==tr[y].w&&x<y))swap(x,y);
rc=merge(rc,y);tr[rc].fa=tr[lc].fa=x;
if(tr[lc].dis<tr[rc].dis)swap(lc,rc);tr[x].dis=tr[rc].dis+;
return x;
}
inline int getf(int x){return x==tr[x].fa?x:tr[x].fa=getf(tr[x].fa);}
void work(int x,int y){
x=getf(x),y=getf(y);
if(x==y){
puts("-1");
return;
}
int rt,art,brt;
tr[x].w>>=;
rt=merge(tr[x].l,tr[x].r);
tr[x].l=tr[x].r=tr[x].dis=;
art=merge(rt,x);
tr[rt].fa=tr[x].fa=art;
tr[y].w>>=;
rt=merge(tr[y].l,tr[y].r);
tr[tr[y].l].fa=tr[tr[y].r].fa=rt;
tr[y].l=tr[y].r=tr[y].dis=;
brt=merge(rt,y);
tr[rt].fa=tr[y].fa=brt;
rt=merge(art,brt);
tr[art].fa=tr[brt].fa=rt;
printf("%d\n",tr[rt].w);
}int n,m;
int main(){
while(~scanf("%d",&n)){
tr[].dis=-;
for(register int i=;i<=n;i++){
tr[i].fa=i;
tr[i].w=read();
tr[i].dis=tr[i].l=tr[i].r=;
}m=read();
for(register int i=;i<=m;i++)work(read(),read());
}
return ;
}

Money King【题解】的更多相关文章

  1. POJ2728:Desert King——题解

    http://poj.org/problem?id=2728 题目大意:求一棵生成树使得路费用和/路长之和最小(路的费用是两端点的高度差) 最小比率生成树. 我们还是01分数规划的思想将边权变为路费用 ...

  2. DP——由蒟蒻到神犇的进阶之路

    开始更新咯 DP专题[题目来源BZOJ] 一.树形DP 1.bzoj2286消耗战 题解:因为是树形结构,一个点与根节点不联通,删一条边即可, 于是我们就可以简化这棵树,把有用的信息建立一颗虚树,然后 ...

  3. POJ 1904 King's Quest(强连通图)题解

    题意:n个王子有自己喜欢的ki个公主,有n个公主,每个王子只能娶一个自己喜欢的公主且不能绿别的王子.现在给你一种王子娶公主的方案,并且保证这种方案是正确的.请你给出,每个王子能娶哪些公主,要求娶这些公 ...

  4. 【luogu P1456 Monkey King】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1456 左偏树并查集不加路径压缩吧... #include <cstdio> #include & ...

  5. LuoguP7041 [NWRRC2016]King's Heir 题解

    Content 给出现在的日期,请从 \(n\) 个人当中选出一个人,使得他是所有成年人(\(\geqslant 18\) 岁的人)中年龄最小的. 数据范围:设日期为 \(yy/mm/dd\),则有 ...

  6. BZOJ-1087 互不侵犯King 状压DP+DFS预处理

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2337 Solved: 1366 [Submit][ ...

  7. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

  8. cdoj 93 King's Sanctuary 傻逼几何题

    King's Sanctuary Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/sho ...

  9. hdu 5640 King's Cake(BestCoder Round #75)

    King's Cake Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

随机推荐

  1. Dynamics 365-为什么查到的Record的Id是Guid初始值

    通过代码查询CRM数据,这个是开发经常会碰到的情况,获取返回的EntityCollection之后,我们会拿Entity.Id做进一步操作.笔者最近碰到的情况,是Entity.Id是个初始值.先上一段 ...

  2. Android应用跳转到微信和支付宝扫一扫

    新版的微信已经把微信功能的schema都禁掉了,意味着我们无法打开微信的扫一扫等功能,目前正常的只能先进入微信(我测试的时候是微信版本7.0.3)已经是很新的版本了 具体调起微信扫一扫代码如下,测试后 ...

  3. typescript中类的继承

    typescript中类的继承用到的是:extends和super 先看一下typescript中类的写法: class Demo{ //类的属性 name:string; age:number; / ...

  4. numpy 基础操作

    Numpy 基础操作¶ 以numpy的基本数据例子来学习numpy基本数据处理方法 主要内容有: 创建数组 数组维度转换 数据选区和切片 数组数据计算 随机数 数据合并 数据统计计算 In [1]: ...

  5. 自动化批量管理工具pssh - 运维小结

    pssh提供OpenSSH和相关工具的并行版本.包括pssh,pscp,prsync,pnuke和pslurp.该项目包括psshlib,可以在自定义应用程序中使用.pssh是python写的可以并发 ...

  6. MVC 伪静态路由、MVC路由配置,实现伪静态。

    前段时间,研究了一下mvc路由配置伪静态,在网上扒了很多最后还是行不通,所以我现在把这些心得整理出来,供大家分享: 1.mvc中默认路由配置是:http://localhost:24409/Home/ ...

  7. mongodb复制+分片集原理

    ----------------------------------------复制集---------------------------------------- 一.复制集概述: Mongodb ...

  8. android获取string.xml的值

    在android开发过程中,编写java代码中的常量过一般情况下,我们是定义在string.xml这个文件中.这样修改起来也很方便,而且做国际化也很简单. 这个string.xml的值会被R文件映射, ...

  9. 28 Python初学(事件驱动模型)

    参考文章地址:http://www.cnblogs.com/yuanchenqi/articles/5722574.html 两个步骤: recvfrom 系统调用 : 拷贝数据 从kernel到数据 ...

  10. Luogu P1038 神经网络

    qwq 拓扑排序模板题. 拓扑排序,是在一个$DAG$中,其拓扑排序为其所有结点的一个线性排序(答案不唯一). 该排序满足这样的条件——对于图中的任意两个结点$u$和$v$,若存在一条有向边从$u$指 ...