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

后来也有不断的无向边增加,每个岛屿有个一独一无二的重要度,问随意时刻的与一个岛屿联通的全部岛中重要度第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);
}

BZOJ 2733 HNOI 2012 永无乡 平衡树启示式合并的更多相关文章

  1. HNOI 2012 永无乡

    codevs 1477 永无乡 http://codevs.cn/problem/1477/ 2012年湖南湖北省队选拔赛  时间限制: 1 s  空间限制: 128000 KB   题目描述 Des ...

  2. 解题:HNOI 2012 永无乡

    题面 并查集维护连通性,然后暴力启发式合并就完了,记得合并时边DFS边清空数组 #include<cstdio> #include<cstring> #include<a ...

  3. Bzoj 2733: [HNOI2012]永无乡 数组Splay+启发式合并

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3955  Solved: 2112[Submit][Statu ...

  4. Bzoj 2733: [HNOI2012]永无乡(线段树+启发式合并)

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己 ...

  5. [BZOJ2733][HNOI2010]永无乡 解题报告 启发式合并,线段树合并

    好久没更新博客了,前段时间一直都在考试,都没时间些,现在终于有点闲了(cai guai)... 写了一道题,[HNOI2012]永无乡,其实是一道板子题,我发现我写了好多板子题...还是太菜了... ...

  6. BZOJ2733 永无乡 【splay启发式合并】

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 4190  Solved: 2226 [Submit][Sta ...

  7. BZOJ2733 [HNOI2012]永无乡 【线段树合并】

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  8. BZOJ2733 永无乡【splay启发式合并】

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  9. BZOJ2733: [HNOI2012]永无乡(线段树合并)

    Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...

随机推荐

  1. Codeforces 468B Two Sets 并查集

    题目大意:给出n个数,要求将n个数分配到两个集合中,集合0中的元素x,要求A-x也再0中,同理1集合. 写了几个版本号,一直WA在第8组数据...最后參考下ans,写了并查集过了 学到:1.注意离散的 ...

  2. fread(),fwrite() 读/写流

    C 库函数 - fread() 描述 C 库函数 size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) 从给定流 strea ...

  3. openssl 非对称加密算法RSA命令详解

    1.非对称加密算法概述 非对称加密算法也称公开密钥算法,其解决了对称加密算法密钥分配的问题,非对称加密算法基本特点如下: 1.加密密钥和解密密钥不同 2.密钥对中的一个密钥可以公开 3.根据公开密钥很 ...

  4. sqlite创建数据库问题

    1.<Sqlite权威指南>上说是这么创建数据库的: sqlite3 test.db 但是我写了这条语句之后出现了下面的情况(注:安装Sqlite过程见 ...) 我的sqlite3放在 ...

  5. android码农神器 偷懒工具 android懒人框架 LoonAndroid 3 讲解

    LoonAndroid 3.0 Loonandroid是一个注解框架,不涉及任何UI效果,目的是一个功能一个方法,以方法为最小颗粒度对功能进行拆解.把功能傻瓜化,简单化,去掉重复性的代码,隐藏复杂的实 ...

  6. Windows命令行(DOS命令)教程 -1 (转载) http://www.pconline.com.cn/pcedu/rookie/basic/10111/15325.html

    一.命令行简介 命令行就是在Windows操作系统中打开DOS窗口,以字符串的形式执行Windows管理程序. 在这里,先解释什么是DOS? DOS--Disk Operation System 磁盘 ...

  7. MessageDigest 类

    MessageDigest 类    MessageDigest 类是一个引擎类,它是为了提供诸如 SHA1 或 MD5 等密码上安全的报文摘要功能而设计的.密码上安全的报文摘要可接受任意大小的输入( ...

  8. asp.net从服务器(指定文件夹)下载任意格式的文件到本地

    一.我需要从服务器下载ppt文件到本地 protected void Btn_DownPPT_Click(object sender, EventArgs e)        {            ...

  9. [原创]旧事重提:只配置参数实现OAuth2登录

    其实这个组件写出来很长时间了,有几个月吧,一直在 MrHuo工作室  上放着,一直说要整理,太忙没时间. 另外,关于OAuth2的一些基础内容还请从网上找找资料,太多了,写的累赘. 废话不多说,先上图 ...

  10. Python标准库内置函数——hasattr

    hasattr(object, name): # object 对象 name 特征名称 判断对象object是否包含名为name的特性(hasattr是通过调用getattr(ojbect, nam ...