当前位置:网站首页>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;
}
边栏推荐
- [solution] Ming Chu Liang Zao video edge computing gateway solution
- 【英伟达发展历程纪实618-01】
- Problems and solutions of non debug mode execution failure when using gomonkey
- twenty-one
- After the counter is completed, you want to count the results whose string length is greater than 2
- Farm Game
- MySql DUMP 自动备份数据库 Shell 脚本
- BSV上的委托合约(3)
- "Good morning, good afternoon, good night" game jam
- Amazon evaluation browser, core knowledge points of Amazon evaluation risk control
猜你喜欢

2022年Q1手机银行用户规模达6.5亿,加强ESG个人金融产品创新

High score schemes have been opened to the public, and the second round of the China "software Cup" remote sensing competition is coming!

抓包工具:Fiddler,软件测试工程师必备技能

BSV上的委托合约

記錄webscraper的使用過程

2022年中国手机银行年度专题分析

【第 07 章 基于主成分分析的人脸二维码识别MATLAB深度学习实战案例】

全行业数字化转型加速,到底什么存储会更吃香?

Expenditure budget and adjustment records and use records output use progress construction process records

Tongji and Ali won the CVPR best student thesis, lifeifei won the Huang xutao award, and nearly 6000 people attended the offline conference
随机推荐
一条短视频成本几十万元,虚拟数字人凭“实力”出圈
google多用户防止关联工具
“早安、午安、晚安” Game Jam
第 24 章 基于 Simulink 进行图像和视频处理--matlab深度学习实战整理
抓包工具:Fiddler,软件测试工程师必备技能
同济、阿里获CVPR最佳学生论文,李飞飞获黄煦涛奖,近6000人线下参会
第 12 章 基于块匹配的全景图像拼接--Matlab深度学习实战图像处理应用
出现UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xe9 in position 0: ordinal not in range解决方法
Google Earth Engine(GEE)——合并VCI指数和TCI温度得时序影像折线图(危地马拉、萨尔瓦多为例)
Test case design method -- cause and effect diagram method
【第 06 章 MATLAB实现基于分水岭分割进行肺癌诊断】
21
大厂英伟达面试题整理123
【位运算】leetcode1009. Complement of Base 10 Integer
ROS 2 驱动程序现在可用于 ABB 的机械臂
求一个防关联检测工具,浏览器指纹在线检测
Problems and solutions of non debug mode execution failure when using gomonkey
SQL operation: with expression and its application
第六届世界智能大会“云”端召开在即
twenty-one