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