当前位置:网站首页>acwing 837. 连通块中点的数量 (并查集维护额外信息---集合数量)
acwing 837. 连通块中点的数量 (并查集维护额外信息---集合数量)
2022-06-22 01:16:00 【_刘小雨】
在上一节写了,并查集中,主要有两个功能,现在探索一下,并查集能不能保存额外的信息呢
回答是可以的哈
并查集保存额外的信息: 这里指的是保存每个集合的数量。
题目
给定一个包含 n 个点(编号为 1∼n)的无向图,初始时图中没有边。
现在要进行 m 个操作,操作共有三种:
C a b,在点 a 和点 b 之间连一条边,a 和 b 可能相等;
Q1 a b,询问点 a 和点 b 是否在同一个连通块中,a 和 b 可能相等;
Q2 a,询问点 a 所在连通块中点的数量;
输入格式
第一行输入整数 n 和 m。
接下来 m 行,每行包含一个操作指令,指令为 C a b,Q1 a b 或 Q2 a 中的一种。
输出格式
对于每个询问指令 Q1 a b,如果 a 和 b 在同一个连通块中,则输出 Yes,否则输出 No。
对于每个询问指令 Q2 a,输出一个整数表示点 a 所在连通块中点的数量
每个结果占一行。
数据范围
1≤n,m≤105
输入样例:
5 5
C 1 2
Q1 1 2
Q2 1
C 2 5
Q2 5
输出样例:
Yes
2
3
核心:
- cnt 计数数组
- 集合合并时,数量相加
code:
#include <iostream>
using namespace std;
const int N = 100010;
int p[N], cnt[N]; // cnt 维护集合的大小
int n, m;
int find(int x)
{
if(p[x] != x) p[x] = find(p[x]);
return p[x];
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i++ )
{
p[i] = i;
// 最开始的时候每个集合中只有1个点
cnt[i] = 1; // 只用维护集合中的根节点(祖宗节点), 就一个有效
}
while(m -- )
{
char op[5];
int a, b;
cin >> op;
if(op[0] == 'C')
{
cin >> a >> b;
if(find(a) == find(b)) continue; // 不判断这个, 数量可能会翻倍
// 将b 集合的 加到a集合中, cnt 只用维护a集合的根节点, 所以下面的只能值寻找b集合的祖宗节点,
cnt[find(a)] += cnt[find(b)]; // 只用维护集合中的根节点(祖宗节点), 就一个有效
p[find(b)] = find(a); // 这两行代码需要对应好
}
else if(op[1] == '1')
{
cin >> a >> b;
if(find(a) == find(b)) puts("Yes");
else puts("No");
}
else
{
cin >> a;
cout << cnt[find(a)]<< endl;
}
}
return 0;
}
边栏推荐
- Commission contract on BSV
- [ÑÖÏ Simulation Competition] fading (matrix acceleration, cyclic convolution, Gauss elimination)
- 第 19 章 基于语音识别的信号灯图像模拟控制技术
- Google Earth Engine(GEE)——合并VCI指数和TCI温度得时序影像折线图(危地马拉、萨尔瓦多为例)
- [number theory] leetcode1010 Pairs of Songs With Total Durations Divisible by 60
- Huawei cloud releases desktop ide codearts
- The sandbox has reached a cooperation with Time magazine to establish "New York Times Square" in metauniverse
- Apache ActiveMQ Artemis简介
- 第 09 章 基于特征匹配的英文印刷字符识别 MATLAB深度学习实战案例
- Standing at the digital tuyere, how can tooling enterprises "fly"
猜你喜欢

阿里腾讯百度软件测试工程师推荐——软件测试模型之快速原型模型
![[ÑÖÏ Simulation Competition] fading (matrix acceleration, cyclic convolution, Gauss elimination)](/img/4a/9dfcb699e36f67e14c036e3ae26417.png)
[ÑÖÏ Simulation Competition] fading (matrix acceleration, cyclic convolution, Gauss elimination)

出现IOError: No translation files found for default language zh-cn.的解决方法

The Sandbox 与《时代周刊》达成合作,在元宇宙建立“纽约时报广场”

ROS 2 driver is now available for ABB manipulator

I just learned a cool 3D pyramid stereoscopic effect. Come and have a look

第 24 章 基于 Simulink 进行图像和视频处理--matlab深度学习实战整理

MBA-day24 最值问题

Divide the list into boxes and draw a histogram through pyechart

第六届世界智能大会“云”端召开在即
随机推荐
Commission contract on BSV
MBA-day24 最值问题
大厂英伟达面试题整理123
第 24 章 基于 Simulink 进行图像和视频处理--matlab深度学习实战整理
第 21 章 路面裂缝检测识别系统设计--matlab深度学习实战
【第 14 章 基于主成分分析的图像压缩和重建--matlab深度学习实战案例】
After the counter is completed, you want to count the results whose string length is greater than 2
2022年Q1手机银行用户规模达6.5亿,加强ESG个人金融产品创新
机器学习 Pytorch实现案例 LSTM案例(航班人数预测)
linxu 将文件夹的权限修改为所有人可以访问 777
出现UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xe9 in position 0: ordinal not in range解决方法
站在数字化风口,工装企业如何“飞起来”
Seeking an anti association detection tool, online detection of browser fingerprint
Redis implements distributed locks
The way to build the efficiency platform of didi project
pytorch中copy_()、detach()、data()和clone()操作区别小结
ASEMI肖特基二极管1N5819参数,1N5819代换,1N5819货源
同济、阿里获CVPR最佳学生论文,李飞飞获黄煦涛奖,近6000人线下参会
英特尔笔试题小整理DIY
google多用户防止关联工具