public static int solution(int n, int[][] computers) {
boolean[] visited = new boolean[n];
int answer = 0;
for (int i = 0; i < n; i++) {
if (!visited[i]) {
dfs(computers, visited, i);
answer++;
}
}
return answer;
}
static void dfs(int[][] computers, boolean[] visited, int start) {
visited[start] = true;
for (int i = 0; i < computers.length; i++) {
if (computers[start][i] == 1 && !visited[i]) {
dfs(computers, visited, i);
}
}
}
출처:https://school.programmers.co.kr/learn/courses/30/lessons/43162