可合并优先队列 HDU 1512 Monkey King

Monkey King

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3001    Accepted Submission(s): 1284


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.
 


 

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
 


 

Author

 

JIANG, Yanyan
 


 

Source

 

 

 

题意:有n个猴子,刚开始的时候,猴子们之间互不相识。然后如果a和b发生冲突,a和b会叫自己所认识的猴子中最厉害的猴子出来单挑,单挑完后,最厉害的这两个猴子的实力变为原来的一半,双方所有的人从今以后都互相认识。然后就是给出询问a和b如果发生冲突,合并之后,这群猴子实力最大是多少。

 

思路:不好说,就是套个左偏树吧。左偏树能实现堆的功能,而且还能支持logn的两个左偏树的合并操作。还要用并查集记录自己所在群体的实力最强的猴子编号。。。但是我觉得这里并查集的优势貌似没有发挥出来。。。因为我这样写感觉并不是常数级别的。

 

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
#define LL long long 
const int maxn = 100000 + 5;
int n, m;
inline void swap(int&a, int&b) { int c = a; a = b; b = c; }
int key[maxn];
int lson[maxn], rson[maxn];
int dist[maxn];
int p[maxn];

int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); }

void input()
{
	for (int i = 1; i <= n; ++i) scanf("%d", key + i);
}



int merge(int a, int b)
{
	if (a == 0) return b;
	else if (b == 0) return a;
	if (key[a] < key[b]) swap(a, b);
	rson[a] = merge(rson[a], b);
	p[rson[a]] = p[lson[a]] = a;
	if (dist[rson[a]] > dist[lson[a]]) swap(rson[a], lson[a]);
	if (rson[a] == 0) dist[a] = 0;
	else dist[a] = dist[rson[a]] + 1;
	return a;
}

int pop(int rt)
{
	int ret=merge(lson[rt], rson[rt]);
	p[lson[rt]] = p[rson[rt]] = ret;
	lson[rt] = rson[rt] = 0;
	return ret;
}

void solve()
{
	for (int i = 1; i <= n; ++i) p[i] = i;
	memset(lson, 0, sizeof(lson));
	memset(rson, 0, sizeof(rson));
	memset(dist, 0, sizeof(dist));
	scanf("%d", &m);
	while (m--) {
		int x, y; scanf("%d%d", &x,&y);
		x = find(x), y = find(y);
		if (x == y) {
			printf("-1\n");
			continue;
		}
		key[x] >>= 1, key[y] >>= 1;
		int rt_x = pop(x), rt_y = pop(y);
		x = merge(rt_x, x), y = merge(rt_y, y);
		int king = merge(x, y);
		printf("%d\n", key[king]);
	}
}

int main()
{
	while (scanf("%d", &n) == 1) {
		input();
		solve();
	}
}


 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值