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. 按公因数计算最大组件大小】【欧拉筛+并查集】的更多相关文章

  1. [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 ...

  2. 7-10 社交网络图中结点的“重要性”计算(30 point(s)) 【并查集+BFS】

    7-10 社交网络图中结点的"重要性"计算(30 point(s)) 在社交网络中,个人或单位(结点)之间通过某些关系(边)联系起来.他们受到这些关系的影响,这种影响可以理解为网络 ...

  3. 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 ...

  4. leetcode 886. 可能的二分法(DFS,染色,种类并查集)

    题目链接 886. 可能的二分法 题意: 给定一组 N 人(编号为 1, 2, ..., N), 我们想把每个人分进任意大小的两组. 每个人都可能不喜欢其他人,那么他们不应该属于同一组. 形式上,如果 ...

  5. QuantLib 金融计算——基本组件之 InterestRate 类

    目录 QuantLib 金融计算--基本组件之 InterestRate 类 InterestRate 对象的构造 一些常用的成员函数 如果未做特别说明,文中的程序都是 Python3 代码. Qua ...

  6. 【Java布局】FlowLayout布局时设定组件大小

    默认的JPanel中,采用的是FlowLayout布局 下面是api中的定义: JPanel(boolean isDoubleBuffered)           创建具有 FlowLayout 和 ...

  7. ios UITextView 计算文字内容大小

    先设置好 textView的内容文字,再调用以下代码,就能够得到文字内容的size,其中参数表示最大的size的尺寸,通常,高度应该不限制,宽度是控件的宽度. let newSize = statem ...

  8. 【转】Android绘制View的过程研究——计算View的大小

    Android绘制View的过程研究——计算View的大小 转自:http://liujianqiao398.blog.163.com/blog/static/18182725720121023218 ...

  9. iOS计算缓存文件的大小

    //获取缓存文件路径 -(NSString *)getCachesPath{ // 获取Caches目录路径 NSArray *paths = NSSearchPathForDirectoriesIn ...

  10. PHP计算某个目录大小的方法

    用PHP来计算某个目录大小的方法. PHP CURL session COOKIE  可以调用系统命令,还可以这样:  <?php function dirsize($dir) {  @$dh  ...

随机推荐

  1. ElasticSearch7.3学习(十七)----搜索结果字段解析及time_out字段解析

    1.搜索结果字段解析 首先插入一条测试数据 PUT /my_index/_doc/1 { "title": "2019-09-10" } 然后无条件搜索所有 G ...

  2. .NET 云原生架构师训练营(模块二 基础巩固 Scrum 简介)--学习笔记

    2.7.2 Scrum 简介 SCRUM 是什么 SCRUM 精髓 SCRUM 框架 角色 SCRUM 是什么 SCRUM 是迄今为止最著名的敏捷方法,主要用于开发.交付和持续支持复杂产品的一个框架, ...

  3. OpenWrt的dnsmasq, ipset和iptables配置

    说明 这篇文章主要用于介绍在运行OpenWrt的MT7621系列路由器上, 如果安装v2rxy并开启自动出园功能. 这里介绍的是最佳实践, 不同于常见的代理方法. 通过ipset和iptables配合 ...

  4. Ubuntu18.04 Server安装Harbor

    Harbor是一个开源的本地部署的容器镜像注册中心, 带访问权限控制, 以及管理界面. 作用类似于Java中Maven的私有仓库Nexus, 用于配合Jenkins在部署容器到K8s时提供镜像源, 加 ...

  5. Linux证书问题:curl#60 - “The certificate issuer‘s certificate has expired

    问题说明 最近在centos7上打算安装php7版本,需要下载一个外网https的yum源,结果报错如下: 执行命令 rpm -Uvh https://mirror.webtatic.com/yum/ ...

  6. Java集合框架学习(十三) Collections类详解

    Collections类介绍 这个类操作或返回集合的专有静态方法. 它包含多态算法,利用wrappers(封装器)返回指定集合类型的新集合,以及其他一些零散功能. 如果该类的方法引用的集合或类对象为n ...

  7. python课本学习-第二章

    chapter 2 编程基础 一.基本语法 1.注释 python注释以\("#"\)开始 多行注释使用三个单引号/双引号作为开始和结束标志 2.标识符与关键字 python命名时 ...

  8. 【Android 逆向】frida 检测绕过

    1. aaa.apk 安装到手机,是一个叫玩吧的应用 ./hooker ...... 23248 浏 览 器 com.browser2345_oem 32541 玩吧 com.wodi.who 244 ...

  9. 在矩池云使用Llama2-7B的方法

    今天给大家分享如何在矩池云服务器使用 Llama2-7b模型. 硬件要求 矩池云已经配置好了 Llama 2 Web UI 环境,显存需要大于 8G,可以选择 A4000.P100.3090 以及更高 ...

  10. 【Azure 应用服务】在安全漏洞扫描中发现有泄露服务器IIS版本的情况,如何实现屏蔽服务版本号信息呢?

    问题描述 当对Azure App Service应用进行安全扫描时,发现了HTTP/S请求的响应头中会包含服务端IIS的版本信息,这是一个低风险因素. 如: Server: Microsoft-IIS ...