-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2709.cpp
More file actions
71 lines (70 loc) · 2.06 KB
/
2709.cpp
File metadata and controls
71 lines (70 loc) · 2.06 KB
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
65
66
67
68
69
70
71
const int primes[65] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131,
137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223,
227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313};
class DisjointSet {
public:
vector<int> parents;
vector<int> ranks;
DisjointSet(int size) {
parents.resize(size + 1, 0);
for (int i = 0; i < size + 1; ++i) parents[i] = i;
ranks.resize(size + 1, 1);
}
int find(int x) {
if (parents[x] != x) {
parents[x] = find(parents[x]);
}
return parents[x];
}
void join(int x, int y) {
int pX = find(x);
int pY = find(y);
if (pX == pY) return;
if (ranks[pX] > ranks[pY]) {
parents[pY] = pX;
}
else if (ranks[pX] < ranks[pY]) {
parents[pX] = pY;
}
else {
ranks[pX]++;
parents[pY] = pX;
}
}
};
class Solution {
public:
vector<int> factorize(int n) {
vector<int> facts;
for (int p : primes)
if (n % p == 0) {
facts.push_back(p);
while(n % p == 0)
n /= p;
}
if (n != 1) // Some large prime
facts.push_back(n);
return facts;
}
bool canTraverseAllPairs(vector<int>& nums) {
int numOneCnt = 0;
for (auto num : nums) {
if (num == 1) numOneCnt++;
if (numOneCnt > 1) return false;
}
int maxValue = *max_element(nums.begin(), nums.end());
DisjointSet* disjointSet = new DisjointSet(maxValue);
for (auto num : nums) {
for (auto f : factorize(num)) {
disjointSet->join(num, f);
}
}
unordered_set<int> parents;
for (auto num : nums) {
int parent = disjointSet->find(num);
parents.insert(parent);
}
return parents.size() == 1;
}
};