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. 代码自带大常数,比启发式合并都慢 [ ...
随机推荐
- 14.2.5.6 Adaptive Hash Indexes 自适应Hash Indexes
14.2.5.6 Adaptive Hash Indexes 自适应Hash Indexes adaptive hash index(AHI) 让InnoDB 执行更加像在一个内存数据库里在, 在不牺 ...
- io端口
io端口 *********************************************************** io端口设备访问流程为 --------------------- ...
- 程序猿的量化交易之路(29)--Cointrader之Tick实体(16)
转载需注明出处:http://blog.csdn.net/minimicall,http://cloudtrade.top Tick:什么是Tick,在交易平台中很常见,事实上就 单笔交易时某仅仅证券 ...
- [Android]mac下开发环境搭建
好像没神马好些的? 1.下载adt-bundle-mac-x86_64bit(http://developer.android.com/sdk/installing/bundle.html) 2.解压 ...
- Cocos2d-x 3.1.1 学习日志8--2分钟让你知道cocos2d-x3.1.1 文本类别
实际上文本经常使用的三个,LabelTTF,LabelBMF和LabelAtlas.而他们使用非常相似.所以,你会只举一反三,非常快就能够掌握了. <span style="font- ...
- The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar
出现 The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the j ...
- Opencv246+vs2012生成不依赖编译环境的exe文件
我们都知道,vs2012编译项目有两个版本号:Debug和Release,这里我们在Release下生成exe文件,为什么要在Release以下生成呢,原因是你在Debug模式下生成的exe须要vs2 ...
- .net Mvc文件下载的功能,大文件下载完成之后修改数据库功能
原文:.net Mvc文件下载的功能,大文件下载完成之后修改数据库功能 我服务器上文件只能下载一次,下载了之后就不能下载了,大文件或网速不好时,可能服务端文件流发送完了,客户端还没下载完,导致下载失败 ...
- Android 调用谷歌语音识别
調用谷歌语音识别其实很简单,直接利用 intent 跳转到手机里面的谷歌搜索 代码也很简单,直接调用方法 startVoiceRecognitionActivity() 如果大家手机里面没有谷歌搜索, ...
- hdu 4888 Redraw Beautiful Drawings 最大流
好难好难,将行列当成X和Y,源汇点连接各自的X,Y集,容量为行列的和,相当于从源点流向每一行,然后分配流量给每一列,最后流入汇点,这样执意要推断最后是否满流,就知道有没有解,而解就是每一行流向每一列多 ...