-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCount_ProvinceGraph.java
More file actions
36 lines (35 loc) · 950 Bytes
/
Count_ProvinceGraph.java
File metadata and controls
36 lines (35 loc) · 950 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
class Solution {
public int findCircleNum(int[][] isConnected) {
int m =isConnected.length;
int n =isConnected[0].length;
List<List<Integer>> adj = new ArrayList<>();
for(int i =0 ; i<m ;i++){
adj.add(new ArrayList());
}
for(int i =0;i<m;i++){
for(int j =0 ;j< m ;j++){
if(i==j) continue;
if(isConnected[i][j] ==1){
adj.get(i).add(j);
}
}
}
int[] vis = new int[m];
int count =0;
for(int i = 0; i< m;i++){
if(vis[i] != 1){
count++;
dfs(i , adj , vis);
}
}
return count;
}
void dfs(int v , List<List<Integer>> list , int[] vis){
vis[v] =1;
for(int i : list.get(v)){
if(vis[i] != 1){
dfs(i , list , vis);
}
}
}
}