当前位置:网站首页>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;
}
};
边栏推荐
- Py程序员的七夕情人节
- Web 3.0入门教程
- 测试行业干了5年,从只会点点点到了现在的测试开发,总算是证明了自己
- 中文字符集编码Unicode ,gb2312 , cp936 ,GBK,GB18030
- Error occurred while trying to proxy request The project suddenly can't get up
- 592. Fraction Addition and Subtraction
- 首发!阿里技术大牛最新耗时半个月整理出最全MySQL性能优化和高可用架构技术宝典,直接封神!
- LeetCode167: Sum of two numbers in sorted array
- 高级语言垃圾回收思路和如何减少性能影响原理分析
- 什么是无损检测设备?
猜你喜欢

FastJson反序列化漏洞(复现)

js中的基础知识点 —— BOM

Research on intelligent charging strategy of matlab simulink lithium-ion battery

olap——入门ClickHouse

Analysis and Simulation of Short Circuit Fault in Power System Based on MATLAB

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.

Shell implementation based on stm32

Promise entry to proficient (1.5w word detailed explanation)

Win11如何把d盘空间分给c盘?Win11d盘分盘出来给c盘的方法

测试.net文字转语音模块System.Speech
随机推荐
有效的括号字符串[贪心练习]
KDD 2020 | 深入浅出优势特征蒸馏在淘宝推荐中的应用
C# 跨程序传图(共享内存块传图)跨exe传图
SLIM: Sparse Linear Methods (TopN推荐)
FastJson反序列化漏洞(复现)
ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)Solution
腾讯专家献上技术干货,带你一览腾讯广告召回系统的演进
Web3时代重要基础设施深度拆解:4EVERLAND
C陷阱与缺陷 第7章 可移植性缺陷 7.5 移位运算符
数据库系统原理与应用教程(063)—— MySQL 练习题:操作题 39-50(七):SELECT 基本语法联系
Moralis去中心化Web3应用开发教程
理解实现搜索二叉树
Dive deep on Netflix‘s recommender system(Netflix推荐系统是如何实现的?)
Win11如何把d盘空间分给c盘?Win11d盘分盘出来给c盘的方法
Error EPERM operation not permitted, mkdir 'Dsoftwarenodejsnode_cache_cacach Two solutions
bert-base调试心得
强烈推荐APP破解常用工具集合!
weiit新零售小程序如何探索数字化门店的破局之路
从零开始的Multi-armed Bandit
ARC在编译期和运行期做了什么