BZOJ 2733 HNOI 2012 永无乡 平衡树启示式合并

简介:

题目大意:有一些岛屿,一開始由一些无向边连接。

后来也有不断的无向边增加,每个岛屿有个一独一无二的重要度,问随意时刻的与一个岛屿联通的全部岛中重要度第k大的岛的编号是什么。


思路:首先连通性一定要用并查集维护。然后就是联通快内的第k大问题,显然是平衡树。可是并查集的合并怎么搞?能够考虑按秩合并,这种话就保证每次在平衡树中处理的元素尽量的少,就能够水过这个题了。

注意一下输出-1的推断。


CODE:

#include <map>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 100010
#define SIZE(a) (a == NULL ? 0:a->size)
using namespace std;

map<int,int> G;

struct Complex{
	int val,random,size,cnt;
	Complex *son[2];
	
	Complex(int _) {
		val = _;
		random = rand();
		size = cnt = 1;
		son[0] = son[1] = NULL;
	}
	int Compare(int x) {
		if(x == val)	return -1;
		return x > val;
	}
	void Maintain() {
		size = cnt;
		if(son[0] != NULL)	size += son[0]->size;
		if(son[1] != NULL)	size += son[1]->size;
	}
};

int points,edges,asks;
int src[MAX];
int father[MAX],cnt[MAX];

Complex *tree[MAX];

char c[10];

int Find(int x);

inline void Rotate(Complex *&a,bool dir);
void Insert(Complex *&a,int x);
void Delete(Complex *&a,int x);
int Kth(Complex *a,int k);

int main()
{
	cin >> points >> edges;
	for(int i = 1;i <= points; ++i) {
		father[i] = i,cnt[i] = 1;
		scanf("%d\n",&src[i]);
		G[src[i]] = i;
	}
	for(int x,y,i = 1;i <= edges; ++i) {
		scanf("%d%d",&x,&y);
		int fx = Find(x);
		int fy = Find(y);
		if(fx != fy) {
			father[fy] = fx;
			cnt[fx] += cnt[fy];
		}
	}
	for(int i = 1;i <= points; ++i) {
		int fx = Find(i);
		Insert(tree[fx],src[i]);
	}
	cin >> asks;
	for(int x,y,i = 1;i <= asks; ++i) {
		scanf("%s%d%d",c,&x,&y);
		if(c[0] == 'Q') {
			int fx = Find(x);
			if(y > cnt[fx])	puts("-1");
			else	printf("%d\n",G[Kth(tree[fx],y)]);
		}
		else {
			int fx = Find(x);
			int fy = Find(y);
			if(fx != fy) {
				if(cnt[fy] > cnt[fx])	swap(fx,fy);
				father[fy] = fx;
				cnt[fx] += cnt[fy];
				for(int j = 1;j <= cnt[fy]; ++j) {
					int temp = Kth(tree[fy],1);
					Delete(tree[fy],temp);
					Insert(tree[fx],temp);
				}
			}
		}
	}
	return 0;
}

int Find(int x)
{
	if(father[x] == x)	return x;
	return father[x] = Find(father[x]);
}

inline void Rotate(Complex *&a,bool dir)
{
	Complex *k = a->son[!dir];
	a->son[!dir] = k->son[dir];
	k->son[dir] = a;
	a->Maintain(),k->Maintain();
	a = k;
}

inline void Insert(Complex *&a,int x)
{
	if(a == NULL) {
		a = new Complex(x);
		return ;
	}
	int dir = a->Compare(x);
	if(dir == -1)
		a->cnt++;
	else {
		Insert(a->son[dir],x);
		if(a->son[dir]->random > a->random)
			Rotate(a,!dir);
	}
	a->Maintain();
}

void Delete(Complex *&a,int x)
{
	int dir = a->Compare(x);
	if(dir != -1)
		Delete(a->son[dir],x);
	else {
		if(a->cnt > 1)
			--a->cnt;
		else {
			if(a->son[0] == NULL)	a = a->son[1];
			else if(a->son[1] == NULL)	a = a->son[0];
			else {
				bool _dir = a->son[0]->random > a->son[1]->random;
				Rotate(a,_dir);
				Delete(a->son[_dir],x);
			}
		}
	}
	if(a != NULL)	a->Maintain();
}

int Kth(Complex *a,int k)
{
	if(k <= SIZE(a->son[0]))	return Kth(a->son[0],k);
	k -= SIZE(a->son[0]);
	if(k <= a->cnt)	return a->val;
	return Kth(a->son[1],k - a->cnt);
}




本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5246546.html,如需转载请自行联系原作者



相关文章
|
9月前
牛客网-树的子结构
牛客网-树的子结构
54 0
|
4月前
【初阶数据结构】打破递归束缚:掌握非递归版快速排序与归并排序
【初阶数据结构】打破递归束缚:掌握非递归版快速排序与归并排序
|
4月前
|
数据可视化 搜索推荐 Python
Leecode 刷题笔记之可视化六大排序算法:冒泡、快速、归并、插入、选择、桶排序
这篇文章是关于LeetCode刷题笔记,主要介绍了六大排序算法(冒泡、快速、归并、插入、选择、桶排序)的Python实现及其可视化过程。
40 0
|
8月前
|
算法 架构师 NoSQL
「AVL平衡树专项」带你领略常用的AVL树与红黑树的奥秘(规则篇)
原先属于X的空结点被A“继承”。如果被删除结点是黑色结点,则可能造成树的黑色高度变化。
466 0
每日一题——合并两个有序链表
每日一题——合并两个有序链表
|
Java
红黑树(中)完整建树过程
手撕JAVA(十四)一文中有些地方表述有误,笔者日后在做修改。这里用画图的方式展示两次红黑树的完整建图过程,一次简单,一次复杂,根据建图过程,就可以理解红黑树是如何实现的。 总结的几点为: 1.根节点必为黑色,任何调整动作都无法将根节点染红 2.新插入节点的父节点和uncle节点同为红色,直接染黑父节点和uncle节点,染红祖父节点(如果祖父节点为根节点,就免疫)
87 0
|
机器学习/深度学习
LeetCode每日一题(13)——建立四叉树(递归)
建立四叉树 1.题目 2.示例 3.思路 4.代码
180 0
LeetCode每日一题(13)——建立四叉树(递归)
|
C++
力扣21 - 合并两个有序链表【归并排序思维】
对应力扣21 - 合并两个有序链表,教你如何使用合并两个有序链表
166 0
力扣21 - 合并两个有序链表【归并排序思维】
|
存储 算法 搜索推荐
力扣88 - 合并两个有序数组【归并排序思维】
力扣88 - 合并两个有序数组,将两个有序数组做一个融合
236 0
力扣88 - 合并两个有序数组【归并排序思维】
|
存储 算法 Java
算法学习入门Day3_lecode_88 合并两个有序数组,哦 上帝。
算法学习入门Day3_lecode_88 合并两个有序数组,哦 上帝。
算法学习入门Day3_lecode_88 合并两个有序数组,哦 上帝。