原题链接:https://www.luogu.org/problemnew/show/P1456
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.
一开始有n只孤独的猴子,然后他们要打m次架,每次打架呢,都会拉上自己朋友最牛叉的出来跟别人打,打完之后战斗力就会减半,每次打完架就会成为朋友(正所谓不打不相识o(∩_∩)o )。问每次打完架之后那俩猴子最牛叉的朋友战斗力还有多少,若朋友打架就输出-1.
输入输出格式
输入格式:
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 strength value of the strongest monkey among all of its friends after the duel.
输入输出样例
输入样例#1:
5
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5
输出样例#1:
8
5
5
-1
10
说明
题目可能有多组数据
题解
只需要几颗左偏树即可AC,每次提取出两个大根堆顶的元素除以二再放回去,然后合并两个堆,输出堆顶。
代码
#include<bits/stdc++.h>
using namespace std;
const int M=100005;
struct node{
int val,son[2],dis,dad;
};
node tree[M];
int n,m;
int merge(int a,int b)
{
if(!a||!b)return a+b;
if(tree[a].val<tree[b].val)swap(a,b);
tree[a].son[1]=merge(tree[a].son[1],b);
tree[tree[a].son[1]].dad=a;
if(tree[tree[a].son[0]].dis<tree[tree[a].son[1]].dis)swap(tree[a].son[0],tree[a].son[1]);
if(!tree[a].son[1])tree[a].dis=0;
else tree[a].dis=tree[tree[a].son[1]].dis+1;
return a;
}
int pop(int x)
{
int le=tree[x].son[0],ri=tree[x].son[1];
tree[le].dad=le;
tree[ri].dad=ri;
tree[x].son[0]=tree[x].son[1]=tree[x].dis=0;
return merge(le,ri);
}
int root(int x)
{
while(tree[x].dad!=x)x=tree[x].dad;
return x;
}
void in()
{
memset(tree,0,sizeof(tree));
for(int i=1;i<=n;++i)
scanf("%d",&tree[i].val),tree[i].dad=i;
}
void ac()
{
scanf("%d",&m);
int a,b,t1,t2;
for(int i=1;i<=m;++i)
{
scanf("%d%d",&a,&b);
a=root(a);b=root(b);
if(a==b)
{
printf("-1\n");
continue;
}
tree[a].val/=2;tree[b].val/=2;
t1=pop(a);t2=pop(b);
a=merge(a,t1);b=merge(b,t2);
printf("%d\n",tree[merge(a,b)].val);
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
in(),ac();
return 0;
}
600



被折叠的 条评论
为什么被折叠?



