Money King【题解】

时间:2023-11-23 21:50:56

我又傻了……竟然忘了区别大根堆和小根堆的性质,以至于一个符号打错,debug了半天……(我真是太菜了……)

题目描述

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.

有多组数据

输入样例:


输出样例:


-

解:

每次打完架成为朋友,告诉了我们要合并;查找两人是不是朋友,告诉我们要用路径压缩并查集;找最大值,告诉了我们要用大根堆。

这题做完了。注意merge的符号,以及多重数据的提醒。

题目中要求把堆顶的值减半。然而不方便直接减,于是我们先把它与它的子树断掉,然后合并它的两颗子树,再把它合并进去即可。

注意合并两个堆,最终要把它们全部合并并且输出堆顶。

Code:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#define ll long long
#define lc tr[x].l
#define rc tr[x].r
using namespace std;
const int MAXN=;
inline void swap(int &x,int &y){x^=y^=x^=y;}
inline int read(){
char ch=getchar();
int s=,w=;while(ch<''||ch>''){if(ch=='-')w=-;ch=getchar();}
while(ch>=''&&ch<=''){s=(s<<)+(s<<)+(ch^);ch=getchar();}return s*w;
}
struct node{
int l,r,dis,w,fa;
}tr[MAXN];
int merge(int x,int y){
if(!x||!y)return x+y;
if(tr[x].w<tr[y].w||(tr[x].w==tr[y].w&&x<y))swap(x,y);
rc=merge(rc,y);tr[rc].fa=tr[lc].fa=x;
if(tr[lc].dis<tr[rc].dis)swap(lc,rc);tr[x].dis=tr[rc].dis+;
return x;
}
inline int getf(int x){return x==tr[x].fa?x:tr[x].fa=getf(tr[x].fa);}
void work(int x,int y){
x=getf(x),y=getf(y);
if(x==y){
puts("-1");
return;
}
int rt,art,brt;
tr[x].w>>=;
rt=merge(tr[x].l,tr[x].r);
tr[x].l=tr[x].r=tr[x].dis=;
art=merge(rt,x);
tr[rt].fa=tr[x].fa=art;
tr[y].w>>=;
rt=merge(tr[y].l,tr[y].r);
tr[tr[y].l].fa=tr[tr[y].r].fa=rt;
tr[y].l=tr[y].r=tr[y].dis=;
brt=merge(rt,y);
tr[rt].fa=tr[y].fa=brt;
rt=merge(art,brt);
tr[art].fa=tr[brt].fa=rt;
printf("%d\n",tr[rt].w);
}int n,m;
int main(){
while(~scanf("%d",&n)){
tr[].dis=-;
for(register int i=;i<=n;i++){
tr[i].fa=i;
tr[i].w=read();
tr[i].dis=tr[i].l=tr[i].r=;
}m=read();
for(register int i=;i<=m;i++)work(read(),read());
}
return ;
}