洛谷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炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...
随机推荐
- Windows服务器安全配置指南
1).系统安全基本设置 2).关闭不需要的服务 Computer Browser:维护网络计算机更新,禁用 Distributed File System: 局域网管理共享文件,不需要禁用 Distr ...
- 谈谈Keil 中C51的内存分配与优化
本帖最后由 Cresta 于 2014-1-21 10:49 编辑 看到这篇C51的内存分配和优化的文章,个人觉得分析的十分到位,在这里转给大家 C51的内存分配不同于一般的PC,内存空间有限,采 ...
- .NET 类库研究必备参考 扣丁格鲁
.NET 类库的强大让我们很轻松的解决常见问题,作为一个好专研的程序员,为了更上一层楼,研究CLR的基础类库实现是快速稳定的捷径. 一般场景下,采用 Reflector可以反射出.NET 的部分实现出 ...
- PAT 1024 科学计数法
https://pintia.cn/problem-sets/994805260223102976/problems/994805297229447168 科学计数法是科学家用来表示很大或很小的数字的 ...
- mongo学习1 (转)
关于mongodb的好处,优点之类的这里就不说了,唯一要讲的一点就是mongodb中有三元素:数据库,集合,文档,其中“集合” 就是对应关系数据库中的“表”,“文档”对应“行”. 一: 下载 上Mon ...
- net user 修改密码的坑
不多说 直接上图 自己偷懒修改 admin的密码.. 结果没注意 这个地方 能够输入全角字符. 造成密码 实质上是全角的 标点符号 ... 以后一定注意一些. 里面的坑..说多了 都是浪费时间 另外 ...
- MachineLearning Exercise 7 : K-means Clustering and Principle Component Analysis
findClosestCentroids.m m = size(X,); :m [value index] = min(sum((repmat(X(i,:),K,)-centroids).^,)); ...
- java递归方法求数组最大元素
一直对递归写法不是很熟悉,特写一个增进理解 /** * Created by Administrator on 2017-11-01. */ public class recursion { priv ...
- oracle 空表导出dmp会报错
步骤一.查找出来的语句全部复制并执行 select 'analyze table '||table_name||' compute statistics;' from user_tables; 步骤二 ...
- codevs2464超级麻将
题目链接http://codevs.cn/problem/2464/ 题目描述 Description 很多人都知道玩麻将,当然也有人不知道,呵呵,不要紧,我在这里简要地介绍一下麻将规则: 普通麻将有 ...