当前位置:网站首页>LeetCode 952. 按公因数计算最大组件大小
LeetCode 952. 按公因数计算最大组件大小
2022-07-30 17:37:00 【HumbleFool】
LeetCode 952. 按公因数计算最大组件大小
建图 + 并查集
const int N = 1e5 + 10;
class Solution {
public:
int p[N], cnt[N];
int find(int x)
{
if(p[x] != x) p[x] = find(p[x]);
return p[x];
}
int largestComponentSize(vector<int>& nums) {
int n = nums.size();
for(int i = 0; i < N; i ++) p[i] = i, cnt[i] = 0;
for(int i = 0; i < n; i ++)
{
// 通过约数将各个数连接起来
for(int j = 2; j <= nums[i] / j; j ++)
{
if(nums[i] % j == 0)
{
int pa = find(nums[i]), pb = find(nums[i] / j), pc = find(j);
p[pb] = pa;
p[pc] = pa;
}
}
}
int res = 0;
for(int i = 0; i < n; i ++)
{
int pi = find(nums[i]);
cnt[pi] ++; //查找数的根元素出现次数 ++
res = max(res, cnt[pi]);
}
return res;
}
};
边栏推荐
- JMeter Notes 3 | JMeter Installation and Environment Instructions
- matlab simulink锂离子电池智能充电策略研究
- 论文阅读之《DeepIlluminance: Contextual IlluminanceEstimation via Deep Neural Networks》
- torch.optim.Adam() function usage
- FP6606CMP5 CPC-16L USB类型-C和PD充电控制器 百盛电子代理商
- 【网络工程】A、B、C、D、E类IP地址划分依据和特殊的IP地址
- 从零开始的Multi-armed Bandit
- 快使用flyway管理sql脚本吧~
- 多年以后「PageHelper」又深深的给我上了一课
- Shell implementation based on stm32
猜你喜欢

莫队--优雅的暴力
![(17)[系统调用]追踪系统调用(0环)](/img/d4/aa48745ac918ebfc45c07b587fa86f.png)
(17)[系统调用]追踪系统调用(0环)

LeetCode167: Sum of two numbers in sorted array

JMeter Notes 3 | JMeter Installation and Environment Instructions

FP6606ACAW4 TQFN-20L (3mmx3mm) USB双端口充电控制器 百盛电子代理

向量检索基础方法总结

知识蒸馏1:基础原理讲解及yolov5项目实战介绍
![Valid bracketed strings [greedy exercise]](/img/1c/5cefb53bc4aba54dd79b0cc9b09b0d.png)
Valid bracketed strings [greedy exercise]

Shell implementation based on stm32

C陷阱与缺陷 第7章 可移植性缺陷 7.5 移位运算符
随机推荐
万华化学精细化工创新产品大会
如何让 JOIN 跑得更快?
将 APACHE 日志解析到 SQL 数据库中
数据预处理:离散特征编码方法
Shell implementation based on stm32
优酷视频元素内容召回系统:多级多模态引擎探索
JMeter笔记3 | JMeter安装和环境说明
什么是工业射线照相设备?
Error EPERM operation not permitted, mkdir ‘Dsoftwarenodejsnode_cache_cacach两种解决办法
升级Win11后不喜欢怎么退回Win10系统?
高级语言垃圾回收思路和如何减少性能影响原理分析
信息学奥赛一本通 1915:【01NOIP普及组】最大公约数与最小公倍数 | 洛谷 P1029 [NOIP2001 普及组] 最大公约数和最小公倍数问题
js中的基础知识点 —— BOM
向量检索基础方法总结
Deep Feedback Network for Recommendation
习题:变量、常量和基本数据类型
基于MATLAB的电力系统短路故障分析与仿真
C语言向MySQL插入数据
Tensorflow模型量化(Quantization)原理及其实现方法
全球架构师峰会