-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cpp
35 lines (35 loc) · 974 Bytes
/
Solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution {
public:
int countCompleteComponents(int n, vector<vector<int>>& edges) {
vector<vector<int>> g(n);
bool vis[n];
memset(vis, false, sizeof(vis));
for (auto& e : edges) {
int a = e[0], b = e[1];
g[a].push_back(b);
g[b].push_back(a);
}
function<pair<int, int>(int)> dfs = [&](int i) -> pair<int, int> {
vis[i] = true;
int x = 1, y = g[i].size();
for (int j : g[i]) {
if (!vis[j]) {
auto [a, b] = dfs(j);
x += a;
y += b;
}
}
return make_pair(x, y);
};
int ans = 0;
for (int i = 0; i < n; ++i) {
if (!vis[i]) {
auto [a, b] = dfs(i);
if (a * (a - 1) == b) {
++ans;
}
}
}
return ans;
}
};