UVA11987Almost Union-Find(并查集删除节点)
题意:n个数(即1-n)和m个操作:
1表示把x和y合并,2表示把x移到y集合里面,3表示统计x集合的元素个数
1,3好说,关键是2操作,可以先把2删除掉,删除的操作可以找一个其他的数字来取代x,这样就有新生出来一个集合,移到y集合就合并
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 100000;
typedef long long LL;
int father[N + N + 10];
int sum[N + N + 10],cnt[N + N + 10]; // sum求集合里面所有元素和,cnt表示个数
int value[N + N + 10]; //表示第i个数值
int n,m,k;
void init()
{
k = n + 1;
for(int i = 0; i <= n; i++)
{
father[i] = i;
sum[i] = i;
cnt[i] = 1;
value[i] = i;
}
}
int Find(int x)
{
if(x == father[x])
return x;
return father[x] = Find(father[x]);
}
void Union(int x, int y)
{
int fx = Find(x);
int fy = Find(y);
if(fx != fy)
{
father[fx] = fy;
sum[fy] += sum[fx];
cnt[fy] += cnt[fx];
}
}
void Remove(int x)
{
/*
int fx = Find(value[x]);
cnt[fx]--;
sum[fx] -= x;
value[x] = k;
int fy = Find(value[y]);
father[k] = fy;
cnt[fy]++;
sum[fy] += x;
k++;
*/
int fx = Find(value[x]);
cnt[fx]--; // 删除x时从父节点集合中减一
sum[fx] -= x; // sum中减去x
value[x] = k; // 用 k 来代替 x ,生成了一个单独的集合,父节点是k
cnt[k] = 1;
sum[k] = x;
father[k] = k;
k++;
}
int main()
{
while(scanf("%d%d", &n, &m) != EOF)
{
k = n + 1;
for(int i = 0; i <= n; i++)
{
father[i] = i;
sum[i] = i;
cnt[i] = 1;
value[i] = i;
}
int order,p,q;
while(m--)
{
scanf("%d", &order);
if(order == 1)
{
scanf("%d%d", &p, &q);
Union(value[p], value[q]);
}
else if(order == 2)
{
scanf("%d%d", &p, &q);
int fx = Find(value[p]);
int fy = Find(value[q]);
if(fx != fy) // 如果p和q本来就在一个集合,就不用操作了。但是少了这个就wa了,不明白为什么操作不行
{
Remove(p);
Union(value[p], value[q]);
}
}
else if(order == 3)
{
scanf("%d", &p);
int fx = Find(value[p]);
printf("%d %d\n", cnt[fx], sum[fx]);
}
}
}
return 0;
}
UVA11987Almost Union-Find(并查集删除节点)的更多相关文章
- hdu2473 Junk-Mail Filter 并查集+删除节点+路径压缩
Description Recognizing junk mails is a tough task. The method used here consists of two steps: 1) ...
- 洛谷UVA11987Almost Union-Find题解--并查集的删除
题目链接 https://www.luogu.org/problemnew/show/UVA11987 分析 分析下操作发现就是加了个删除操作的并查集,怎么做删除操作呢. 我们用一个\(id[]\)记 ...
- 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 ...
- 并查集(删除) UVA 11987 Almost Union-Find
题目传送门 题意:训练指南P246 分析:主要是第二种操作难办,并查集如何支持删除操作?很巧妙的方法:将并查集树上p的影响消除,即在祖先上(sz--, sum -= p),然后为p换上马甲:id[p] ...
- HDU 5441 Travel(并查集+统计节点个数)
http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给出一个图,每条边有一个距离,现在有多个询问,每个询问有一个距离值d,对于每一个询问,计算出有多少点 ...
- 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~ ...
- HDU2473 Junk-Mail Filter - 并查集删除操作(虚父节点)
传送门 题意: 每次合并两份邮件,或者将某一份邮件独立出来,问最后有多少个邮件集合. 分析: 考虑初始化每个节点的祖先为一个虚父节点(i + n),虚父节点指向它自己.这样可以进行正常的合并操作. 而 ...
- UVA - 11987 Almost Union-Find[并查集 删除]
UVA - 11987 Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, y ...
随机推荐
- angular一些冷门的用法
1.controller的第三个参数
- [jQuery EasyUI系列] 创建增删改查应用
一.数据收集并妥善管理数据是网络应用共同的必要.CRUD允许我们生产页面列表并编辑数据库记录. 本文主要演示如何使用jQuery EasyUI实现CRUD DataGrid. 将使用到的插件有: da ...
- 34-nl 简明笔记
为文本文件添加行号 nl [options] files 参数 files是nl需要为其添加行号的文本文件路径名,如果有多个文件,则nl会把多个文件合在一起编号,并输出到标准输出上 选项 -b ...
- python环境搭建-在Windows上安装python3.5.2
在Windows上安装Python3.5.2 首先,根据你的Windows版本(64位还是32位)从Python的官方网站下载Python 3.5.2对应的64位安装程序或32位安装程序(网速慢的同学 ...
- bootstrap实现pc屏幕五等分
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...
- Query DSL for elasticsearch Query
Query DSL Query DSL (资料来自: http://www.elasticsearch.cn/guide/reference/query-dsl/) http://elasticsea ...
- 关于php插件pdo_mysql的安装
今天在做一个商城的连接的时候,需要MpDO验证.需要安装pdo_mysql模块,刚开始按照php扩展模块的安装按照这个安装ZIP,curl都成功了但是安装pdo_mysql却不行,在./configu ...
- 【转】c# 调用windows API(user32.dll)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...
- 小 div在大 div中左右上下居中
<!DOCTYPE HTML><html><head> <meta http-equiv="Content-Type" content=& ...
- Mustache 使用总结
前言: 在分析 jeesite 项目的时候,看到了 Mustache,于是查了下 正文: 1.Mustache 概述 Mustache 是基于 JavaScript 实现的模板引擎,即用来 渲染前台页 ...