Skip to content

Commit c039cc7

Browse files
committed
feat: add solutions to lc problem: No. 3378
1 parent 511e9a1 commit c039cc7

File tree

4 files changed

+39
-42
lines changed

4 files changed

+39
-42
lines changed

solution/3300-3399/3378.Count Connected Components in LCM Graph/README.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,7 @@ class Solution {
206206
#### C++
207207

208208
```cpp
209-
typedef struct DSU
210-
{
209+
typedef struct DSU {
211210
unordered_map<int, int> par, rank;
212211
DSU(int n) {
213212
for (int i = 0; i < n; ++i) {
@@ -237,23 +236,22 @@ typedef struct DSU
237236
if (rank[u] == rank[v]) rank[u]++;
238237
}
239238
}
240-
}DSU;
239+
} DSU;
241240

242241
class Solution {
243242
public:
244-
int countComponents(vector<int>& nums, int threshold) {
243+
int countComponents(vector<int> &nums, int threshold) {
245244
DSU dsu(threshold);
246-
for(auto& num : nums) {
245+
for (auto &num : nums) {
247246
for (int j = num; j <= threshold; j += num) {
248247
dsu.unionSet(num, j);
249248
}
250249
}
251250
unordered_set<int> par;
252-
for (auto& num : nums) {
251+
for (auto &num : nums) {
253252
if (num > threshold) {
254253
par.insert(num);
255-
}
256-
else {
254+
} else {
257255
par.insert(dsu.find(num));
258256
}
259257
}
@@ -417,29 +415,30 @@ class Solution {
417415
```cpp
418416
class Solution {
419417
private:
420-
void dfs(int node, vector<vector<int>>& adj, vector<bool>& vis) {
418+
void dfs(int node, vector<vector<int>> &adj, vector<bool> &vis) {
421419
if (vis[node]) return;
422420
vis[node] = true;
423-
for (auto& u : adj[node]) {
421+
for (auto &u : adj[node]) {
424422
dfs(u, adj, vis);
425423
}
426424
}
425+
427426
public:
428-
int countComponents(vector<int>& nums, int threshold) {
427+
int countComponents(vector<int> &nums, int threshold) {
429428
vector<vector<int>> adj(threshold + 1);
430429
vector<bool> vis(threshold + 1, false);
431430
int ans = 0;
432-
for (auto& num : nums) {
431+
for (auto &num : nums) {
433432
if (num > threshold) {
434433
++ans;
435434
continue;
436435
}
437-
for (int j = 2*num; j <= threshold; j += num) {
436+
for (int j = 2 * num; j <= threshold; j += num) {
438437
adj[num].push_back(j);
439438
adj[j].push_back(num);
440439
}
441440
}
442-
for (auto& num : nums) {
441+
for (auto &num : nums) {
443442
if (num <= threshold && !vis[num]) {
444443
dfs(num, adj, vis);
445444
++ans;

solution/3300-3399/3378.Count Connected Components in LCM Graph/README_EN.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,7 @@ class Solution {
203203
#### C++
204204

205205
```cpp
206-
typedef struct DSU
207-
{
206+
typedef struct DSU {
208207
unordered_map<int, int> par, rank;
209208
DSU(int n) {
210209
for (int i = 0; i < n; ++i) {
@@ -234,23 +233,22 @@ typedef struct DSU
234233
if (rank[u] == rank[v]) rank[u]++;
235234
}
236235
}
237-
}DSU;
236+
} DSU;
238237

239238
class Solution {
240239
public:
241-
int countComponents(vector<int>& nums, int threshold) {
240+
int countComponents(vector<int> &nums, int threshold) {
242241
DSU dsu(threshold);
243-
for(auto& num : nums) {
242+
for (auto &num : nums) {
244243
for (int j = num; j <= threshold; j += num) {
245244
dsu.unionSet(num, j);
246245
}
247246
}
248247
unordered_set<int> par;
249-
for (auto& num : nums) {
248+
for (auto &num : nums) {
250249
if (num > threshold) {
251250
par.insert(num);
252-
}
253-
else {
251+
} else {
254252
par.insert(dsu.find(num));
255253
}
256254
}
@@ -414,29 +412,30 @@ class Solution {
414412
```cpp
415413
class Solution {
416414
private:
417-
void dfs(int node, vector<vector<int>>& adj, vector<bool>& vis) {
415+
void dfs(int node, vector<vector<int>> &adj, vector<bool> &vis) {
418416
if (vis[node]) return;
419417
vis[node] = true;
420-
for (auto& u : adj[node]) {
418+
for (auto &u : adj[node]) {
421419
dfs(u, adj, vis);
422420
}
423421
}
422+
424423
public:
425-
int countComponents(vector<int>& nums, int threshold) {
424+
int countComponents(vector<int> &nums, int threshold) {
426425
vector<vector<int>> adj(threshold + 1);
427426
vector<bool> vis(threshold + 1, false);
428427
int ans = 0;
429-
for (auto& num : nums) {
428+
for (auto &num : nums) {
430429
if (num > threshold) {
431430
++ans;
432431
continue;
433432
}
434-
for (int j = 2*num; j <= threshold; j += num) {
433+
for (int j = 2 * num; j <= threshold; j += num) {
435434
adj[num].push_back(j);
436435
adj[j].push_back(num);
437436
}
438437
}
439-
for (auto& num : nums) {
438+
for (auto &num : nums) {
440439
if (num <= threshold && !vis[num]) {
441440
dfs(num, adj, vis);
442441
++ans;

solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
typedef struct DSU
2-
{
1+
typedef struct DSU {
32
unordered_map<int, int> par, rank;
43
DSU(int n) {
54
for (int i = 0; i < n; ++i) {
@@ -29,23 +28,22 @@ typedef struct DSU
2928
if (rank[u] == rank[v]) rank[u]++;
3029
}
3130
}
32-
}DSU;
31+
} DSU;
3332

3433
class Solution {
3534
public:
36-
int countComponents(vector<int>& nums, int threshold) {
35+
int countComponents(vector<int> &nums, int threshold) {
3736
DSU dsu(threshold);
38-
for(auto& num : nums) {
37+
for (auto &num : nums) {
3938
for (int j = num; j <= threshold; j += num) {
4039
dsu.unionSet(num, j);
4140
}
4241
}
4342
unordered_set<int> par;
44-
for (auto& num : nums) {
43+
for (auto &num : nums) {
4544
if (num > threshold) {
4645
par.insert(num);
47-
}
48-
else {
46+
} else {
4947
par.insert(dsu.find(num));
5048
}
5149
}

solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
class Solution {
22
private:
3-
void dfs(int node, vector<vector<int>>& adj, vector<bool>& vis) {
3+
void dfs(int node, vector<vector<int>> &adj, vector<bool> &vis) {
44
if (vis[node]) return;
55
vis[node] = true;
6-
for (auto& u : adj[node]) {
6+
for (auto &u : adj[node]) {
77
dfs(u, adj, vis);
88
}
99
}
10+
1011
public:
11-
int countComponents(vector<int>& nums, int threshold) {
12+
int countComponents(vector<int> &nums, int threshold) {
1213
vector<vector<int>> adj(threshold + 1);
1314
vector<bool> vis(threshold + 1, false);
1415
int ans = 0;
15-
for (auto& num : nums) {
16+
for (auto &num : nums) {
1617
if (num > threshold) {
1718
++ans;
1819
continue;
1920
}
20-
for (int j = 2*num; j <= threshold; j += num) {
21+
for (int j = 2 * num; j <= threshold; j += num) {
2122
adj[num].push_back(j);
2223
adj[j].push_back(num);
2324
}
2425
}
25-
for (auto& num : nums) {
26+
for (auto &num : nums) {
2627
if (num <= threshold && !vis[num]) {
2728
dfs(num, adj, vis);
2829
++ans;

0 commit comments

Comments
 (0)