-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_04.java
More file actions
35 lines (35 loc) · 982 Bytes
/
Copy pathDay_04.java
File metadata and controls
35 lines (35 loc) · 982 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
// Number of Provinces
class Solution {
public void dfs(int node,int[][] isConnected,List<List<Integer>> adj,boolean[] vis){
int n = isConnected.length;
vis[node] = true;
for(var it : adj.get(node)){
if(!vis[it]){
dfs(it,isConnected,adj,vis);
}
}
}
public int findCircleNum(int[][] isConnected) {
int n = isConnected.length;
List<List<Integer>> adj = new ArrayList<>();
for(int i=0;i<n;i++){
adj.add(new ArrayList<>());
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(isConnected[i][j] == 1 && i!=j){
adj.get(i).add(j);
}
}
}
int cnt = 0;
boolean[] vis = new boolean[n];
for(int i=0;i<n;i++){
if(!vis[i]){
cnt++;
dfs(i,isConnected,adj,vis);
}
}
return cnt;
}
}