-
Notifications
You must be signed in to change notification settings - Fork 0
/
2685.cpp
65 lines (44 loc) · 1.12 KB
/
2685.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<int> *g;
vector<int> vis;
int nv = 0;
void dfs(int i){
vis[i] = 1;
++nv;
for(auto v : g[i]){
if(vis[v] != 0) continue;
dfs(v);
}
return;
}
void dfs_c(int i, bool &c){
vis[i] = 2;
if(c == false) return;
if(g[i].size() != nv-1){ c = false; return; }
for(auto v : g[i]){
if(vis[v] == 2) continue;
dfs_c(v, c);
}
return;
}
int countCompleteComponents(int n, vector<vector<int>>& e) {
vector<int> gr[n];
for(auto v : e) gr[v[0]].push_back(v[1]), gr[v[1]].push_back(v[0]);
g = gr;
vis.resize(n, 0);
int res = 0;
for(int i = 0; i < n; ++i){
if(vis[i] != 0) continue;
dfs(i);
//cout << nv << "\n";
bool c = true;
dfs_c(i, c);
if(c) ++res;
nv = 0;
}
return res;
}
};