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
8
5
5
-1
10
题目大意:刚开始的时候每两个猴子都是不认识的,但是他们打一架以后就认识了,并且随着认识的猴数增多,两个猴子争吵,打架的却是他们认识的最强壮的猴子。
因为每次都是最强壮的,所以我们容易想到建一个堆,每次都是堆顶的猴子打架。然后将堆合并,并且将堆顶猴子的强壮值修改后再加入到堆中。虽然好像直接使用优先队列也可以,这里使用的是新学的左偏树。
为了判断两个猴子是否认识,就需要并查集。注意合并并查集的时候操作的细节。最主要的是修改最顶部的并查集的值,这样其他节点访问的时候才不会出错。还需要用到路径压缩。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<climits>
#include<algorithm>
#include<ctime>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<cmath>
using namespace std;
const int MAXN=1e5+5;
int n,m;
int strong[MAXN];
int ls[MAXN],rs[MAXN],root[MAXN],dis[MAXN];
int Merge(int x,int y)
{
if(!x || !y) return x+y;
if(strong[x]<strong[y]) swap(x,y);
rs[x]=Merge(rs[x],y);
root[rs[x]]=x;
if(dis[rs[x]]>dis[ls[x]]) swap(ls[x],rs[x]);
dis[x]=dis[rs[x]]+1;
return x;
}
int find(int x)
{
int rt=x;
while(rt!=root[rt])
{
rt=root[rt];
}
int father=root[x];
while(father!=rt)
{
root[x]=rt;
x=father; father=root[x];
}
return rt;
}
int main()
{
while(~scanf("%d",&n))
{
memset(ls,0,sizeof(ls));
memset(rs,0,sizeof(rs));
memset(dis,0,sizeof(dis));
for(int i=1;i<=n;i++)
{
scanf("%d",&strong[i]);
root[i]=i;
}
int u,v;
scanf("%d",&m);
while(m--)
{
scanf("%d%d",&u,&v);
int x1=find(u),x2=find(v);
if(x1==x2) //认识,不会发生冲突
{
printf("-1\n");
continue;
}
//不认识,先把两个堆里面最大的猴子打架,其他的合并
strong[x1]/=2; strong[x2]/=2;
int root1=Merge(ls[x1],rs[x1]);
int root2=Merge(ls[x2],rs[x2]);
ls[x1]=0; rs[x1]=0; ls[x2]=0; rs[x2]=0;dis[x1]=dis[x2]=0;
root1=Merge(root1,x1);
root2=Merge(root2,x2);
int rt=Merge(root1,root2);
root[x1]=root[x2]=rt;
root[rt]=rt;
printf("%d\n",strong[rt]);
}
}
return 0;
}