문제

programmers 네트워크 문제 보러가기



풀이

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])
				continue;
			
			for (int j = 0; j < n; j++) {
				if (computers[i][j] == 1 && !visited[j]) {
					visited[i] = true;
					answer += dfs(i, 1, n, computers);
				}
			}
		}
		return n - answer == 0 ? 1 : answer;
	}

	static int dfs(int node, int network, int n, int[][] computers) {
		for (int i = 0; i < n; i++) {
			if (!visited[i] && computers[i][node] == computers[node][i] && computers[i][node] == 1) {
				visited[i] = true;
				dfs(i, network+1, n, computers);
			}
		}
		return network;
	}
}