思路:用并查集即可,每次合并的时候将小的集合合并到大的集合上去。理论上的平均复杂度是n*lgn*lgn。

#include<map>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Maxn 100010
#define LL long long
#define Mod 1000000007
int sz[Maxn],fa[Maxn];
using namespace std;
priority_queue<int> q[Maxn];
void init()
{
for(int i=;i<Maxn;i++){
sz[i]=;
fa[i]=i;
}
}
int find(int x)
{
if(x!=fa[x])
fa[x]=find(fa[x]);
return fa[x];
}
int merg(int a,int b)
{
int x=find(a);
int y=find(b);
if(x==y) return ;
int s1,s2;
s1=q[x].top();
s2=q[y].top();
q[x].pop();
q[y].pop();
s1/=;
s2/=;
if(sz[x]>sz[y]){
q[x].push(s1);
q[x].push(s2);
while(!q[y].empty()){
q[x].push(q[y].top());
q[y].pop();
}
printf("%d\n",q[x].top());
sz[x]+=sz[y];
fa[y]=x;
}else{
q[y].push(s1);
q[y].push(s2);
while(!q[x].empty()){
q[y].push(q[x].top());
q[x].pop();
}
printf("%d\n",q[y].top());
sz[y]+=sz[x];
fa[x]=y;
}
return ;
}
int main()
{
int n,m,i,j;
while(scanf("%d",&n)!=EOF){
init();
for(i=;i<Maxn;i++){
while(!q[i].empty())
q[i].pop();
}
int val;
for(i=;i<=n;i++){
scanf("%d",&val);
q[i].push(val);
}
scanf("%d",&m);
int a,b;
for(i=;i<=m;i++){
scanf("%d%d",&a,&b);
if(!merg(a,b))
printf("-1\n");
}
}
return ;
}

hdu 1512的更多相关文章

  1. hdu 1512 Monkey King 左偏树

    题目链接:HDU - 1512 Once in a forest, there lived N aggressive monkeys. At the beginning, they each does ...

  2. HDU 1512 Monkey King(左偏树+并查集)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1512 [题目大意] 现在有 一群互不认识的猴子,每个猴子有一个能力值,每次选择两个猴子,挑出他们所 ...

  3. HDU 1512 Monkey King(左偏树模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1512 题意: 有n只猴子,每只猴子一开始有个力量值,并且互相不认识,现有每次有两只猴子要决斗,如果认识,就不打了 ...

  4. hdu 1512 Monkey King —— 左偏树

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1512 很简单的左偏树: 但突然对 rt 的关系感到混乱,改了半天才弄对: 注意是多组数据! #includ ...

  5. HDU 1512 Monkey King ——左偏树

    [题目分析] 也是堆+并查集. 比起BZOJ 1455 来说,只是合并的方式麻烦了一点. WA了一天才看到是多组数据. 盲人OI (- ̄▽ ̄)- Best OI. 代码自带大常数,比启发式合并都慢 [ ...

  6. HDU 1512 Monkey King

    左偏树.我是ziliuziliu,我是最强的 #include<iostream> #include<cstdio> #include<cstring> #incl ...

  7. 数据结构(左偏树):HDU 1512 Monkey King

    Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  8. HDU 1512 Monkey King(左偏树)

    Description Once in a forest, there lived N aggressive monkeys. At the beginning, they each does thi ...

  9. HDU 1512 Monkey King (左偏树+并查集)

    题意:在一个森林里住着N(N<=10000)只猴子.在一开始,他们是互不认识的.但是随着时间的推移,猴子们少不了争斗,但那只会发生在互不认识 (认识具有传递性)的两只猴子之间.争斗时,两只猴子都 ...

随机推荐

  1. LOJ#515. 「LibreOJ β Round #2」贪心只能过样例(bitset)

    内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: nzhtl1477 提交提交记录统计讨论测试数据   题目描述 一共有 nnn个数,第 iii ...

  2. 解决Cannot reinitialise DataTable问题 解决dataTables再次调用不能清空数据

    这里我们只需要多设置一个字段 “destroy" : true 即可 或者设置retrieve: true, 或者在加载datatable之前使用$("#example" ...

  3. rsync+lsyncd实现实时同步

    1.接收端安装rsync,修改/etc/rsyncd.conf配置文件,然后启动服务. uid = rootgid = rootuse chroot = nomax connection = 4sec ...

  4. 使用IDEA将本地项目上传到GitHub

    00.首先保证git和github能够使用ssh连接. 01.在GitHub上新建仓库 需要注意的是不要勾选Initialize this repository with a README. 02.在 ...

  5. (转)Clang 比 GCC 编译器好在哪里?

    编译速度更快.编译产出更小.出错提示更友好.尤其是在比较极端的情况下.两年多前曾经写过一个Scheme解释器,词法分析和语法解析部分大约2000行,用的是Boost.Spirit--一个重度依赖C++ ...

  6. 【PHP】nl2br转化输出input框的换行

    在input或者textarea框中输入的换行符保存到数据库是/n,如果直接输出到前端的话是不会有换行的,所以要用到nl2br转化 nl2br($test);

  7. pytorch中torch.nn构建神经网络的不同层的含义

    主要是参考这里,写的很好PyTorch 入门实战(四)--利用Torch.nn构建卷积神经网络 卷积层nn.Con2d() 常用参数 in_channels:输入通道数 out_channels:输出 ...

  8. Reward HDU - 2647

    传送门     Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to dis ...

  9. 使用泛型类简化ibatis系统架构

    jdk1.5的推出为我们带来了枚举.泛型.foreach循环.同步工具包等等好东西.其中,泛型的使用为我们的代码开发提供了很大的简便,简化了我们的代码. 1.设计思路 1)GenericDao泛型类提 ...

  10. 关于原生JS获取class,ID等属性的一些封装

    一.传统上获取是通过document.getElementById获取元素的ID属性,通过总结与学习总结一下获取元素class以及id属性的一些封装; 1.创建构造函数,这里面不需要多解释什么:(主要 ...