-
Notifications
You must be signed in to change notification settings - Fork 12
/
13.05.2024.cpp
45 lines (38 loc) · 1.03 KB
/
13.05.2024.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
class Solution {
public:
bool bfs(vector<int>adj[], int start, vector<int>&vis){
int num=0, edges=0;
queue<int>q;
q.push(start);
vis[start]=1;
while(!q.empty()){
int node=q.front();
q.pop();
num++;
edges+=adj[node].size();
for(auto it:adj[node]){
if(!vis[it]){
q.push(it);
vis[it]=1;
}
}
}
return (num*(num-1) == edges);
}
int findNumberOfGoodComponent(int e, int v, vector<vector<int>> &edges) {
// code here
vector<int>adj[v+1];
for(int i=0;i<e;i++){
adj[edges[i][0]].push_back(edges[i][1]);
adj[edges[i][1]].push_back(edges[i][0]);
}
vector<int>vis(v+1,0);
int ans=0;
for(int i=1;i<=v;i++){
if(bfs(adj, i, vis)){
ans++;
}
}
return ans;
}
};