-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumber_of_provinces.cpp
More file actions
44 lines (43 loc) · 1011 Bytes
/
Number_of_provinces.cpp
File metadata and controls
44 lines (43 loc) · 1011 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
34
35
36
37
38
39
40
41
42
43
44
class Solution {
public:
void dfs(int node, vector<int> adjLis[],vector<int> &vis)
{
vis[node]=1;
for(int i: adjLis[node])
{
if(!vis[i])
{
vis[i]=1;
dfs(i,adjLis,vis);
}
}
}
int findCircleNum(vector<vector<int>>& isConnected) {
int n = isConnected.size();
vector<int> adjLis[n];
vector<int> vis(n+1,0);
for(int i = 0;i<n;i++)
{
for(int j = 0;j<n;j++)
{
if(isConnected[i][j]==1)
{
adjLis[i].push_back(j);
adjLis[j].push_back(i);
}
}
}
int cnt = 0;
for(int i = 0;i<n;i++)
{
if(!vis[i])
{
cnt++;
dfs(i,adjLis,vis);
}
}
return cnt;
}
};
//Using dfs to traverse all the nodes
//Solution submitted on leetcode