洛谷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. tip:删除数组中的undefined

    this.checkedImg = this.checkedImg.filter(Boolean)

  2. 我喜欢Mouding

    我Smily喜欢Mouding

  3. 解析Request和Response

    简介: Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象. request和response对象即然代表请求和响应 ...

  4. 整合SSH框架最基本的例子

    ssh框架整合 一.思路 1.导包 struts2: \apps\struts2-blank\WEB-INF\lib\所有包 struts2-spring-plugin-2.3.28.jar hibe ...

  5. 对话框处理Enter,Esc键相应问题

    在类视图里面选择你要实现的类,右键属性,在属性里面找到函数PreTranslateMessage,然后添加PreranslateMessage的消息函数,在PreTranslateMessage的消息 ...

  6. springBoot 项目 jar/war打包 并运行

    一:idea  打jar  包 简述:springboor  项目最好的打包方式就是打成jar  ,下边就是简单的过程 1.打开idea工具 ,选着要打开的项目, 然后打开view--> too ...

  7. 从NoSQL到NewSQL数据库

  8. 打开springboot的run dashboard

    默认情况下,idea的run dashboard是关闭的,当检测到你有多个springboot项目时会弹出提示框,询问是否打开. 如果我们想要自己打开,需要修改配置. 在你的idea的项目目录中,有一 ...

  9. (补充)06.Hibernate持久化类&持久化对象

    持久化类:就是一个Java类(JavaBean),这个类与表建立映射关系就可以成为是持久类 持久化类 = JavaBean + xxx.hbm.xml 编写规则: 1.提供一个无参数,public访问 ...

  10. C# Predicate委托

    Predicate在集合搜索和WPF数据绑定中用途广泛,其调用形式: 调用形式:Predicate<object>(Method)/Predicate<参数类型>(方法) 1. ...