Monkey King
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.
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.
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
删除
如果x是单个结点,我们直接改变值即可
如果x有儿子,那么我们需要merge一下x的左右儿子
注意这里fa的维护,因为我们需要把x删除,所以fa[x]=x
而merge得到的根t:fa[t]=t
if (!ch[x][0]&&!ch[x][1])
{
val[x]/=2;
return x;
}
int t=merge(ch[x][0],ch[x][1]);
fa[t]=t;
fa[x]=x;
ch[x][0]=ch[x][1]=0; val[x]/=2;
插入
这个相对简单一点,只要把两部分merge一下,修改一下fa即可
int t=merge(f1,f2); //注意merge的一定是根节点
fa[f1]=fa[f2]=t;
printf("%d\n",val[t]);
tip
一旦涉及根结点的信息,我们都需要调用find函数
//这里写代码片
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int N=100010;
int val[N],fa[N],ch[N][2],dis[N];
int n,m;
int find(int x)
{
if (fa[x]!=x) fa[x]=find(fa[x]);
return fa[x];
}
int merge(int x,int y)
{
if (!x) return y;
if (!y) return x;
if (val[x]<val[y]) swap(x,y);
ch[x][1]=merge(ch[x][1],y);
if (dis[ch[x][1]]>dis[ch[x][0]]) swap(ch[x][0],ch[x][1]);
dis[x]=dis[ch[x][1]]+1;
return x;
}
int solve(int x)
{
if (!ch[x][0]&&!ch[x][1])
{
val[x]/=2;
return x;
}
int t=merge(ch[x][0],ch[x][1]);
fa[t]=t;
fa[x]=x;
ch[x][0]=ch[x][1]=0; val[x]/=2;
int t1=merge(t,x);
fa[x]=fa[t]=t1;
return t1;
}
int main()
{
while (scanf("%d",&n)!=EOF)
{
memset(dis,0,sizeof(dis));
memset(val,0,sizeof(val));
memset(ch,0,sizeof(ch));
for (int i=1;i<=n;i++)
{
scanf("%d",&val[i]);
fa[i]=i;
}
scanf("%d",&m);
int x,y;
for (int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
int f1=find(x),f2=find(y);
if (f1==f2) {
printf("-1\n");
continue;
}
f1=solve(f1); f2=solve(f2);
int t=merge(f1,f2);
fa[f1]=fa[f2]=t;
printf("%d\n",val[t]);
}
}
return 0;
}