1. make
public static void make() {
parents = new int[N+1];
for (int i = 1; i <= N; i++) {
parents[i] = i;
}
}
2. find
private static int find(int v) {
if (parents[v] == v) {
return v;
}
return parents[v] = find(parents[v]);
}
3. union
public static boolean union(int a, int b) {
int aRoot = find(a);
int bRoot = find(b);
if (aRoot == bRoot) {
return false;
}
if (aRoot < bRoot) {
parents[bRoot] = aRoot;
} else {
parents[aRoot] = bRoot;
}
return true;
}
'Algorithm Study > java' 카테고리의 다른 글
| 프로그래머스 - [3차] 압축 (0) | 2024.04.02 |
|---|---|
| [프로그래머스] 전략망을 둘로 나누기 (풀이 : union-find & dfs) (0) | 2024.03.20 |
| [프로그래머스] 메뉴 리뉴얼 - 2021 KAKAO BLIND RECRUITMENT (0) | 2024.03.19 |
| [백준 - Java] 2565 - 전깃줄 (0) | 2024.02.20 |