알고리즘/Programmers
자바 | 프로그래머스 | 네트워크
cha-n
2021. 5. 28. 19:03
https://programmers.co.kr/learn/courses/30/lessons/43162
Solution
그냥 dfs돌리면 된다..
class Solution {
static boolean[] visited;
public int solution(int n, int[][] computers) {
int answer = 0;
visited = new boolean[n];
for (int i=0;i<n;i++){
if (!visited[i]) {
dfs(i, computers);
answer++;
}
}
return answer;
}
void dfs(int x, int[][] computers){
visited[x] = true;
for (int i=0;i<computers[x].length;i++){
if (!visited[i] && computers[x][i]==1){
dfs(i, computers);
}
}
}
}