Monkey King ZOJ - 2334
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
左偏树模板题
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include<string.h>
#include<math.h>
#include<list>
using namespace std;
#define ll long long
#define pii pair<int,int>
const int inf = 1e9 + 7;
const int N = 1e5+5;
inline int read(){
int x;
char ch;
while(!isdigit(ch=getchar()));
x=ch-'0';
while(isdigit(ch=getchar())){
x=x*10+ch-'0';
}
return x;
}
struct LT{
const static int maxn=N;
int tot=0,v[maxn],l[maxn],r[maxn],d[maxn],fa[maxn];
int merge(int x,int y){
if(!x){
fa[x]=y;
return y;
}
if(!y){
fa[y]=x;
return x;
}
if(v[x]<v[y]){
swap(x,y);
}
r[x]=merge(r[x],y);
fa[r[x]]=x;
if(d[l[x]]<d[r[x]]){
swap(l[x],r[x]);
}
d[x]=d[r[x]]+1;
return x;
}
int init(int x){//初始化一个权值为x的点
tot++;
v[tot]=x;
l[tot]=r[tot]=d[tot]=0;
fa[tot]=-1;
return tot;
}/*
int insert(int x,int y){//往第x个左偏树插入一个权值为y的点
return merge(x,init(y));
}*/
int top(int x){
return v[x];
}
int pop(int x){
int root=merge(l[x],r[x]);
fa[root]=-1;
l[x]=r[x]=0;
return root;
}
int root(int x){
while(fa[x]!=-1){
x=fa[x];
}
return x;
}
}lt;
int main()
{
//freopen("/home/lu/Documents/r.txt","r",stdin);
//freopen("/home/lu/Documents/w.txt","w",stdout);
int n,m;
while(~scanf("%d",&n)){
lt.tot=0;
for(int i=0;i<n;++i){
int x;
scanf("%d",&x);
lt.init(x);
}
scanf("%d",&m);
while(m--){
int x,y;
scanf("%d%d",&x,&y);
x=lt.root(x);
y=lt.root(y);
if(x==y){
puts("-1");
}
else{
int ans=max(lt.v[x],lt.v[y])/2;
printf("%d\n",ans);
int k=lt.merge(x,y);
int nk=lt.pop(k);
lt.v[k]/=2;
lt.merge(nk,k);
}
}
}
return 0;
}