-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathcount-the-number-of-complete-components.cpp
45 lines (43 loc) · 1.23 KB
/
count-the-number-of-complete-components.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
36
37
38
39
40
41
42
43
44
45
// Time: O(n)
// Space: O(n)
// bfs
class Solution {
public:
int countCompleteComponents(int n, vector<vector<int>>& edges) {
vector<bool> lookup(n);
vector<vector<int>> adj(n);
for (const auto& e : edges) {
adj[e[0]].emplace_back(e[1]);
adj[e[1]].emplace_back(e[0]);
}
const auto& bfs = [&](int u) {
if (lookup[u]) {
return false;
}
int v_cnt = 0, e_cnt = 0;
lookup[u] = true;
vector<int> q = {u};
while (!empty(q)) {
vector<int> new_q;
v_cnt += size(q);
for (const auto& u : q) {
e_cnt += size(adj[u]);
for (const auto& v : adj[u]) {
if (lookup[v]) {
continue;
}
lookup[v] = true;
new_q.emplace_back(v);
}
}
q = move(new_q);
}
return v_cnt * (v_cnt - 1) == e_cnt;
};
int result = 0;
for (int u = 0; u < n; ++u) {
result += bfs(u);
}
return result;
}
};