【leetcode 952. 按公因数计算最大组件大小】【欧拉筛+并查集】
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; class Solution {
List<Integer> list = new ArrayList<>();
int primeNum = 0;
int[] fa;
int[] rank;
int[] map ; public static void main(String[] args) {
Solution solution = new Solution();
int cnt = solution.largestComponentSize(new int[]{
12377,95569,53366,10979,43909,40213,22501,359,96269,82847,85829,81154,43423,2801,97049,30053,72049,84779,17659,34033,49663,10159,61283,91771,22153,93991,8779,10357,89213,2791,55465,21061,9017,44777,77876,67343,64661,28631,20107,23557,10201,86737,4534,31481,83761,8977,59743,3659,4479,42577,2011,94063,59233,69777,31957,4909,82633,70709,72977,52973,74216,93109,37328,5591,77323,91857,36259,42743,76739,94697,88339,14801,39982,59473,26153,75689,55201,12293,14669,94819,36545,33403,16183,809,12451,20602,52354,54476,51421,53267,25589,45869,13829,90821,37547,52951,80943,33329,48989,11483,21548,75323,32987,51503,16987,89911,54589,74821,25763,34283,23977,46589,87023,36979,40057,43159,12763,29339,41521,85823,57029,69259,18119,27947,97561,54669,63377,69739,72367,27793,57373,79757,30187,83,83089,47527,69899,43786,26951,84278,36721,58207,81773,60283,79641,29483,87797,76313,30236,54359,16007,99371,44501,42649,14673,95789,31907,66049,93745,93985,4591,12813,5813,89767,41045,3607
});
System.out.println(cnt);
} public int largestComponentSize(int[] nums) {
pre();
int cnt1 = 0;
for (int num : nums) {
int tmp = num;
if( num == 1){
cnt1=1;
continue;
}
List<Integer> primes = new ArrayList<>();
for (int i = 0; i < primeNum && list.get(i) <= tmp && map[tmp] == -1; i++) {
int p = list.get(i);
boolean find = false;
while (tmp % p == 0) {
tmp = tmp / p;
find = true;
}
if (find) {
primes.add(i);
}
}
int set0 = map[tmp]>-1?map[tmp]:primes.get(0);
rank[find(set0)]++;
for (int j = 0; j < primes.size(); j++) {
int set = primes.get(j);
merge(set0, set);
} }
int max = 0;
for (int i = 0; i < list.size(); i++) {
int p = find(i);
if (rank[p] > max) {
max = rank[p];
}
}
return Math.max(max,1);
} public void merge(int set0, int set1) {
int p1 = find(set0);
int p2 = find(set1); if (p1 == p2) {
return;
} if (rank[p1] > rank[p2]) {
fa[p2] = p1;
rank[p1] += rank[p2];
} else {
fa[p1] = p2;
rank[p2] += rank[p1];
} } public int find(int set0) {
int tmp = set0;
while (fa[tmp] != tmp) {
tmp = fa[tmp];
}
int root = tmp;
while(fa[set0] != set0){
int p = fa[set0];
fa[set0] = root;
set0 = p;
}
return root;
} public void pre() {
primeNum = 0;
Arrays.fill(isprime,true);
for (int i = 2; i <= 100000; i++) {
if (isprime[i]) {
primeNum++;
list.add(i);
}
for(int j=i;j<=1_000_00;j+=i){
isprime[j] = false;
}
isprime[i] = true;
}
// System.out.println(primeNum);
fa = new int[primeNum];
rank = new int[primeNum];
for (int i = 0; i < primeNum; i++) {
fa[i] = i;
rank[i] = 0;
}
map = new int[100001];
Arrays.fill(map,-1);
for(int i = 0;i<list.size();i++){
map[list.get(i)] = i;
} } boolean[] isprime = new boolean[100001];
}
【leetcode 952. 按公因数计算最大组件大小】【欧拉筛+并查集】的更多相关文章
- [Swift]LeetCode952. 按公因数计算最大组件大小 | Largest Component Size by Common Factor
Given a non-empty array of unique positive integers A, consider the following graph: There are A.len ...
- 7-10 社交网络图中结点的“重要性”计算(30 point(s)) 【并查集+BFS】
7-10 社交网络图中结点的"重要性"计算(30 point(s)) 在社交网络中,个人或单位(结点)之间通过某些关系(边)联系起来.他们受到这些关系的影响,这种影响可以理解为网络 ...
- PAT Advanced A1021 Deepest Root (25) [图的遍历,DFS,计算连通分量的个数,BFS,并查集]
题目 A graph which is connected and acyclic can be considered a tree. The height of the tree depends o ...
- leetcode 886. 可能的二分法(DFS,染色,种类并查集)
题目链接 886. 可能的二分法 题意: 给定一组 N 人(编号为 1, 2, ..., N), 我们想把每个人分进任意大小的两组. 每个人都可能不喜欢其他人,那么他们不应该属于同一组. 形式上,如果 ...
- QuantLib 金融计算——基本组件之 InterestRate 类
目录 QuantLib 金融计算--基本组件之 InterestRate 类 InterestRate 对象的构造 一些常用的成员函数 如果未做特别说明,文中的程序都是 Python3 代码. Qua ...
- 【Java布局】FlowLayout布局时设定组件大小
默认的JPanel中,采用的是FlowLayout布局 下面是api中的定义: JPanel(boolean isDoubleBuffered) 创建具有 FlowLayout 和 ...
- ios UITextView 计算文字内容大小
先设置好 textView的内容文字,再调用以下代码,就能够得到文字内容的size,其中参数表示最大的size的尺寸,通常,高度应该不限制,宽度是控件的宽度. let newSize = statem ...
- 【转】Android绘制View的过程研究——计算View的大小
Android绘制View的过程研究——计算View的大小 转自:http://liujianqiao398.blog.163.com/blog/static/18182725720121023218 ...
- iOS计算缓存文件的大小
//获取缓存文件路径 -(NSString *)getCachesPath{ // 获取Caches目录路径 NSArray *paths = NSSearchPathForDirectoriesIn ...
- PHP计算某个目录大小的方法
用PHP来计算某个目录大小的方法. PHP CURL session COOKIE 可以调用系统命令,还可以这样: <?php function dirsize($dir) { @$dh ...
随机推荐
- 蓝鲸:安装SaaS组件bk_monitor失败分析解决
使用./bk_install saas-o 安装发现bk_monitor(蓝鲸监控)组件报错"ERROR deploy failed: timeout". 单独尝试安装各个组件: ...
- .NET Core开发实战(第19课:日志作用域:解决不同请求之间的日志干扰)--学习笔记
19 | 日志作用域:解决不同请求之间的日志干扰 开始之前先看一下上一节的代码 // 配置的框架 var configBuilder = new ConfigurationBuilder(); con ...
- Python numpy数组操作(分割数组)
分割数组 函数 数组及操作 split 将一个数组分割为多个子数组 hsplit 将一个数组水平分割为多个子数组(按列) vsplit 将一个数组垂直分割为多个子数组(按行) numpy.split ...
- Hive-分区取TOP N问题
问题背景 设想你对用户在不同品类上的行为打分聚合后得到这样一个表 user_cate_score uid cate score 1 1 0.3 2 2 0.5 8 3 0.9 现在,你想将每个品类的T ...
- Power BI 14 DAY
快消行业销售情况分析 分析目的 帮助经销商优化进.销.存各环节业务流程行为,实现进销存一体化管理,加快资金运转速度,实现销售利润最大化的目的 重要指标 销售类指标 动销次数:在一段期间内产品的销售次数 ...
- Nginx实战-公网LB限流配置等
前提: Nginx要实现根据ip地址进行限流与不限流的区分需要通过源码包安装GeoIP模块 找到与yum安装版本相同的源码包,通过configure进行安装 ./configure --prefix= ...
- SATA 中ATA与AHCI的区别
SATA中ATA和AHCI有什么区别? 1.ACHI是针对SATA2设计的,可以卡其NCQ功能,表面上没有速度的优势,但是因为算法不同,可以有效的保护硬盘.ATA 是硬件模拟IDE的一种方法.表面 ...
- 虚拟化技术VirtualBox和vagrant基本使用
虚拟化技术VirtualBox和vagrant基本使用 1.首先安装VirtualBox 可以去官网下载 https://www.virtualbox.org/ 2.安装vagrant(根据自己电脑得 ...
- springboot多线程TaskExecutor的使用,以及使用@Async实现异步调用
目录 @Async实现异步调用 pom.xml 启动类 定义controller 定义接口 实现类 将isDone换程CountDownLatch来判断线程是否执行完实例化CountDownLatch ...
- 【App Service for Windows】为 App Service 配置自定义 Tomcat 环境
问题描述 当在App Service for Windows环境中所列出的Tomcat Version 没有所需要的情况下,如何实现自定义Tomcat 环境呢? 问题解答 第一步: 从官网下载要使用的 ...