-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathLC0952.cpp
executable file
·64 lines (57 loc) · 1.13 KB
/
LC0952.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Problem Statement: https://leetcode.com/problems/largest-component-size-by-common-factor/
Time: O(n • sqrt(m))
Space: O(m)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
*/
class UnionFind {
private:
vector<int> link;
public:
UnionFind(int n) : link(n) {
iota(link.begin(), link.end(), 0);
}
int find(int x) {
int y = x;
// find the root of component
while (x != link[x])
x = link[x];
// path compression
while (y != x)
y = exchange(link[y], x);
return x;
}
void unify(int a, int b) {
a = find(a);
b = find(b);
link[b] = a;
}
};
class Solution {
public:
int largestComponentSize(vector<int>& A) {
int n, m, max_len;
n = A.size();
m = *max_element(A.begin(), A.end());
max_len = 0;
UnionFind dsu(m + 1);
vector<int> cnt(m + 1);
// prime factorization
for (int x: A) {
int a = x;
for (int y = 2; y * y <= x; y++) {
if (x % y != 0)
continue;
while (x % y == 0)
x /= y;
dsu.unify(y, a);
}
if (x > 1)
dsu.unify(x, a);
}
// get size of largest component
for (int x: A)
max_len = max(++cnt[dsu.find(x)], max_len);
return max_len;
}
};