-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay87.cpp
More file actions
46 lines (43 loc) · 1.38 KB
/
Day87.cpp
File metadata and controls
46 lines (43 loc) · 1.38 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
class Solution {
public:
unordered_map<int, vector<int>> pi;
unordered_map<int, vector<int>> ip;
void dfs(int index, vector<int>& Index, unordered_map<int,bool>& Prime){
if(Index[index] == true) return;
Index[index] = true;
for(auto &prime : ip[index]){
if(Prime[prime] == true)
continue;
Prime[prime] = true;
for(auto &index1 : pi[prime]){
if(Index[index1] == true) continue;
dfs(index1, Index, Prime);
}
}
}
bool canTraverseAllPairs(vector<int>& nums) {
int n = nums.size();
for (int i=0; i<n; i++) {
int temp = nums[i];
for (int j = 2; j*j <= nums[i]; j++) {
if (temp % j == 0) {
pi[j].push_back(i);
ip[i].push_back(j);
while (temp % j == 0)
temp /= j;
}
}
if (temp > 1) {
pi[temp].push_back(i);
ip[i].push_back(temp);
}
}
vector<int> Index(nums.size(),0);
unordered_map<int,bool> Prime;
dfs(0, Index, Prime);
for(int i=0; i<Index.size(); i++)
if(Index[i] == false)
return false;
return true;
}
};