diff --git a/solution/2600-2699/2685.Count the Number of Complete Components/README.md b/solution/2600-2699/2685.Count the Number of Complete Components/README.md index 523f109cef196..03951f996f45b 100644 --- a/solution/2600-2699/2685.Count the Number of Complete Components/README.md +++ b/solution/2600-2699/2685.Count the Number of Complete Components/README.md @@ -234,4 +234,61 @@ func countCompleteComponents(n int, edges [][]int) (ans int) { + + +### 方法二:取巧做法 + +要解决的问题: + +1. 如何保存每一个节点与其它点联通状态 +2. 如何判断多个点是否是一个联通图 + +对于第一点:实际上就是保存了当前到每个点的联通点集合(包括自己),方便后续判等。 +第二点:有了第一点之后,如果是连通图中的点就有: + +1. 此点包含此联通图中所有的点(包括自己) +2. 并且只包含此联通图中的点 + +拿示例一举例: + +- 5 包含的联通点有且只有自己,所以是连通图 +- 0 包含 0、1、2,同理 1、2 点也是 +- 3 和 4 也是包含自己和彼此 +- 基于以上就有以下代码实现: + + + +#### C++ + +```cpp +class Solution { +public: + int countCompleteComponents(int n, vector>& edges) { + int ans = 0; + vector> m(n + 1, set()); + for (int i = 0; i < n; i++) { + m[i].insert(i); + } + for (auto x : edges) { + m[x[0]].insert(x[1]); + m[x[1]].insert(x[0]); + } + map, int> s; + for (int i = 0; i < n; i++) { + s[m[i]] ++; + } + for (auto &[x,y] : s) { + if (y == x.size()) { + ans ++; + } + } + return ans; + } +}; +``` + + + + + diff --git a/solution/2600-2699/2685.Count the Number of Complete Components/README_EN.md b/solution/2600-2699/2685.Count the Number of Complete Components/README_EN.md index fb6077f895077..f0292e5585d04 100644 --- a/solution/2600-2699/2685.Count the Number of Complete Components/README_EN.md +++ b/solution/2600-2699/2685.Count the Number of Complete Components/README_EN.md @@ -222,4 +222,61 @@ func countCompleteComponents(n int, edges [][]int) (ans int) { + + +### Solution 2: Simple Method + +Problems needed to solve: + +1. How do we maintain the link state between each node and the others? 如 +2. How can one determine whether multiple points form a connected graph? + +For the first one: we can maintain each node's connection set(including itself). + +For the second one: After solving the first one, we can see: + +- the node itself includes every node in the connected graph(including itself). +- and only connected to the nodes in the connected graph. + +Take example 1 to explain: + +- Node 5's connected node is itself, so it is a connected graph. +- Node 0's connected 0, 1, 2. Same as nodes 1, 2. +- Nodes 3 and 4 also include themselves and each other. + + + +#### C++ + +```cpp +class Solution { +public: + int countCompleteComponents(int n, vector>& edges) { + int ans = 0; + vector> m(n + 1, set()); + for (int i = 0; i < n; i++) { + m[i].insert(i); + } + for (auto x : edges) { + m[x[0]].insert(x[1]); + m[x[1]].insert(x[0]); + } + map, int> s; + for (int i = 0; i < n; i++) { + s[m[i]] ++; + } + for (auto &[x,y] : s) { + if (y == x.size()) { + ans ++; + } + } + return ans; + } +}; +``` + + + + + diff --git a/solution/2600-2699/2685.Count the Number of Complete Components/Solution2.cpp b/solution/2600-2699/2685.Count the Number of Complete Components/Solution2.cpp new file mode 100644 index 0000000000000..1d9762826d9f4 --- /dev/null +++ b/solution/2600-2699/2685.Count the Number of Complete Components/Solution2.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int countCompleteComponents(int n, vector>& edges) { + int ans = 0; + vector> m(n + 1, set()); + for (int i = 0; i < n; i++) { + m[i].insert(i); + } + for (auto x : edges) { + m[x[0]].insert(x[1]); + m[x[1]].insert(x[0]); + } + map, int> s; + for (int i = 0; i < n; i++) { + s[m[i]] ++; + } + for (auto &[x,y] : s) { + if (y == x.size()) { + ans ++; + } + } + return ans; + } +};