forked from Nimesh-Srivastava/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0547.cpp
More file actions
33 lines (23 loc) · 779 Bytes
/
0547.cpp
File metadata and controls
33 lines (23 loc) · 779 Bytes
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
class Solution {
public:
void dfs(vector<vector<int>>& graph, vector<bool>& visited, int pos){
visited[pos] = true;
for(int i = 0; i < graph[pos].size(); i++){
if(pos == i)
continue;
if(graph[pos][i] && !visited[i])
dfs(graph, visited, i);
}
}
int findCircleNum(vector<vector<int>>& isConnected) {
vector<bool> visited(isConnected.size(), false);
int result = 0;
for(int i = 0; i < isConnected.size(); i++){
if(!visited[i]){
dfs(isConnected, visited, i);
result++;
}
}
return result;
}
};