您的位置:首页 > 其它

hdu 1512 Monkey King 左偏树

2015-05-21 17:51 441 查看
题目链接:HDU - 1512

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 there 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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.

题意描述:有n只猴子,每只猴子有一个值,两只猴子如果打架的话,他们的值各自掉一半。给出m个事件,每个事件给出两只猴子,如果两只猴子不认识的话,打完架就成为了朋友(两只猴子的各自朋友也都互相成为了朋友),求出打完架后两只猴子的所有朋友中值最大的。
算法分析:这道题我刚开始做的时候,想到了并查集,以为这就够了(其实时间复杂度我也不敢直视),交上去果断TLE了,后来看了讨论里有说,尼玛,这就是传说中的左偏树的题目啊,果断得好好学习一下,弥补一下自己的数据结构的知识。
说明:头一次搞左偏树,代码是借鉴别人的,不过真心写的比较好就拿来了。同时,推荐一下集训队的论文,基本上看了论文后就对左偏树有了一定的了解了。

左偏树的特点及其应用

/*左偏树*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
const int maxn = 100000+10;

int father[maxn];
struct node
{
int l,r;
int dis;
int strong;
}LTree[maxn];
int Find(int x)
{
if (father[x]==x) return x;
return father[x]=Find(father[x]);
}
int Merge(int x,int y)
{   //返回合并后的根
if (x==0) return y;
if (y==0) return x;
if (LTree[x].strong < LTree[y].strong)            //大顶堆
swap(x,y);
LTree[x].r = Merge(LTree[x].r,y);            //递归合并右子树和Y
int l = LTree[x].l , r = LTree[x].r;
father[r] = x;                        //更新T右子树的根
if (LTree[l].dis < LTree[r].dis)                //维护堆性质
swap(LTree[x].l,LTree[x].r);
if (LTree[x].r == 0)                    //如果没有右子树 则距离为0
LTree[x].dis = 0;
else
LTree[x].dis = LTree[LTree[x].r].dis + 1;
return x;
}
int del(int x)
{   //返回删除根以后左右子树的合并的根
int l,r;
l=LTree[x].l;
r=LTree[x].r;
father[l]=l;
father[r]=r;
LTree[x].l=LTree[x].r=LTree[x].dis=0;
return Merge(l,r);
}
void solve(int x,int y)
{
LTree[x].strong /= 2;
LTree[y].strong /= 2;
//问每次PK以后,当前这个群体里力量最大的猴子的力量是多少。
int left,right;
left = del(x);
right = del(y);
left = Merge(left,x);
right = Merge(right,y);
left = Merge(left,right);
printf("%d\n",LTree[left].strong);
}
int main()
{
int n,m,x,y;
while (scanf("%d",&n)!=EOF)
{
for (int i=1 ;i<=n ;i++)
{
scanf("%d",<ree[i].strong);
LTree[i].l=0;
LTree[i].r=0;
LTree[i].dis=0;
father[i]=i;            //起始已自己为父亲
}
scanf("%d",&m);
for (int i=1 ;i<=m ;i++)
{
scanf("%d%d",&x,&y);
int fx=Find(x),fy=Find(y);
if (fx == fy) printf("-1\n");
else solve(fx,fy);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: