https://www.luogu.org/problemnew/show/1456

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<algorithm>
  4.  
  5. using namespace std;
  6.  
  7. #define N 100001
  8.  
  9. struct node
  10. {
  11. int lc,rc;
  12. int key,dis;
  13. }e[N];
  14.  
  15. int fa[N];
  16.  
  17. void read(int &x)
  18. {
  19. x=; char c=getchar();
  20. while(!isdigit(c)) c=getchar();
  21. while(isdigit(c)) { x=x*+c-''; c=getchar(); }
  22. }
  23.  
  24. int find(int i) { return fa[i]==i ? i : fa[i]=find(fa[i]); }
  25.  
  26. int merge(int a,int b)
  27. {
  28. if(!a) return b;
  29. if(!b) return a;
  30. if(e[a].key<e[b].key) swap(a,b);
  31. e[a].rc=merge(e[a].rc,b);
  32. if(e[e[a].lc].dis<e[e[a].rc].dis) swap(e[a].lc,e[a].rc);
  33. if(!e[a].rc) e[a].dis=;
  34. else e[a].dis=e[e[a].rc].dis+;
  35. return a;
  36. }
  37.  
  38. int del(int x)
  39. {
  40. int lc=e[x].lc,rc=e[x].rc;
  41. e[x].dis=e[x].lc=e[x].rc=;
  42. return fa[lc]=fa[rc]=merge(lc,rc);
  43. }
  44.  
  45. int main()
  46. {
  47. int n,m;
  48. int x,y,u,v,f,g,h;
  49. while(scanf("%d",&n)!=EOF)
  50. {
  51. for(int i=;i<=n;++i)
  52. {
  53. read(e[i].key);
  54. fa[i]=i;
  55. e[i].dis=e[i].lc=e[i].rc=;
  56. }
  57. read(m);
  58. while(m--)
  59. {
  60. read(x);
  61. read(y);
  62. x=find(x);
  63. y=find(y);
  64. if(x==y)
  65. {
  66. puts("-1");
  67. continue;
  68. }
  69. e[x].key>>=;
  70. e[y].key>>=;
  71.  
  72. u=del(x);
  73. v=del(y);
  74. g=merge(u,v);
  75. fa[u]=fa[v]=g;
  76.  
  77. f=merge(x,y);
  78. fa[x]=fa[y]=f;
  79.  
  80. h=merge(g,f);
  81. fa[g]=fa[f]=h;
  82. cout<<e[h].key<<'\n';
  83. }
  84. }
  85. }

题目描述

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.

输入输出样例

输入样例#1: 复制

  1. 5
  2. 20
  3. 16
  4. 10
  5. 10
  6. 4
  7. 5
  8. 2 3
  9. 3 4
  10. 3 5
  11. 4 5
  12. 1 5
输出样例#1: 复制

  1. 8
  2. 5
  3. 5
  4. -1
  5. 10

洛谷P1456 Monkey King的更多相关文章

  1. P1456 Monkey King

    题目地址:P1456 Monkey King 一道挺模板的左偏树题 不会左偏树?看论文打模板,完了之后再回来吧 然后你发现看完论文打完模板之后就可以A掉这道题不用回来了 细节见代码 #include ...

  2. 【luogu P1456 Monkey King】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1456 左偏树并查集不加路径压缩吧... #include <cstdio> #include & ...

  3. 洛谷P1456Monkey King

    洛谷P1456 Monkey King 题目描述 Once in a forest, there lived N aggressive monkeys. At the beginning, they ...

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

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

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

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

  6. POJ ???? Monkey King

      题目描述 Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things i ...

  7. 洛谷 P2504 [HAOI2006]聪明的猴子

    洛谷 P2504 [HAOI2006]聪明的猴子 题目描述 在一个热带雨林中生存着一群猴子,它们以树上的果子为生.昨天下了一场大雨,现在雨过天晴,但整个雨林的地表还是被大水淹没着,部分植物的树冠露在水 ...

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

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

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

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

随机推荐

  1. 结对作业(1.0版)(bug1已修复)

    import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing ...

  2. HighCharts点击柱形或饼块等加URL或Click事件

    我们在做图表的时候,有时候需要在单个数据上加链接或点击事件,是在plotOptions里的events里设置的: 如下代码: plotOptions: { pie: { cursor: 'pointe ...

  3. linux创建账户并自动生成主目录和主目录下的文件

    # useradd -d /home/test -m test; 然后给test设置密码. # passwd test; 1. useradd 添加用户或更新新创建用户的默认信息 语法:useradd ...

  4. TP5 助手函数与TP3.2单字母函数

    一.TP5 助手函数 助手函数 描述 abort 中断执行并发送HTTP状态码 action 调用控制器类的操作 cache 缓存管理 config 获取和设置配置参数 controller 实例化控 ...

  5. Implement Trie and find longest prefix string list

    package leetcode; import java.util.ArrayList; import java.util.List; class TrieNode{ Boolean isWord; ...

  6. BZOJ5302 HAOI2018奇怪的背包(动态规划)

    由裴蜀定理,子集S有解当且仅当gcd(S,P)|w. 一个显然的dp是设f[i][j]为前i个数gcd为j的选取方案.注意到这里的gcd一定是P的约数,所以状态数是n√P的.然后可以通过这个得到gcd ...

  7. BZOJ3267/3272 KC采花/Zgg吃东西(线段树)

    直接维护选k个子段时的最优解似乎也可以做,然而复杂度是O(nk2logn),显然跑不过. 考虑一种费用流做法.序列里每个点拆成入点和出点,源连入汇连出,入点和出点间连流量1费用ai的边,相邻点出点向入 ...

  8. STL 基本概念

    STL 基本概念 STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称.现在是一个C++软件库,也是C++标准程序库的一部分,但在被引入C++之前 ...

  9. 胡小兔的NOIP2017游记【出成绩后更新版】

    胡小兔的NOIP2017游记[出成绩后更新版] 2017.11.22 Update 前几天成绩出来啦,看这篇博客访问量还挺多的,下面就分享一下结果吧: 我的Day1T2和Day2T1两道最水的题都跪了 ...

  10. loj2538 「PKUWC2018」Slay the Spire 【dp】

    题目链接 loj2538 题解 比较明显的是,由于强化牌倍数大于\(1\),肯定是能用强化牌尽量用强化牌 如果强化牌大于等于\(k\),就留一个位给攻击牌 所以我们将两种牌分别排序,企图计算\(F(i ...