File tree Expand file tree Collapse file tree 10 files changed +1119
-8
lines changed
solution/3300-3399/3378.Count Connected Components in LCM Graph Expand file tree Collapse file tree 10 files changed +1119
-8
lines changed Load Diff Large diffs are not rendered by default.
Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change
1
+ typedef struct DSU {
2
+ unordered_map<int , int > par, rank;
3
+ DSU (int n) {
4
+ for (int i = 0 ; i < n; ++i) {
5
+ par[i] = i;
6
+ rank[i] = 0 ;
7
+ }
8
+ }
9
+
10
+ void makeSet (int v) {
11
+ par[v] = v;
12
+ rank[v] = 1 ;
13
+ }
14
+
15
+ int find (int x) {
16
+ if (par[x] == x) {
17
+ return x;
18
+ }
19
+ return par[x] = find (par[x]);
20
+ }
21
+
22
+ void unionSet (int u, int v) {
23
+ u = find (u);
24
+ v = find (v);
25
+ if (u != v) {
26
+ if (rank[u] < rank[v]) swap (u, v);
27
+ par[v] = u;
28
+ if (rank[u] == rank[v]) rank[u]++;
29
+ }
30
+ }
31
+ } DSU;
32
+
33
+ class Solution {
34
+ public:
35
+ int countComponents (vector<int >& nums, int threshold) {
36
+ DSU dsu (threshold);
37
+ for (auto & num : nums) {
38
+ for (int j = num; j <= threshold; j += num) {
39
+ dsu.unionSet (num, j);
40
+ }
41
+ }
42
+ unordered_set<int > par;
43
+ for (auto & num : nums) {
44
+ if (num > threshold) {
45
+ par.insert (num);
46
+ } else {
47
+ par.insert (dsu.find (num));
48
+ }
49
+ }
50
+ return par.size ();
51
+ }
52
+ };
Original file line number Diff line number Diff line change
1
+ type DSU struct {
2
+ parent map [int ]int
3
+ rank map [int ]int
4
+ }
5
+
6
+ func NewDSU (n int ) * DSU {
7
+ dsu := & DSU {
8
+ parent : make (map [int ]int ),
9
+ rank : make (map [int ]int ),
10
+ }
11
+ for i := 0 ; i <= n ; i ++ {
12
+ dsu .parent [i ] = i
13
+ dsu .rank [i ] = 0
14
+ }
15
+ return dsu
16
+ }
17
+
18
+ func (dsu * DSU ) Find (x int ) int {
19
+ if dsu .parent [x ] != x {
20
+ dsu .parent [x ] = dsu .Find (dsu .parent [x ])
21
+ }
22
+ return dsu .parent [x ]
23
+ }
24
+
25
+ func (dsu * DSU ) Union (u , v int ) {
26
+ uRoot := dsu .Find (u )
27
+ vRoot := dsu .Find (v )
28
+ if uRoot != vRoot {
29
+ if dsu .rank [uRoot ] < dsu .rank [vRoot ] {
30
+ uRoot , vRoot = vRoot , uRoot
31
+ }
32
+ dsu .parent [vRoot ] = uRoot
33
+ if dsu .rank [uRoot ] == dsu .rank [vRoot ] {
34
+ dsu .rank [uRoot ]++
35
+ }
36
+ }
37
+ }
38
+
39
+ func countComponents (nums []int , threshold int ) int {
40
+ dsu := NewDSU (threshold )
41
+
42
+ for _ , num := range nums {
43
+ for j := num ; j <= threshold ; j += num {
44
+ dsu .Union (num , j )
45
+ }
46
+ }
47
+
48
+ uniqueParents := make (map [int ]struct {})
49
+ for _ , num := range nums {
50
+ if num > threshold {
51
+ uniqueParents [num ] = struct {}{}
52
+ } else {
53
+ uniqueParents [dsu .Find (num )] = struct {}{}
54
+ }
55
+ }
56
+
57
+ return len (uniqueParents )
58
+ }
Original file line number Diff line number Diff line change
1
+ class DSU {
2
+ private Map <Integer , Integer > parent ;
3
+ private Map <Integer , Integer > rank ;
4
+
5
+ public DSU (int n ) {
6
+ parent = new HashMap <>();
7
+ rank = new HashMap <>();
8
+ for (int i = 0 ; i <= n ; i ++) {
9
+ parent .put (i , i );
10
+ rank .put (i , 0 );
11
+ }
12
+ }
13
+
14
+ public void makeSet (int v ) {
15
+ parent .put (v , v );
16
+ rank .put (v , 1 );
17
+ }
18
+
19
+ public int find (int x ) {
20
+ if (parent .get (x ) != x ) {
21
+ parent .put (x , find (parent .get (x )));
22
+ }
23
+ return parent .get (x );
24
+ }
25
+
26
+ public void unionSet (int u , int v ) {
27
+ u = find (u );
28
+ v = find (v );
29
+ if (u != v ) {
30
+ if (rank .get (u ) < rank .get (v )) {
31
+ int temp = u ;
32
+ u = v ;
33
+ v = temp ;
34
+ }
35
+ parent .put (v , u );
36
+ if (rank .get (u ).equals (rank .get (v ))) {
37
+ rank .put (u , rank .get (u ) + 1 );
38
+ }
39
+ }
40
+ }
41
+ }
42
+
43
+ class Solution {
44
+ public int countComponents (int [] nums , int threshold ) {
45
+ DSU dsu = new DSU (threshold );
46
+
47
+ for (int num : nums ) {
48
+ for (int j = num ; j <= threshold ; j += num ) {
49
+ dsu .unionSet (num , j );
50
+ }
51
+ }
52
+
53
+ Set <Integer > uniqueParents = new HashSet <>();
54
+ for (int num : nums ) {
55
+ if (num > threshold ) {
56
+ uniqueParents .add (num );
57
+ } else {
58
+ uniqueParents .add (dsu .find (num ));
59
+ }
60
+ }
61
+
62
+ return uniqueParents .size ();
63
+ }
64
+ }
Original file line number Diff line number Diff line change
1
+ class DSU :
2
+ def __init__ (self , n ):
3
+ self .parent = {i : i for i in range (n )}
4
+ self .rank = {i : 0 for i in range (n )}
5
+
6
+ def make_set (self , v ):
7
+ self .parent [v ] = v
8
+ self .rank [v ] = 1
9
+
10
+ def find (self , x ):
11
+ if self .parent [x ] != x :
12
+ self .parent [x ] = self .find (self .parent [x ])
13
+ return self .parent [x ]
14
+
15
+ def union_set (self , u , v ):
16
+ u = self .find (u )
17
+ v = self .find (v )
18
+ if u != v :
19
+ if self .rank [u ] < self .rank [v ]:
20
+ u , v = v , u
21
+ self .parent [v ] = u
22
+ if self .rank [u ] == self .rank [v ]:
23
+ self .rank [u ] += 1
24
+
25
+
26
+ class Solution :
27
+ def countComponents (self , nums , threshold ):
28
+ dsu = DSU (threshold + 1 )
29
+
30
+ for num in nums :
31
+ for j in range (num , threshold + 1 , num ):
32
+ dsu .union_set (num , j )
33
+
34
+ unique_parents = set ()
35
+ for num in nums :
36
+ if num > threshold :
37
+ unique_parents .add (num )
38
+ else :
39
+ unique_parents .add (dsu .find (num ))
40
+
41
+ return len (unique_parents )
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ private:
3
+ void dfs (int node, vector<vector<int >>& adj, vector<bool >& vis) {
4
+ if (vis[node]) return ;
5
+ vis[node] = true ;
6
+ for (auto & u : adj[node]) {
7
+ dfs (u, adj, vis);
8
+ }
9
+ }
10
+
11
+ public:
12
+ int countComponents (vector<int >& nums, int threshold) {
13
+ vector<vector<int >> adj (threshold + 1 );
14
+ vector<bool > vis (threshold + 1 , false );
15
+ int ans = 0 ;
16
+ for (auto & num : nums) {
17
+ if (num > threshold) {
18
+ ++ans;
19
+ continue ;
20
+ }
21
+ for (int j = 2 * num; j <= threshold; j += num) {
22
+ adj[num].push_back (j);
23
+ adj[j].push_back (num);
24
+ }
25
+ }
26
+ for (auto & num : nums) {
27
+ if (num <= threshold && !vis[num]) {
28
+ dfs (num, adj, vis);
29
+ ++ans;
30
+ }
31
+ }
32
+ return ans;
33
+ }
34
+ };
Original file line number Diff line number Diff line change
1
+ func dfs (node int , adj [][]int , visited []bool ) {
2
+ if visited [node ] {
3
+ return
4
+ }
5
+ visited [node ] = true
6
+ for _ , neighbor := range adj [node ] {
7
+ dfs (neighbor , adj , visited )
8
+ }
9
+ }
10
+
11
+ func countComponents (nums []int , threshold int ) int {
12
+ adj := make ([][]int , threshold + 1 )
13
+ for i := range adj {
14
+ adj [i ] = []int {}
15
+ }
16
+
17
+ visited := make ([]bool , threshold + 1 )
18
+ components := 0
19
+
20
+ for _ , num := range nums {
21
+ if num > threshold {
22
+ components ++
23
+ continue
24
+ }
25
+ for j := 2 * num ; j <= threshold ; j += num {
26
+ adj [num ] = append (adj [num ], j )
27
+ adj [j ] = append (adj [j ], num )
28
+ }
29
+ }
30
+
31
+ for _ , num := range nums {
32
+ if num <= threshold && ! visited [num ] {
33
+ dfs (num , adj , visited )
34
+ components ++
35
+ }
36
+ }
37
+
38
+ return components
39
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ private void dfs (int node , List <List <Integer >> adj , boolean [] visited ) {
3
+ if (visited [node ]) return ;
4
+ visited [node ] = true ;
5
+ for (int neighbor : adj .get (node )) {
6
+ dfs (neighbor , adj , visited );
7
+ }
8
+ }
9
+
10
+ public int countComponents (int [] nums , int threshold ) {
11
+ List <List <Integer >> adj = new ArrayList <>();
12
+ for (int i = 0 ; i <= threshold ; i ++) {
13
+ adj .add (new ArrayList <>());
14
+ }
15
+ boolean [] visited = new boolean [threshold + 1 ];
16
+ int ans = 0 ;
17
+
18
+ for (int num : nums ) {
19
+ if (num > threshold ) {
20
+ ans ++;
21
+ continue ;
22
+ }
23
+ for (int j = 2 * num ; j <= threshold ; j += num ) {
24
+ adj .get (num ).add (j );
25
+ adj .get (j ).add (num );
26
+ }
27
+ }
28
+
29
+ for (int num : nums ) {
30
+ if (num <= threshold && !visited [num ]) {
31
+ dfs (num , adj , visited );
32
+ ans ++;
33
+ }
34
+ }
35
+
36
+ return ans ;
37
+ }
38
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def dfs (self , node , adj , vis ):
3
+ if vis [node ]:
4
+ return
5
+ vis [node ] = True
6
+ for neighbor in adj [node ]:
7
+ self .dfs (neighbor , adj , vis )
8
+
9
+ def countComponents (self , nums , threshold ):
10
+ adj = [[] for _ in range (threshold + 1 )]
11
+ vis = [False ] * (threshold + 1 )
12
+ ans = 0
13
+
14
+ for num in nums :
15
+ if num > threshold :
16
+ ans += 1
17
+ continue
18
+ for j in range (2 * num , threshold + 1 , num ):
19
+ adj [num ].append (j )
20
+ adj [j ].append (num )
21
+
22
+ for num in nums :
23
+ if num <= threshold and not vis [num ]:
24
+ self .dfs (num , adj , vis )
25
+ ans += 1
26
+
27
+ return ans
You can’t perform that action at this time.
0 commit comments