diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/README.md b/solution/3300-3399/3378.Count Connected Components in LCM Graph/README.md index f4cdfae1174c0..8e0573b4d8cce 100644 --- a/solution/3300-3399/3378.Count Connected Components in LCM Graph/README.md +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/README.md @@ -206,8 +206,7 @@ class Solution { #### C++ ```cpp -typedef struct DSU -{ +typedef struct DSU { unordered_map par, rank; DSU(int n) { for (int i = 0; i < n; ++i) { @@ -237,23 +236,22 @@ typedef struct DSU if (rank[u] == rank[v]) rank[u]++; } } -}DSU; +} DSU; class Solution { public: - int countComponents(vector& nums, int threshold) { + int countComponents(vector &nums, int threshold) { DSU dsu(threshold); - for(auto& num : nums) { + for (auto &num : nums) { for (int j = num; j <= threshold; j += num) { dsu.unionSet(num, j); } } unordered_set par; - for (auto& num : nums) { + for (auto &num : nums) { if (num > threshold) { par.insert(num); - } - else { + } else { par.insert(dsu.find(num)); } } @@ -417,29 +415,30 @@ class Solution { ```cpp class Solution { private: - void dfs(int node, vector>& adj, vector& vis) { + void dfs(int node, vector> &adj, vector &vis) { if (vis[node]) return; vis[node] = true; - for (auto& u : adj[node]) { + for (auto &u : adj[node]) { dfs(u, adj, vis); } } + public: - int countComponents(vector& nums, int threshold) { + int countComponents(vector &nums, int threshold) { vector> adj(threshold + 1); vector vis(threshold + 1, false); int ans = 0; - for (auto& num : nums) { + for (auto &num : nums) { if (num > threshold) { ++ans; continue; } - for (int j = 2*num; j <= threshold; j += num) { + for (int j = 2 * num; j <= threshold; j += num) { adj[num].push_back(j); adj[j].push_back(num); } } - for (auto& num : nums) { + for (auto &num : nums) { if (num <= threshold && !vis[num]) { dfs(num, adj, vis); ++ans; diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/README_EN.md b/solution/3300-3399/3378.Count Connected Components in LCM Graph/README_EN.md index 554c9e296f85f..01379722ebc6c 100644 --- a/solution/3300-3399/3378.Count Connected Components in LCM Graph/README_EN.md +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/README_EN.md @@ -203,8 +203,7 @@ class Solution { #### C++ ```cpp -typedef struct DSU -{ +typedef struct DSU { unordered_map par, rank; DSU(int n) { for (int i = 0; i < n; ++i) { @@ -234,23 +233,22 @@ typedef struct DSU if (rank[u] == rank[v]) rank[u]++; } } -}DSU; +} DSU; class Solution { public: - int countComponents(vector& nums, int threshold) { + int countComponents(vector &nums, int threshold) { DSU dsu(threshold); - for(auto& num : nums) { + for (auto &num : nums) { for (int j = num; j <= threshold; j += num) { dsu.unionSet(num, j); } } unordered_set par; - for (auto& num : nums) { + for (auto &num : nums) { if (num > threshold) { par.insert(num); - } - else { + } else { par.insert(dsu.find(num)); } } @@ -414,29 +412,30 @@ class Solution { ```cpp class Solution { private: - void dfs(int node, vector>& adj, vector& vis) { + void dfs(int node, vector> &adj, vector &vis) { if (vis[node]) return; vis[node] = true; - for (auto& u : adj[node]) { + for (auto &u : adj[node]) { dfs(u, adj, vis); } } + public: - int countComponents(vector& nums, int threshold) { + int countComponents(vector &nums, int threshold) { vector> adj(threshold + 1); vector vis(threshold + 1, false); int ans = 0; - for (auto& num : nums) { + for (auto &num : nums) { if (num > threshold) { ++ans; continue; } - for (int j = 2*num; j <= threshold; j += num) { + for (int j = 2 * num; j <= threshold; j += num) { adj[num].push_back(j); adj[j].push_back(num); } } - for (auto& num : nums) { + for (auto &num : nums) { if (num <= threshold && !vis[num]) { dfs(num, adj, vis); ++ans; diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.cpp b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.cpp index 07ef50e81967d..8436808daadc2 100644 --- a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.cpp +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.cpp @@ -1,5 +1,4 @@ -typedef struct DSU -{ +typedef struct DSU { unordered_map par, rank; DSU(int n) { for (int i = 0; i < n; ++i) { @@ -29,23 +28,22 @@ typedef struct DSU if (rank[u] == rank[v]) rank[u]++; } } -}DSU; +} DSU; class Solution { public: - int countComponents(vector& nums, int threshold) { + int countComponents(vector &nums, int threshold) { DSU dsu(threshold); - for(auto& num : nums) { + for (auto &num : nums) { for (int j = num; j <= threshold; j += num) { dsu.unionSet(num, j); } } unordered_set par; - for (auto& num : nums) { + for (auto &num : nums) { if (num > threshold) { par.insert(num); - } - else { + } else { par.insert(dsu.find(num)); } } diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.cpp b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.cpp index 9f1b31cef07a1..e398b903a990c 100644 --- a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.cpp +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.cpp @@ -1,28 +1,29 @@ class Solution { private: - void dfs(int node, vector>& adj, vector& vis) { + void dfs(int node, vector> &adj, vector &vis) { if (vis[node]) return; vis[node] = true; - for (auto& u : adj[node]) { + for (auto &u : adj[node]) { dfs(u, adj, vis); } } + public: - int countComponents(vector& nums, int threshold) { + int countComponents(vector &nums, int threshold) { vector> adj(threshold + 1); vector vis(threshold + 1, false); int ans = 0; - for (auto& num : nums) { + for (auto &num : nums) { if (num > threshold) { ++ans; continue; } - for (int j = 2*num; j <= threshold; j += num) { + for (int j = 2 * num; j <= threshold; j += num) { adj[num].push_back(j); adj[j].push_back(num); } } - for (auto& num : nums) { + for (auto &num : nums) { if (num <= threshold && !vis[num]) { dfs(num, adj, vis); ++ans;