[Monkey King]
题目描述
输入
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 strongness value of the strongest monkey in all friends of them after the duel.
样例输入
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5
样例输出
5
5
-1
10
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
const int N=;
int n;int fa[N];
struct node{
int x,dis;
node *l,*r;
int ldis(){return l?l->dis:;}
int rdis(){return r?r->dis:;}
}a[N];
int gi()
{
int str=;char ch=getchar();
while(ch>'' || ch<'')ch=getchar();
while(ch>='' && ch<='')str=str*+ch-'',ch=getchar();
return str;
}
int find(int x){
return x==fa[x]?x:fa[x]=find(fa[x]);
}
node *root[N],*pos=a;
void newnode(node *&p,int key)
{
p=pos++;
p->l=p->r=NULL;
p->dis=;p->x=key;
}
node *merge(node *p,node *q)
{
if(!p || !q)return p?p:q;
if(p->x<q->x)swap(p,q);
p->r=merge(p->r,q);
if(p->ldis()<p->rdis())swap(p->l,p->r);
p->dis=p->rdis()+;
return p;
}
void Delet(int t)
{
node *R=root[t]->r;
node *L=root[t]->l;
node *rt=root[t];
rt->dis=;rt->l=rt->r=NULL;rt->x>>=;
root[t]=merge(R,L);
root[t]=merge(root[t],rt);
}
void work()
{
int x;
for(int i=;i<=n;i++)
{
fa[i]=i;x=gi();
newnode(root[i],x);
}
int m=gi(),y;
while(m--)
{
x=gi();y=gi();
if(find(x)==find(y)){
printf("-1\n");
continue;
}
else{
int xx=find(x),yy=find(y);
Delet(xx);Delet(yy);
fa[yy]=xx;
root[xx]=merge(root[xx],root[yy]);
printf("%d\n",root[xx]->x);
}
}
return ;
}
int main()
{
while(~scanf("%d",&n)){
work();
pos=a;
}
return ;
}
[Monkey King]的更多相关文章
- ZOJ 2334 Monkey King
并查集+左偏树.....合并的时候用左偏树,合并结束后吧父结点全部定成树的根节点,保证任意两个猴子都可以通过Find找到最厉害的猴子 Monkey King ...
- 数据结构(左偏树):HDU 1512 Monkey King
Monkey King Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- P1456 Monkey King
题目地址:P1456 Monkey King 一道挺模板的左偏树题 不会左偏树?看论文打模板,完了之后再回来吧 然后你发现看完论文打完模板之后就可以A掉这道题不用回来了 细节见代码 #include ...
- HDU - 5201 :The Monkey King (组合数 & 容斥)
As everyone known, The Monkey King is Son Goku. He and his offspring live in Mountain of Flowers and ...
- Monkey King(左偏树 可并堆)
我们知道如果要我们给一个序列排序,按照某种大小顺序关系,我们很容易想到优先队列,的确很方便,但是优先队列也有解决不了的问题,当题目要求你把两个优先队列合并的时候,这就实现不了了 优先队列只有插入 删除 ...
- 1512 Monkey King
Monkey King Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- The Monkey King(hdu5201)
The Monkey King Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- hdu1512 Monkey King
Problem Description Once in a forest, there lived N aggressive monkeys. At the beginning, they each ...
- hdu1512 Monkey King(左偏树 + 并查集)
Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its o ...
随机推荐
- Timestamp解析0000-00-00 00:00:00报格式错误
mysql中存储的是Timestamp类型的0000-00-00 00:00:00, 但是在java程序中使用 Timestamp.valueOf("0000-00-00 00:00:00& ...
- gzip 与 gunzip 语法与示例
gzip 与 gunzip 语法与示例 语法: gunzip -c 被压缩的文件 > 已解压的文件示例: 将 catalina.out.gz 文件解压到 catalina.out 文件中: gu ...
- 分离数据库时出错:无法对数据库'XXX' 执行删除,因为它正用于复制"的解决方法
出现的原因是要分离的数据库是一个发布订阅的数据库.因为正在复制,所以无法脱机. 解决办法是停止发布订阅,或者删掉它..再分离.有部分情况是在复制目录下并没有看到发布订阅. 有可能是因为以前建立发布订阅 ...
- 关于连接数据库的T-SQL语句中的一种小技巧
(编程生活中,我们经常会用到数据库.然后在通过T-SQL语句来对数据库进行操作的时候,遇到很多麻烦.话说昨天我就被困扰了一天.明明这个T-sql插数据的语句放在数据库运行的时候没有问题,到了java代 ...
- Android中查看布局文件中的控件(view,id)在哪里被调用(使用)
在阅读别人的代码时通常是很痛苦的,有时很想要看一看布局中的控件在哪里被调用了,为之很苦恼 在这里提供一种方法. 复制要查看的控件ID,到R文件中搜索到该ID, 接下来就好办的了,选中ID按下C ...
- Arrays工具类的实用功能
- iOS开发之UIDevice通知
UIDevice类提供了一个单例对象,它代表着设备,通过它可以获得一些设备相关的信息,比如电池电量值(batteryLevel).电池状态(batteryState).设备的类型(model,比如iP ...
- strspn 和strcspn
1.strcspn头文件:#inclued<string.h>定义函数:size_t strcspn(const char *s, const char * reject);函数说明:st ...
- libcrypto.so.1.0.0内容丢失导致sshd无法运行解决方案
libcrypto.so.10丢失导致sshd无法运行解决方案 服务器由于掉电开启系统后无法远程ssh,重启sshd服务弹如下图的错误: /usr/sbin/sshd:error while load ...
- sub,dl,dt,排版,横向滚动条,浮动元素居中,box-sizing
1.sub标签 下标 2.dl,dt,dd用的地方通常是具有标题,而标题下对应有若干列表简单的(栏目标题+对应标题列表)和标题对应下面有内容.在使用时候我们能简洁html代码情况下,学会灵活使用dl ...