我又傻了……竟然忘了区别大根堆和小根堆的性质,以至于一个符号打错,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. 升鲜宝V2.0_生鲜配送行业,对生鲜配送系统开发与实施的深度对比与思考_升鲜宝生鲜配送系统_15382353715_余东升

               升鲜宝V2.0_生鲜配送行业,对生鲜配送系统开发与实施的深度对比与思考_升鲜宝生鲜配送系统_15382353715_余东升 笔者从事生鲜配送软件开发接近10年,前前后后研究了很多 ...

  2. 用 Python 描述 Cookie 和 Session

    这篇文章我们来聊聊Cookie和Session,网上有很多关于这两个知识点的描述,可惜的是大部分都没有示例代码,因此本文的重点在于示例代码. 环境 Python3.6.0 Bottle0.12.15 ...

  3. C#标识符与关键字

    标识符是指在程序中用来表示实物的单词,是分配给类型(类.结构.枚举.接口或委托).成员.变量或命名空间的名称.有效标识符必须遵循以下原则: 标识符不能以数字开头也不能包含空格: 标识符可以包含大小写字 ...

  4. eclipse 导入gradle引入多模块项目,引入eclipse后变成了好几个工程

    1.eclipse  导入gradle 项目 ,选择项目文件夹. 2.导入完成后,文档结构变成 ,多个子项目并列了,而且互不依赖,没有层级结构了. 3.点击项目目录,右上角这个小箭头,选择projec ...

  5. SpringMVC的相关知识

    前几天学习了SpringMVC 感觉比Servlet好用得多具体如下: 首先SpringMVC的概念: SpringMVC是一个前端控制框架,主要用来负责与页面的交互.SpringMVC是Spring ...

  6. redux 简介

    概述 Redux 本身是个极其简单的状态管理框架, 它的简单体现在概念少, 流程明确. 但是, 正是因为简单, 使用上没有强制的约束, 所以如果用不好, 反而会让状态管理更加混乱. 我觉得, 用好 R ...

  7. SQL FIRST() 函数

    FIRST() 函数 FIRST() 函数返回指定的字段中第一个记录的值. 提示:可使用 ORDER BY 语句对记录进行排序. SQL FIRST() 语法 SELECT FIRST(column_ ...

  8. Linux命令之常用篇

    一.文件和目录 1. cd命令 它用于切换当前目录,它的参数是要切换到的目录的路径,可以是绝对路径,也可以是相对路径. 指令 说明 cd /home 进入‘home’目录 cd .. 返回上一级目录 ...

  9. openstack基础框架

    openstack组件:     Computer--代码名Nova,管理VM的整个生命周期,主要职责包括启动,调度VMs,计算节点上需要至少两块网卡     Networking--代码名Neutr ...

  10. maven依赖scope配置项讲解(转)

    原文:https://blog.csdn.net/lisongjia123/article/details/56299006 <scope>的分类一.complie编译域,这个是Maven ...