-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path547.number-of-provinces.cpp
36 lines (28 loc) · 1.03 KB
/
547.number-of-provinces.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
class Solution {
public:
int findCircleNum(vector<vector<int>>& isConnected) {
vector<int> visited(isConnected.size());
int count = 0;
auto startNode = find(visited.begin(), visited.end(), false);
while(startNode != visited.end()){
queue<int> q;
q.push(startNode - visited.begin());
while(!q.empty()){
int node = q.front();
q.pop();
// cout<<"BFS for : "<<node<<endl;
visited[node] = true;
for(int i=0; i<isConnected[0].size(); i++)
if(isConnected[node][i] == 1 && !visited[i] && i!= node)
q.push(i);
}
// for(auto k:visited)
// cout<<k<<"\t";
// cout<<endl;
// cout<<endl;
count++;
startNode = find(visited.begin(), visited.end(), false);
}
return count;
}
};