Skip to content

Commit 3583eb4

Browse files
committed
[UnionFind] programmers-네트워크
1 parent a952267 commit 3583eb4

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@
246246
| 02 | | [Baekjoon-2252 줄 세우기](./src/Graph/P2252) | [위상정렬](./notes/Graph/TopologicalSort.md) |
247247
| 03 | | [Baekjoon-4386 별자리 만들기](./src/Graph/P4386)| MST - Kruskal |
248248
| 04 | | [Baekjoon-16562 친구비](./src/Graph/P16562) | MST - Kruskal |
249+
| 05 | | [Programmers 네트워크](./src/Graph/prg43162) | |
249250

250251
## Math
251252

src/Graph/prg43162/Main.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package Graph.prg43162;
2+
3+
import java.util.HashSet;
4+
5+
public class Main {
6+
7+
public static void main(String[] args) {
8+
Solution sol = new Solution();
9+
System.out.println(sol.solution(3, new int[][]{{1, 1, 0}, {1, 1, 0}, {0, 0, 1}}));
10+
System.out.println(sol.solution(3, new int[][]{{1, 1, 0}, {1, 1, 1}, {0, 1, 1}}));
11+
}
12+
}
13+
14+
// 1 1 0
15+
// 1 1 0
16+
// 0 0 1
17+
18+
// 1 1 0
19+
// 1 1 1
20+
// 0 1 1
21+
22+
class Solution {
23+
24+
static int[] root;
25+
26+
public int solution(int n, int[][] computers) {
27+
root = new int[n];
28+
for (int i = 0; i < n; i++) root[i] = i;
29+
30+
for (int i = 0; i < n; i++) {
31+
for (int j = i+1; j < n; j++) {
32+
if (computers[i][j] == 1) merge(i, j);
33+
}
34+
}
35+
36+
HashSet<Integer> set = new HashSet<>();
37+
for (int i = 0; i < n; i++) set.add(find(i));
38+
39+
return set.size();
40+
}
41+
42+
static int find(int n) {
43+
if (root[n] == n) return n;
44+
return root[n] = find(root[n]);
45+
}
46+
47+
static void merge(int a, int b) {
48+
root[find(b)] = find(a);
49+
}
50+
}

src/Graph/prg43162/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## [programmers - 깊이/너비 우선 탐색(DFS/BFS)] 네트워크
2+
3+
![image](https://user-images.githubusercontent.com/22045163/109410733-617ab900-79e0-11eb-971c-eedfc9d6f6f9.png)

0 commit comments

Comments
 (0)