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