[HDU 1512] Monkey King【左偏树】

  • 2018-03-03
  • 0
  • 0

Problem:

Time Limit: 10000/5000 MS (Java/Others)

Memory Limit: 65536/32768 K (Java/Others)

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

ZOJ 3rd Anniversary Contest

Recommend

linle   |   We have carefully selected several similar problems for you:  1509 1710 1504 1510 1500

Solution:

本题是一道简单的左偏树练习题。

需要实现求最大值修改最大值以及合并集合三个操作。

第一个可以用来快速维护,第二个可以看作弹出最大值+插入元素

而第三个需要用可并堆来维护,可以选用编程复杂度较低的左偏树来实现。

Code: O(TMlogN) [5232K, 951MS]

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 100005;

inline void getint(int &num){
	char ch;
	while(!isdigit(ch = getchar()));
	num = ch - '0';
	while(isdigit(ch = getchar())) num = num * 10 + ch - '0';
}

int N, M, sn[MAXN];

struct Node{
	int val, dist;
	Node *lc, *rc, *fa;
} pool[MAXN], *nul = pool;

inline void build(int *a, int n){
	nul->val = nul->dist = -1;
	for(register int i = 1; i <= n; i++){
		pool[i].val = a[i], pool[i].dist = 0;
		pool[i].lc = pool[i].rc = nul, pool[i].fa = &pool[i];
	}
}

inline Node* find(Node *u) {while(u->fa != u) u = u->fa; return u;}

inline Node* merge(Node *u, Node *v){
	if(u == nul) return v;
	if(v == nul) return u;
	if(u->val == v->val ? u > v : u->val < v->val) swap(u, v);
	u->rc = merge(u->rc, v), u->rc->fa = u;
	if(u->lc->dist < u->rc->dist) swap(u->lc, u->rc);
	return u->dist = u->rc->dist + 1, u;
}

inline Node* pop(Node *u){
	Node *r = merge(u->lc, u->rc);
	r->fa = r, u->lc = u->rc = nul;
	return r;
}

int main(){
	while(scanf("%d", &N) != EOF){
		for(register int i = 1; i <= N; i++) getint(sn[i]);
		build(sn, N), getint(M);
		for(register int i = 1; i <= M; i++){
			int x, y; getint(x), getint(y);
			Node *rx = find(&pool[x]), *ry = find(&pool[y]);
			if(rx == ry) {puts("-1"); continue;}
			rx->val >>= 1, ry->val >>= 1;
			Node *r = merge(pop(rx), pop(ry));
			r = merge(r, merge(rx, ry));
			printf("%d\n", r->val);
		}
	}	
	return 0;
}

评论

还没有任何评论,你来说两句吧



新博客地址: darkleafin.cf

常年不在线的QQ:
49750

不定期更新的GitHub:
https://github.com/Darkleafin


OPEN AT 2017.12.10

如遇到代码不能正常显示的情况,请刷新页面。
Please refresh the page if the code cannot be displayed normally.


发现一个优美的网站:
https://visualgo.net/en
















- Theme by Qzhai