Monkey King
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3423 Accepted Submission(s): 1479
Problem Description
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.
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
Author
JIANG, Yanyan
Source
ZOJ 3rd Anniversary Contest
Recommend
linle
左偏树+并查集。
百度文库对左偏树的讲解
Mato博客截图:
这道题判断是否输出-1,用并查集即可。
对于战斗力减半,先删除原来的,再把减半后的点与原树合并。
合并就是左偏树的基本操作,合并两个堆即可。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define M 100005
using namespace std;
int fa[M],n,m;
struct tree
{
int l,r,dis,v;
}t[M];
int Getfather(int x)
{
if (x==fa[x]) return x;
return fa[x]=Getfather(fa[x]);
}
int Merge(int x,int y)
{
if (x==0) return y;
if (y==0) return x;
if (t[x].v<t[y].v)
swap(x,y);
t[x].r=Merge(t[x].r,y);
int l=t[x].l,r=t[x].r;
fa[r]=x;
if (t[l].dis<t[r].dis)
swap(t[x].l,t[x].r);
if (!t[x].r)
t[x].dis=0;
else t[x].dis=t[t[x].r].dis+1;
return x;
}
int Del(int x)
{
int l,r;
l=t[x].l,r=t[x].r;
fa[l]=l,fa[r]=r;
t[x].l=t[x].r=t[x].dis=0;
return Merge(l,r);
}
void Solve(int x,int y)
{
t[x].v/=2,t[y].v/=2;
int le,ri;
le=Del(x),ri=Del(y);
le=Merge(le,x),ri=Merge(ri,y);
le=Merge(le,ri);
printf("%d\n",t[le].v);
}
int main()
{
while (scanf("%d",&n)!=EOF)
{
for (int i=1;i<=n;i++)
{
scanf("%d",&t[i].v);
t[i].l=t[i].r=t[i].dis=0;
fa[i]=i;
}
scanf("%d",&m);
for (int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
int fx=Getfather(x),fy=Getfather(y);
if (fx==fy)
puts("-1");
else
Solve(fx,fy);
}
}
return 0;
}