洛谷P1456 Monkey King
https://www.luogu.org/problemnew/show/1456
#include<cstdio>
#include<iostream>
#include<algorithm> using namespace std; #define N 100001 struct node
{
int lc,rc;
int key,dis;
}e[N]; int fa[N]; void read(int &x)
{
x=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
} int find(int i) { return fa[i]==i ? i : fa[i]=find(fa[i]); } int merge(int a,int b)
{
if(!a) return b;
if(!b) return a;
if(e[a].key<e[b].key) swap(a,b);
e[a].rc=merge(e[a].rc,b);
if(e[e[a].lc].dis<e[e[a].rc].dis) swap(e[a].lc,e[a].rc);
if(!e[a].rc) e[a].dis=;
else e[a].dis=e[e[a].rc].dis+;
return a;
} int del(int x)
{
int lc=e[x].lc,rc=e[x].rc;
e[x].dis=e[x].lc=e[x].rc=;
return fa[lc]=fa[rc]=merge(lc,rc);
} int main()
{
int n,m;
int x,y,u,v,f,g,h;
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;++i)
{
read(e[i].key);
fa[i]=i;
e[i].dis=e[i].lc=e[i].rc=;
}
read(m);
while(m--)
{
read(x);
read(y);
x=find(x);
y=find(y);
if(x==y)
{
puts("-1");
continue;
}
e[x].key>>=;
e[y].key>>=; u=del(x);
v=del(y);
g=merge(u,v);
fa[u]=fa[v]=g; f=merge(x,y);
fa[x]=fa[y]=f; h=merge(g,f);
fa[g]=fa[f]=h;
cout<<e[h].key<<'\n';
}
}
}
题目描述
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.
有多组数据
输出格式:
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.
输入输出样例
洛谷P1456 Monkey King的更多相关文章
- P1456 Monkey King
		
题目地址:P1456 Monkey King 一道挺模板的左偏树题 不会左偏树?看论文打模板,完了之后再回来吧 然后你发现看完论文打完模板之后就可以A掉这道题不用回来了 细节见代码 #include ...
 - 【luogu P1456 Monkey King】 题解
		
题目链接:https://www.luogu.org/problemnew/show/P1456 左偏树并查集不加路径压缩吧... #include <cstdio> #include & ...
 - 洛谷P1456Monkey King
		
洛谷P1456 Monkey King 题目描述 Once in a forest, there lived N aggressive monkeys. At the beginning, they ...
 - 洛谷1377 M国王 (SCOI2005互不侵犯King)
		
洛谷1377 M国王 (SCOI2005互不侵犯King) 本题地址:http://www.luogu.org/problem/show?pid=1377 题目描述 天天都是n皇后,多么无聊啊.我们来 ...
 - [洛谷3457][POI2007]POW-The Flood
		
洛谷题目链接:[POI2007]POW-The Flood 题意翻译 Description 你手头有一张该市的地图.这张地图是边长为 m∗n 的矩形,被划分为m∗n个1∗1的小正方形.对于每个小正方 ...
 - POJ ???? Monkey King
		
题目描述 Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things i ...
 - 洛谷 P2504 [HAOI2006]聪明的猴子
		
洛谷 P2504 [HAOI2006]聪明的猴子 题目描述 在一个热带雨林中生存着一群猴子,它们以树上的果子为生.昨天下了一场大雨,现在雨过天晴,但整个雨林的地表还是被大水淹没着,部分植物的树冠露在水 ...
 - 【流水调度问题】【邻项交换对比】【Johnson法则】洛谷P1080国王游戏/P1248加工生产调度/P2123皇后游戏/P1541爬山
		
前提说明,因为我比较菜,关于理论性的证明大部分是搬来其他大佬的,相应地方有注明. 我自己写的部分换颜色来便于区分. 邻项交换对比是求一定条件下的最优排序的思想(个人理解).这部分最近做了一些题,就一起 ...
 - 洛谷1640 bzoj1854游戏 匈牙利就是又短又快
		
bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...
 
随机推荐
- 福大软工1816 · 评分结果 · Alpha冲刺答辩总结
			
作业地址:https://edu.cnblogs.com/campus/fzu/Grade2016SE/homework/2462 作业提交准则 按时交 - 有分 晚交 - 0分 迟交一周以上 - 倒 ...
 - Matlab图像匹配问题
			
已知一个任意形状,查找在大图像中最接近的形状位置. 输入:一个小图形状和一张大图 输出:最接近的形状在大图中的位置 假设: (1)已知形状与目标形状有一定的形变. (2)形状与大图像均为二值图像,图中 ...
 - 19_集合_第19天(List、Set)_讲义
			
今日内容介绍 1.List接口 2.Set接口 3.判断集合唯一性原理 非常重要的关系图 xmind下载地址 链接:https://pan.baidu.com/s/1kx0XabmT27pt4Ll9A ...
 - charles抓取移动端app数据
			
pc端为mac 移动端为android pc端 1.下载charles并安装 安利一个超好的良心网站(好多好用的软件都可以在上面找到,并且免费): http://xclient.info/search ...
 - 停止ipv6
			
在Centos5.5默认的状态下,ipv6是被启用的.因为我们不使用ipv6,所以,可以停止ipv6,以最大限度地保证安全和快速.首先确认一下ipv6是不是处于被启动的状态.[root@sample ...
 - PHP开发网站,你是如何提高其网站的安全性的?
			
一.网站程序问题 很多网站的安全问题大多是由于网站程序存在漏洞,所以想要提高网站安全性,必须要选择安全的后台cms系统,若有能力可以自己去开发网站后台,这样安全性能得到极大的提高,若是从网上选择一些免 ...
 - delphi开发学习四:TClientDataSet与TDataSetProvider控件使用实例
			
1.TClientDataSet控件 通过TClientDataSet控件可以建立瘦客户端的应用程序,且数据执行效率较高,但它不能和数据库自动连接,程序中必须制定它如何获取数据.一般情况下,TClie ...
 - python自动化之时间
			
cxz##############################现在时间######################### import time time.time() ############# ...
 - tensorflow环境下安装scikit-learn
			
1. scikit-learn所依赖的环境: python(>=2.6 or >=3.3) numpy(>=1.6.1) scipy(>=0.9) 可用conda list 查 ...
 - Qt——容器类(译)
			
注:本文是我对Qt官方文档的翻译,错误之处还请指正. 原文链接:Container Classes 介绍 Qt库提供了一套通用的基于模板的容器类,可以用这些类存储指定类型的项.比如,你需要一个大小可变 ...