2013
12-12

Monkey King

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.

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.

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.

5
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5

8
5
5
-1
10

#define N 100010
struct node{
int dis,v;
int l,r;
}k[N];
int n,m,pre[N];
void init(){
int i;
for(i=0;i<=n;i++){
pre[i] = i;
}
memset(k,'\0',sizeof(k));
}
int Merge(int a,int b){//  左偏树合并（重要）！
if(a == 0)return b;
if(b == 0)return a;
if(k[b].v>k[a].v)swap(a,b);
k[a].r = Merge(k[a].r,b);
pre[k[a].r] = a;
if(k[k[a].l].dis < k[k[a].r].dis)swap(k[a].r,k[a].l);
if(k[a].r == 0)k[a].dis = 0;
else k[a].dis = k[k[a].r].dis + 1;
return a;
}
int Max(int a){//  合并左右子树后返回最大结点编号
return Merge(k[a].l,k[a].r);
}
int find(int x){//  寻找前驱
while(x!=pre[x]){
x = pre[x];
}
return x;
}
int solve(int a,int b){
int x = find(a),y = find(b);//寻找各自根结点(这里也就是最大值的结点)
int p,xx,yy,tmp;
pre[a] = x,pre[b] = y;
if(x == y)return -1;//同一棵树

k[x].v /= 2;//修改根结点后要重新合并，具体是先合并两个儿子，再插入自己
tmp = Max(x);//合并两个儿子——————— ①
k[x].r = k[x].l = k[x].dis = 0;//自己脱离出来
xx = Merge(tmp,x);// 合并修改后的节点和上面两个儿子的新树①

k[y].v /= 2;
tmp = Max(y);
k[y].r = k[y].l = k[y].dis = 0;
yy = Merge(tmp,y);

p = Merge(xx,yy);// 合并上面的两棵树

pre[xx] = pre[yy] = pre[x] = pre[y] = pre[a] = pre[b] = p;//更改前驱
return k[p].v;
}
int main(){
int i,j;
while(scanf("%d",&n) != -1){
init();
for(i=1;i<=n;i++){
scanf("%d",&k[i].v);
}
scanf("%d",&m);
while(m--){
int a,b;
scanf("%d%d",&a,&b);
int ans = solve(a,b);
printf("%d\n",ans);
}
}
return 0;
}

1. #include <cstdio>
#include <cstring>

const int MAXSIZE=256;
//char store[MAXSIZE];
char str1[MAXSIZE];
/*
void init(char *store) {
int i;
store['A']=’V', store['B']=’W',store['C']=’X',store['D']=’Y',store['E']=’Z';
for(i=’F';i<=’Z';++i) store =i-5;
}
*/
int main() {
//freopen("input.txt","r",stdin);
//init(store);
char *p;
while(fgets(str1,MAXSIZE,stdin) && strcmp(str1,"STARTn")==0) {
if(p=fgets(str1,MAXSIZE,stdin)) {
for(;*p;++p) {
//*p=store[*p]
if(*p<’A’ || *p>’Z') continue;
if(*p>’E') *p=*p-5;
else *p=*p+21;
}
printf("%s",str1);
}
fgets(str1,MAXSIZE,stdin);
}
return 0;
}

2. 题本身没错，但是HDOJ放题目的时候，前面有个题目解释了什么是XXX定律。
这里直接放了这个题目，肯定没几个人明白是干啥