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]; }
标签:tmp,p2,952,int,查集,rank,fa,set0,公因数 From: https://www.cnblogs.com/fishcanfly/p/16852747.html