并查集(删除) UVA 11987 Almost Union-Find
题意:训练指南P246
分析:主要是第二种操作难办,并查集如何支持删除操作?很巧妙的方法:将并查集树上p的影响消除,即在祖先上(sz--, sum -= p),然后为p换上马甲:id[p] = ++pos(可多次),这样id[p]就相当于是新的一个点,那么在Find(x)寻找祖先时要用x的马甲来寻找,即Find (id[x])。
#include <bits/stdc++.h>
using namespace std; const int N = 2e5 + 5;
int rt[N], sz[N], sum[N], id[N/2];
int n, m; void init() {
memset (rt, -1, sizeof (rt));
for (int i=1; i<N; ++i) {
sz[i] = 1; sum[i] = i;
}
for (int i=1; i<=n; ++i) id[i] = i;
} int Find(int x) {
return rt[x] == -1 ? x : rt[x] = Find (rt[x]);
} int main(void) {
while (scanf ("%d%d", &n, &m) == 2) {
init ();
int op, p, q, pos = n;
for (int i=1; i<=m; ++i) {
scanf ("%d%d", &op, &p);
if (op == 3) {
int fp = Find (id[p]);
printf ("%d %d\n", sz[fp], sum[fp]);
}
else {
scanf ("%d", &q);
int fp = Find (id[p]), fq = Find (id[q]);
if (op == 1) {
if (fp != fq) {
rt[fp] = fq;
sz[fq] += sz[fp];
sum[fq] += sum[fp];
}
}
else {
if (fp != fq) {
sz[fp]--; sum[fp] -= p;
id[p] = ++pos; //马甲!
rt[id[p]] = fq;
sz[fq]++; sum[fq] += p;
}
}
}
}
} return 0;
}
并查集(删除) UVA 11987 Almost Union-Find的更多相关文章
- UVA - 11987 Almost Union-Find[并查集 删除]
UVA - 11987 Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, y ...
- 第十四届华中科技大学程序设计竞赛 C Professional Manager【并查集删除/虚点】
题目描述 It's universally acknowledged that there're innumerable trees in the campus of HUST. Thus a pro ...
- Hdu 2473(并查集删除操作) Junk-Mail Filter
有木有非常吊 加强 加强版 啊 ,看了都不敢做了 .后来先做了食物链这个我还是看过的.但还是A不掉,没明确神魔意思 .总而言之.大牛的博客是个好东西.我就那么看了一下,还是不懂怎莫办啊,哎, ...
- HDU 2473 Junk-Mail Filter 【并查集删除】
Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu2473 Junk-Mail Filter 并查集+删除节点+路径压缩
Description Recognizing junk mails is a tough task. The method used here consists of two steps: 1) ...
- HDU 2473 Junk-Mail Filter 并查集删除(FZU 2155盟国)
http://acm.hdu.edu.cn/showproblem.php?pid=2473 http://acm.fzu.edu.cn/problem.php?pid=2155 题目大意: 编号0~ ...
- 并查集(不相交集)的Union操作
在并查集(不相交集)中附加操作\(Deunion\),它实现的功能是取消最后一次\(Union\)的操作. 实现思想 初始化一个空栈,将每一次的\(Union\)操作的两个集合的根和其值\(Push\ ...
- 并查集(UVA 1106)
POINT: 把每个元素看成顶点,则一个简单化合物就是一条无向边,若存在环(即k对组合中有k种元素),则危险,不应该装箱,反之,装箱: 用一个并查集维护连通分量集合,每次得到一种化合物(x, y)时检 ...
- UVA11987Almost Union-Find(并查集删除节点)
题目链接 题意:n个数(即1-n)和m个操作: 1表示把x和y合并,2表示把x移到y集合里面,3表示统计x集合的元素个数 1,3好说,关键是2操作,可以先把2删除掉,删除的操作可以找一个其他的数字来取 ...
随机推荐
- 获得同级iframe页面的指定ID元素的几种实现方法
1.JS实现: var object= window.parent.frames("要获得的iframe的name").contentDocument.getElementById ...
- YAML-初识
YAML简介 YAML-what? YAML Ain't Markup Language 和GNU一样,YAML是一个递归着说"不"的名字.不同的是,GNU对UNIX说不,YAML ...
- 解决At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this log
pom增加:<dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</art ...
- 解决ERROR 2003 (HY000): Can't connect to MySQL server on
方案一: .打开cmd; .输入命令:net stop +MySQL的服务名,停止MySQL服务,如果未启动MySQL服务则可跳过该步骤: .输入命令:mysqld --remove卸载MySQL服务 ...
- java删除被占用的文件
boolean result = f.delete();//判断是否删除完毕 if(!result) { System.gc();//系统进行资源强制回收 f.delete; }
- 打开Genesis设置单位为mm
打开Genesis界面: 点击Configuration: 可看到只要设置get_def_units的值即可: 打开C:\genesis\sys\config配置文件,在最后一行加入:get_def_ ...
- android的JNI 、 NDK 学习!
转载的! Java Native Interface (JNI)标准是java平台的一部分,它允许Java代码和其他语言写的代码进行交互.JNI 是本地编程接口,它使得在 Java 虚拟机 (VM) ...
- jQuery - 4.简单选择器
4.1 简单选择器 (1) :first 选取第一个元素. (2) :last 选取最后一个元素. (3) :not(选择器) 选取不满足"选择器"条件的元素 (4) ...
- ASMCMD命令
安装好用的rlwrap工具,在环境变量里添加如下,就能实现显示当前路径(目录),目录补全的方便功能 alias asmcmd='rlwrap -r -i asmcmd –p' asmcmd>he ...
- wp8 入门到精通 仿QQPivot 提示数量
<Grid x:Name="LayoutRoot" Background="White"> <Grid Width="480&quo ...