forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay-30-Largest Component Size by Common Factor.cpp
80 lines (53 loc) · 1.49 KB
/
Day-30-Largest Component Size by Common Factor.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000
class Solution {
public:
int MAX=100001;
int find(int x, vector<int> &parent){
if(parent[x]==-1) return x;
parent[x]=find(parent[x], parent);
return parent[x];
}
void unionn(int x, int y, vector<int> &parent){
int xp=find(x, parent);
int yp=find(y, parent);
if(xp!=yp)
parent[yp]=xp;
}
int largestComponentSize(vector<int>& A) {
int n=A.size();
vector<int> parent(MAX, -1);
for(int i=0;i<n;i++){
for(int j=2; j<=sqrt(A[i]);j++){
if(A[i]%j==0){
unionn(A[i], j, parent);
unionn(A[i], A[i]/j, parent);
}
}
}
unordered_map<int, int> mp;
for(int i=0;i<n;i++){
int xp=find(A[i], parent);
mp[xp+1]++;
}
int res=0;
for(auto it=mp.begin(); it!=mp.end(); it++){
res=max(res, it->second);
}
return res;
}
};