File tree Expand file tree Collapse file tree 3 files changed +144
-0
lines changed
solution/2600-2699/2685.Count the Number of Complete Components Expand file tree Collapse file tree 3 files changed +144
-0
lines changed Original file line number Diff line number Diff line change @@ -234,4 +234,64 @@ func countCompleteComponents(n int, edges [][]int) (ans int) {
234
234
235
235
<!-- solution: end -->
236
236
237
+ <!-- solution: start -->
238
+
239
+ ### 方法二:取巧做法
240
+
241
+ > [ 我的题解] ( https://leetcode.cn/problems/count-the-number-of-complete-components/solutions/2269282/zui-jian-ji-jie-fa-c-stlzuo-fa-by-dengsc-rw5e/ )
242
+
243
+ 要解决的问题:
244
+
245
+ 1 . 如何保存每一个节点与其它点联通状态
246
+ 2 . 如何判断多个点是否是一个联通图
247
+
248
+ 对于第一点:实际上就是保存了当前到每个点的联通点集合(包括自己),方便后续判等。
249
+ 第二点:有了第一点之后,如果是连通图中的点就有:
250
+
251
+ 1 . 此点包含此联通图中所有的点(包括自己)
252
+ 2 . 并且只包含此联通图中的点
253
+
254
+ 拿示例一举例:
255
+
256
+ - 5包含的联通点有且只有自己,所以是连通图
257
+ - 0包含0、1、2,同理1、2点也是
258
+ - 3和4也是包含自己和彼此
259
+ - 基于以上就有以下代码实现:
260
+
261
+ <!-- tabs: start -->
262
+
263
+ #### C++
264
+
265
+ ``` c++
266
+ class Solution {
267
+ public:
268
+ int countCompleteComponents(int n, vector<vector<int >>& edges) {
269
+ int ans = 0;
270
+ vector<set<int >> m(n + 1, set<int >());
271
+ for (int i = 0; i < n; i++) {
272
+ m[ i] .insert(i);
273
+ }
274
+ for (auto x : edges) {
275
+ m[ x[ 0]] .insert(x[ 1] );
276
+ m[ x[ 1]] .insert(x[ 0] );
277
+ }
278
+ map<set<int >, int> s;
279
+ for (int i = 0; i < n; i++) {
280
+ s[ m[ i]] ++;
281
+ }
282
+ for (auto &[ x,y] : s) {
283
+ if (y == x.size()) {
284
+ ans ++;
285
+ }
286
+ }
287
+ return ans;
288
+ }
289
+ };
290
+ // cost:283ms、beats 5%.
291
+ ```
292
+
293
+ <!-- tabs:end -->
294
+
295
+ <!-- solution:end -->
296
+
237
297
<!-- problem:end -->
Original file line number Diff line number Diff line change @@ -222,4 +222,64 @@ func countCompleteComponents(n int, edges [][]int) (ans int) {
222
222
223
223
<!-- solution: end -->
224
224
225
+ <!-- solution: start -->
226
+
227
+ ### 方法二:Simple Method
228
+
229
+ > [ My Solution Link] ( https://leetcode.cn/problems/count-the-number-of-complete-components/solutions/2269282/zui-jian-ji-jie-fa-c-stlzuo-fa-by-dengsc-rw5e/ )
230
+
231
+ Problems needed to solve:
232
+
233
+ 1 . How do we maintain the link state between each node and the others? 如
234
+ 2 . How can one determine whether multiple points form a connected graph?
235
+
236
+ For the first one: we can maintain each node's connection set(including itself).
237
+
238
+ For the second one: After solving the first one, we can see:
239
+
240
+ - the node itself includes every node in the connected graph(including itself).
241
+ - and only connected to the nodes in the connected graph.
242
+
243
+ Take example 1 to explain:
244
+
245
+ - Node 5's connected node is itself, so it is a connected graph.
246
+ - Node 0's connected 0, 1, 2. Same as nodes 1, 2.
247
+ - Nodes 3 and 4 also include themselves and each other.
248
+
249
+ <!-- tabs: start -->
250
+
251
+ #### C++
252
+
253
+ ``` c++
254
+ class Solution {
255
+ public:
256
+ int countCompleteComponents(int n, vector<vector<int >>& edges) {
257
+ int ans = 0;
258
+ vector<set<int >> m(n + 1, set<int >());
259
+ for (int i = 0; i < n; i++) {
260
+ m[ i] .insert(i);
261
+ }
262
+ for (auto x : edges) {
263
+ m[ x[ 0]] .insert(x[ 1] );
264
+ m[ x[ 1]] .insert(x[ 0] );
265
+ }
266
+ map<set<int >, int> s;
267
+ for (int i = 0; i < n; i++) {
268
+ s[ m[ i]] ++;
269
+ }
270
+ for (auto &[ x,y] : s) {
271
+ if (y == x.size()) {
272
+ ans ++;
273
+ }
274
+ }
275
+ return ans;
276
+ }
277
+ };
278
+ // cost:283ms、beats 5%.
279
+ ```
280
+
281
+ <!-- tabs:end -->
282
+
283
+ <!-- solution:end -->
284
+
225
285
<!-- problem:end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int countCompleteComponents (int n, vector<vector<int >>& edges) {
4
+ int ans = 0 ;
5
+ vector<set<int >> m (n + 1 , set<int >());
6
+ for (int i = 0 ; i < n; i++) {
7
+ m[i].insert (i);
8
+ }
9
+ for (auto x : edges) {
10
+ m[x[0 ]].insert (x[1 ]);
11
+ m[x[1 ]].insert (x[0 ]);
12
+ }
13
+ map<set<int >, int > s;
14
+ for (int i = 0 ; i < n; i++) {
15
+ s[m[i]] ++;
16
+ }
17
+ for (auto &[x,y] : s) {
18
+ if (y == x.size ()) {
19
+ ans ++;
20
+ }
21
+ }
22
+ return ans;
23
+ }
24
+ };
You can’t perform that action at this time.
0 commit comments