洛谷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炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...
随机推荐
- Linux上两种网络连接方式
模式一:NAT方式好处:路由器更换,或者交换机更换,网络仍然可以使用,所用使用最多 准备工作: 查看VMware服务器启动情况,五个全开模式 vmnet8开启模式 1 配置VMware交换机的ip地址 ...
- [转帖]CPU 的缓存
缓存这个词想必大家都听过,其实缓存的意义很广泛:电脑整机最大的缓存可以体现为内存条.显卡上的显存就是显卡芯片所需要用到的缓存.硬盘上也有相对应的缓存.CPU有着最快的缓存(L1.L2.L3缓存等),缓 ...
- BZOJ5105 CodePlus2017晨跑
这个题???我WA了两发??? #include<iostream> #include<cstdio> #include<cmath> #include<cs ...
- linux eclipse add desktop shortcut with root permission
gedit ~/.local/share/applications/opt_eclipse.desktop sudo apt-get install gksu [Desktop Entry] Type ...
- 【刷题】HDU 3435 A new Graph Game
Problem Description An undirected graph is a graph in which the nodes are connected by undirected ar ...
- 【BZOJ1864】三色二叉树(动态规划)
[BZOJ1864]三色二叉树(动态规划) 题面 BZOJ 题解 首先把树给构出来. 设\(f[i][0/1]\)表示当前节点\(i\),是否是绿色节点的子树中最大/最小的绿色节点的个数和. 转移很显 ...
- BZOJ1061 [Noi2008]志愿者招募 【单纯形】
题目链接 BZOJ1061 题解 今天终于用正宗的线性规划\(A\)了这道题 题目可以看做有\(N\)个限制和\(M\)个变量 变量\(x_i\)表示第\(i\)种志愿者的人数,对于第\(i\)种志愿 ...
- loj2540 「PKUWC2018」随机算法 【状压dp】
题目链接 loj2540 题解 有一个朴素三进制状压\(dp\),考虑当前点三种状态:没考虑过,被选入集合,被排除 就有了\(O(n3^{n})\)的转移 但这样不优,我们考虑优化状态 设\(f[i] ...
- 洛谷 P1878 舞蹈课 解题报告
P1878 舞蹈课 题目描述 有\(n\)个人参加一个舞蹈课.每个人的舞蹈技术由整数来决定.在舞蹈课的开始,他们从左到右站成一排.当这一排中至少有一对相邻的异性时,舞蹈技术相差最小的那一对会出列并开始 ...
- HDU.1850 being a good boy in spring festival (博弈论 尼姆博弈)
HDU.1850 Being a Good Boy in Spring Festival (博弈论 尼姆博弈) 题意分析 简单的nim 博弈 博弈论快速入门 代码总览 #include <bit ...