洛谷P1456 Monkey King

题目描述

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次架,每打一次架,双方都会派出自己一方战斗力最强的猴子参战,打完架后,所派出的猴子的战斗力减半,同时两方猴子变为朋友,如果友方内部发生冲突,输出-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.

输出格式:

For each of the conflict, output -1 if the two monkeys know each other, otherwise output the strength value of the strongest monkey among all of its friends after the duel.

输入输出样例

输入样例#1:

5
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5
输出样例#1:

8
5
5
-1
10 这是一道左偏树的模板题,我发现单靠并查集是做不了的,于是去了解了一下左偏树这个东西。
打架前,先判断两只猴子是不是属于同一方,若是,输出-1;若不是,直接用左偏树的merge函数合并两方猴子,同时维护左偏树,输出根节点即可。
 #include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int n,m,x1,y;
int f1,f2,u,v;
struct node{
int r;
int l;
int v;
int dis;
int f;
}tr[];
int find(int x)
{
if(tr[x].f!=x) return find(tr[x].f);
return x;
}
int merge(int a,int b)//合并两方猴子
{
if(a==) return b;
if(b==) return a;
if(tr[a].v<tr[b].v) swap(a,b);//性质1:节点的键值小于或等于它的左右子节点的键值。
tr[a].r=merge(tr[a].r,b);//性质2:节点的左子节点右子节点也是一颗左偏树。
tr[tr[a].r].f=a;
if(tr[tr[a].l].dis<tr[tr[a].r].dis) swap(tr[a].l,tr[a].r);//性质:3:节点的左子节点的距离不小于右子节点的距离。
if(tr[a].r==) tr[a].dis=;
else tr[a].dis=tr[tr[a].r].dis+;
return a;
}
int pop(int a)
{
int l=tr[a].l;
int r=tr[a].r;
tr[l].f=l;
tr[r].f=r;
tr[a].l=tr[a].r=tr[a].dis=;
return merge(l,r);
}
int main()
{
while(scanf("%d",&n)!=EOF)//多组数据
{
for(int i=;i<=n;i++)
{
scanf("%d",&tr[i].v);
tr[i].l=tr[i].r=tr[i].dis=;
tr[i].f=i;
}
scanf("%d",&m);
for(int i=;i<=m;i++)
{
scanf("%d%d",&x1,&y);
f1=find(x1);
f2=find(y);
if(f1==f2)
printf("-1\n");
else
{
tr[f1].v/=;
u=pop(f1);
u=merge(u,f1);
tr[f2].v/=;
v=pop(f2);
v=merge(v,f2);
printf("%d\n",tr[merge(u,v)].v);
}
}
}
return ;
}

洛谷P1456Monkey King的更多相关文章

  1. 洛谷 P1456Monkey King

    题目描述 要把打架的两堆猴子合并为一堆,查询的又是最大值,所以很容易想到可并堆. 题目要求打完架后战斗力最大的猴子的战斗力要减半,但不能直接在堆中进行这个操作,因为战斗力减半后这只猴子不一定是战斗力最 ...

  2. 洛谷1377 M国王 (SCOI2005互不侵犯King)

    洛谷1377 M国王 (SCOI2005互不侵犯King) 本题地址:http://www.luogu.org/problem/show?pid=1377 题目描述 天天都是n皇后,多么无聊啊.我们来 ...

  3. [洛谷3457][POI2007]POW-The Flood

    洛谷题目链接:[POI2007]POW-The Flood 题意翻译 Description 你手头有一张该市的地图.这张地图是边长为 m∗n 的矩形,被划分为m∗n个1∗1的小正方形.对于每个小正方 ...

  4. 【流水调度问题】【邻项交换对比】【Johnson法则】洛谷P1080国王游戏/P1248加工生产调度/P2123皇后游戏/P1541爬山

    前提说明,因为我比较菜,关于理论性的证明大部分是搬来其他大佬的,相应地方有注明. 我自己写的部分换颜色来便于区分. 邻项交换对比是求一定条件下的最优排序的思想(个人理解).这部分最近做了一些题,就一起 ...

  5. 洛谷1640 bzoj1854游戏 匈牙利就是又短又快

    bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...

  6. 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.

    没有上司的舞会  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...

  7. 洛谷P1108 低价购买[DP | LIS方案数]

    题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...

  8. 洛谷 P2701 [USACO5.3]巨大的牛棚Big Barn Label:二维数组前缀和 你够了 这次我用DP

    题目背景 (USACO 5.3.4) 题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N ...

  9. 洛谷P1710 地铁涨价

    P1710 地铁涨价 51通过 339提交 题目提供者洛谷OnlineJudge 标签O2优化云端评测2 难度提高+/省选- 提交  讨论  题解 最新讨论 求教:为什么只有40分 数组大小一定要开够 ...

随机推荐

  1. natapp出现Invalid Host header

    前端是vue 3.x 项目,需要更改vue的配置文件 vue.config.js,在module.exports中添加devServer:{disableHostCheck:true}

  2. 33 N皇后问题

    原题网址:https://www.lintcode.com/zh-cn/old/problem/n-queens/# n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击. 给定一个整 ...

  3. Matlab系列作业

    (2019年2月19日注:Matlab这门课是在我大四上学期经历的,那时候开这篇文章是为了记录学习Matlab的时候遇到的坑,所以将所有的作业题合并到一篇文章中) 1.创建一个10*10的矩阵,矩阵所 ...

  4. Python学习day37-并发编程(3)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  5. Python学习day03 - Python基础(1)

    1. 执行Python程序的两种方式 (1)交互式(Jupyter) 优点:运行一句执行一句 缺点:关闭即消失# (2)命令行式(pycharm) 优点:可以一直保存 缺点:全部写完才能调试bug虽然 ...

  6. vue 组件内引入外部在线js、css

    参考:https://blog.csdn.net/u010881899/article/details/80895661 例:引入element-ui js: mounted() { const oS ...

  7. linux NC网络通信工具的安装

    由于开发socket一类工具需要用到管道进行测试,就想到安装个linux nc工具来测试 于是上网找了个rpm安装完发现不可用(公司服务器没外网) 报错内容: [root@rhel071 instal ...

  8. springboot新增jsp的支持

    一.添加依赖 <!-- 添加对jsp的支持 --> <!-- web 依赖 --> <dependency> <groupId>org.springfr ...

  9. Android Studio && NDK开发

    Android Studio下载安装网址:http://www.android-studio.org/index.php/download/hisversion 在下载界面可以查看安装包内是否包含SD ...

  10. xshell上传文件到linux

    z,sz是Linux/Unix同Windows进行ZModem文件传输的命令行工具. 优点就是不用再开一个sftp工具登录上去上传下载文件. sz:将选定的文件发送(send)到本地机器 rz:运行该 ...