当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢

自动化早已不是那个自动化了,谈一谈自动化测试现状和自我感受……

FP6606CMP5 CPC-16L USB类型-C和PD充电控制器 百盛电子代理商

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

JMeter Notes 4 | JMeter Interface Introduction
![有效的括号字符串[贪心练习]](/img/1c/5cefb53bc4aba54dd79b0cc9b09b0d.png)
有效的括号字符串[贪心练习]

Error EPERM operation not permitted, mkdir ‘Dsoftwarenodejsnode_cache_cacach两种解决办法

ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)Solution

真正懂经营管理的CIO具备哪些特质

FP6600QSO SOP-8 USB专用充电端口控制器 用于快充电协议和QC2.0/3.0

Promise入门到精通(1.5w字详解)
随机推荐
Express framework connects MySQL and ORM framework
C陷阱与缺陷 第7章 可移植性缺陷 7.1 应对C语言标准变更
CMake库搜索函数居然不搜索LD_LIBRARY_PATH
fast shell porting
Summary of String Copy, Concatenation, Comparison and Split Functions (1)
C陷阱与缺陷 第7章 可移植性缺陷 7.5 移位运算符
知识蒸馏4:准备数据集并修改网络配置
基于亚马逊云科技无服务器服务快速搭建电商平台——性能篇
Daily practice------Generate 13-digit bar, Ean-13 code rule: The thirteenth digit is the check code obtained by the calculation of the first twelve digits.
leetcode:1488. 避免洪水泛滥【二分 + 贪心】
一篇文 带你搞懂,虚拟内存、内存分页、分段、段页式内存管理(超详细)
matlab simulink锂离子电池智能充电策略研究
华为无线设备配置Mesh业务
JMeter Notes 3 | JMeter Installation and Environment Instructions
快使用flyway管理sql脚本吧~
Error occurred while trying to proxy request项目突然起不来了
Valid bracketed strings [greedy exercise]
【综合类型第 34 篇】喜讯!喜讯!!喜讯!!!,我在 CSDN 的第一个实体铭牌
信息学奥赛一本通 1915:【01NOIP普及组】最大公约数与最小公倍数 | 洛谷 P1029 [NOIP2001 普及组] 最大公约数和最小公倍数问题
升级Win11后不喜欢怎么退回Win10系统?