ZOJ 2334(Monkey King-左偏树第一题)
Monkey King
Time Limit: 10 Seconds Memory Limit: 32768 KB
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 their 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.
Input
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.
Output
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.
Sample Input
5
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5
Sample Output
8
5
5
-1
10
左偏树第一题。
root[i]表以i为根的并查集的左偏树的当前根
这题在怎么表示[i]所在树之根的问题上纠结了很久,后发现,只要将2样拆开就行了。。。。这样每次修改都是O(1)的
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#include<cmath>
#include<cctype>
#include<cassert>
#include<climits>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define RepD(i,n) for(int i=n;i>=0;i--)
#define MEM(a) memset(a,0,sizeof(a))
#define MEMI(a) memset(a,127,sizeof(a))
#define MEMi(a) memset(a,128,sizeof(a))
#define INF (2139062143)
#define F (1000000009)
#define MAXN (101000+10)
#define MAXM (101000+10)
typedef long long ll;
struct node
{
int v,ch[2],dis;
node():v(0),dis(0){ch[0]=ch[1]=0;}
}q[MAXN];
int father[MAXN],root[MAXN];// root[i]表示以i为根的并查集的左偏树的当前根
int merge(int a,int b)
{
if (a*b==0) return a+b;
if (q[a].v<q[b].v) swap(a,b);
q[a].ch[1]=merge(q[a].ch[1],b);
if (q[q[a].ch[0]].dis<q[q[a].ch[1]].dis) swap(q[a].ch[0],q[a].ch[1]);
if (q[a].ch[1]) q[a].dis=q[q[a].ch[1]].dis+1;else q[a].dis=0;
return a;
}
int pop(int a)
{
int p=merge(q[a].ch[0],q[a].ch[1]);
q[a].dis=q[a].ch[0]=q[a].ch[1]=0;q[a].v/=2;
int x=merge(a,p);
return x;
}
int getfather(int x)
{
if (father[x]==x) return x;
return father[x]=getfather(father[x]);
}
int n,m;
int main()
{
//freopen("zoj2334.in","r",stdin); while(scanf("%d",&n)==1)
{
For(i,n) q[i]=node();
For(i,n) scanf("%d",&q[i].v),father[i]=root[i]=i;
scanf("%d",&m);
For(i,m)
{
// For(i,n) cout<<getfather(i)<<' ';cout<<endl;
int u,v,fu,fv;
scanf("%d%d",&u,&v);
if ((fu=getfather(u))==(fv=getfather(v))) printf("-1\n");
else
{
int ru=root[fu],rv=root[fv];
ru=pop(ru);rv=pop(rv);
ru=merge(ru,rv); printf("%d\n",q[ru].v);
father[fu]=fv;root[fv]=ru;
}
}
// break;
} //while(1);
return 0;
}
ZOJ 2334(Monkey King-左偏树第一题)的更多相关文章
- zoj 2334 Monkey King/左偏树+并查集
原题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1389 大致题意:N只相互不认识的猴子(每只猴子有一个战斗力值) 两只 ...
- hdu 1512 Monkey King 左偏树
题目链接:HDU - 1512 Once in a forest, there lived N aggressive monkeys. At the beginning, they each does ...
- ZOJ2334 Monkey King 左偏树
ZOJ2334 用左偏树实现优先队列最大的好处就是两个队列合并可以在Logn时间内完成 用来维护优先队列森林非常好用. 左偏树代码的核心也是两棵树的合并! 代码有些细节需要注意. #include&l ...
- HDU1512 ZOJ2334 Monkey King 左偏树
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - ZOJ2334 题目传送门 - HDU1512 题意概括 在一个森林里住着N(N<=10000)只猴子. ...
- HDU 1512 Monkey King (左偏树+并查集)
题意:在一个森林里住着N(N<=10000)只猴子.在一开始,他们是互不认识的.但是随着时间的推移,猴子们少不了争斗,但那只会发生在互不认识 (认识具有传递性)的两只猴子之间.争斗时,两只猴子都 ...
- hdu 1512 Monkey King —— 左偏树
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1512 很简单的左偏树: 但突然对 rt 的关系感到混乱,改了半天才弄对: 注意是多组数据! #includ ...
- hdu1512 Monkey King(左偏树 + 并查集)
Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its o ...
- LuoguP1456 Monkey King (左偏树)
struct LeftTree{ int l,r,val,dis; }t[N]; int fa[N]; inline int Find(int x){ return x == fa[x] ? x : ...
- HDU 1512 Monkey King ——左偏树
[题目分析] 也是堆+并查集. 比起BZOJ 1455 来说,只是合并的方式麻烦了一点. WA了一天才看到是多组数据. 盲人OI (- ̄▽ ̄)- Best OI. 代码自带大常数,比启发式合并都慢 [ ...
随机推荐
- OCX控件在IE中无法侦测到键盘消息( MFC ActiveX Control in IE Doesn't Detect Keystrokes)
症状描述: Accelerator keys, such as ARROW keys, are first received by the message pump of the ActiveX co ...
- Swift - 使用EventKit获取系统日历事件,添加事件
通过EventKit可以对iOS日历事件进行读取,添加等操作.但网上找到的都是使用Objective-C来编写的. 下面提供一个Swift版的样例,演示如何添加一个事件以及获取所有的事件列表. 1 2 ...
- cocos2dX 事件之触摸事件和触摸事件集合
今天, 我们来学习cocos2dX里面的触摸事件与触摸事件合集, 如今的手机游戏交互基本上都是通过触摸交互的, 所以大家明确这节的重要性了吧, 本节篇幅比較大, 所以我就不扯闲话了 先来看看经常使用函 ...
- MySQL如何修改root密码
MySQL修改用户密码 因为长期不登录MySQL数据库,登录时经常忘记root权限密码.本文提供一个在数据库服务器上修改root密码的方法,本文撰写基础是在xp操作系统下进行. 第一步 ...
- Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException
1.错误描写叙述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -he ...
- C语言中scanf/fscanf 的%[]和%n说明符的使用方法
标准输入输出函数%[]和%n说明符的使用方法 scanf fscanf,均从第一个非空格的可显示字符开始读起! 标准输入输出函数scanf具有相对较多的转换说明符,它常常作为入 ...
- MySQLdb的安装与使用
一.安装 安装已编译版本号(此方法简便快捷): http://www.codegood.com/downloads 依据自己系统下载,双击安装,搞定 然后import MySQLdb.查看是否成功 我 ...
- JavaScript 中的事件类型5(读书笔记思维导图)
Web 浏览器中可能发生的事件有很多类型.如前所述,不同的事件类型具有不同的信息,而“ DOM3级事件”规定了以下几类事件. UI(User Interface,用户界面)事件:当用户与页面上的元素交 ...
- Java使用Socket传输文件遇到的问题(转)
1.写了一个socket传输文件的程序,发现传输过去文件有问题.找了一下午终于似乎找到了原因,记录下来警示一下: 接受文件的一端,向本地写文件之前使用Thread.sleep(time)休息一下就解决 ...
- android--手机桌面添加网址链接图标(解决方式)
这样的做法最普遍最简单: 1.新建一个android空项目: 2.在drawable文件夹下加入图标文件,如icon.png:在values文件夹下的strings.xml文件里添加名称.如websi ...