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. 代码自带大常数,比启发式合并都慢 [ ...
随机推荐
- HttpSession具体解释
session的机制 http是无状态的协议,客户每次读取web页面时,server都打开新的会话,并且server也不会自己主动维护客户的上下文信息,那么要怎么才干实现会话跟踪呢?session就是 ...
- STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())
一:起因 (1)STL中关于二分查找的函数有三个:lower_bound .upper_bound .binary_search -- 这三个函数都运用于有序区间(当然这也是运用二分查找的前提),以 ...
- [面经] 南京SAP面试(上)
背景 博主乃985弱校的小硕一枚,在南京某外企工作了两年,如今的公司还不错,待遇还行,做的东西也比較有意思.在南京这个地方,给力的公司不太多,仅仅要是跟亲戚朋友聊到我在南京做IT,无一例外都会问&qu ...
- flex网上办(苹果)桌面系统仿真
1.有登录界面 2.能够载入app(每一个app是单独的swf),并可拖动app的图标互相叠加 3.桌面上显示的哪些APP与目录是依据登陆的用户信息.从webservice中读取的(名字.图标信息等) ...
- Web Api集成Swagger
WebApi集成Swagger 1.新建一个WebApi空项目 2.新建一个Person实体类: public class Person { public int ID { get; set; } p ...
- 【BASH】自己主动清理rman脚本备份文件
************************************************************************ ****原文:blog.csdn.net/clark_ ...
- https原理及tomcat配置https方法
一. 什么是HTTPS 在说HTTPS之前先说说什么是HTTP,HTTP就是我们平时浏览网页时候使用的一种协议.HTTP协议传输的数据都是未加密的,也就是明文的,因此使用HTTP协议传输隐私信息非常不 ...
- hdu1260(dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1260 分析:简单dp,dp[i]=min(dp[i-1]+a[i],dp[i-2]); #includ ...
- hdu3664(递推dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3664 分析:dp[i][j]表示i个数的排列中E值为j的个数.假设现在已有一个E值为j的i的排列,对于 ...
- Urban Dictionary: psd
Urban Dictionary: psd psd Share on twitter Share on facebook Share on more 3 up, 1 down It means Poo ...