문제링크 : https://programmers.co.kr/learn/courses/30/lessons/43162
import java.util.*;
class Solution {
public static int par[];
public static int find(int x){
if(par[x] == x) return x;
return par[x] = find(par[x]);
}
public static void union(int x, int y){
int a = find(x);
int b = find(y);
if(a!=b){
par[a] = b;
}
}
public static int solution(int n, int[][] computers) {
int answer = 0;
ArrayList<Integer> adj[] = new ArrayList[n];
par = new int[n];
for(int i=0;i<n;i++){
adj[i] = new ArrayList<Integer>();
par[i] = i;
}
for(int i=0;i<computers.length;i++){
for(int j=i+1;j<computers[i].length;j++){
if(computers[i][j] == 1){
union(i,j);
}
}
}
boolean isCounted[] = new boolean[n];
for(int i=0;i<n;i++){
int parent = find(i);
if(isCounted[parent]) continue;
isCounted[parent] = true;
answer++;
}
return answer;
}
}
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 보석 쇼핑 / JAVA (0) | 2022.02.18 |
---|---|
[프로그래머스] 순위 / JAVA (0) | 2022.02.18 |
[프로그래머스] 정수 삼각형 / JAVA (0) | 2022.02.18 |
[프로그래머스] 디스크 컨트롤러 / JAVA (0) | 2022.02.18 |
[프로그래머스] 입국심사 / JAVA (0) | 2022.02.18 |