我又傻了……竟然忘了区别大根堆和小根堆的性质,以至于一个符号打错,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. Android为TV端助力 EventBus出现has no public methods called onEvent的问题

    Caused by: de.greenrobot.event.EventBusException: Subscriber class com.hhzt.iptv.lvb_w.socket.MyMsgS ...

  2. jsp:set/getProperty底层实现的探究

    关于jsp:set/getProperty底层实现的探究 今天上课讲到<jsp:useBean>时涉及到了<jsp:setProperty>和<jsp:getProper ...

  3. 从PM到非洲酋长,得人心者得天下

    说正事之前,先唠10块钱儿的…… 偶然看到房一波的故事,这个PM了不得了! 房兄是山东电建三公司,派驻到尼日利亚建设燃机电站的PM.本来在非洲,这种“万丈高楼平地起”的项目是很好干的,可是房兄却遭遇了 ...

  4. SQLServer 日期函数及日期转换数据类型

    一.统计语句 1.--统计当前[>当天00点以后的数据] SELECT * FROM 表 WHERE CONVERT(Nvarchar, dateandtime, 111) = CONVERT( ...

  5. 数据库原理剖析 - 序列1 - B+树

    本文节选自<软件架构设计:大型网站技术架构与业务架构融合之道>第6.3章节. 作者微信公众号: 架构之道与术.进入后,可以加入书友群,与作者和其他读者进行深入讨论.也可以在京东.天猫上购买 ...

  6. Jar 初步

    前言 jar 是 java 文件中一种文件格式,用于将 .java 文件编译的字节码文件打包成 jar. 给 Java 应用打包 1. 新建一个 java 源文件 package cn.szxy; p ...

  7. Powershell-创建Module

    1.找到默认module路径,ISE启动时自动加载默认领下的Module代码. $env:PSModulePath 2.在其中一个默认路径下创建个文件夹,在文件夹下创建一个.psm1后缀文件,注意文件 ...

  8. Hybrid APP之Native和H5页面交互原理

    Hybrid APP之Native和H5页面交互原理 Hybrid APP的关键是原生页面与H5页面直接的交互,如下图,痛过JSBridge,H5页面可以调用Native的api,Native也可调用 ...

  9. ASP.NET MVC 下自定义 JsonResult,使用 Json.NET 序列化 JSON

    直接贴代码了: using System; using System.Web.Mvc; using Newtonsoft.Json; namespace MvcSample.Extensions { ...

  10. marathon传参一

    今天试了下marathon传参,新建一个job,增加一个参数,然后用cmd方式,echo出来 定义的json: { "id": "test1", "l ...