Skip to content

Commit 846615a

Browse files
committedDec 27, 2020
feat(boj): boj 10775 공항 - disjoint set
1 parent ce8ef58 commit 846615a

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
 

‎BOJ/boj_10775.java

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// boj 10775 공항
2+
// disjoint set, union-find
3+
4+
import java.io.BufferedReader;
5+
import java.io.InputStreamReader;
6+
import java.util.StringTokenizer;
7+
8+
public class boj_10775 {
9+
static int g, p, ans = 0;
10+
// parent[i] : i번째 게이트(자식노드)까지 도킹 가능할 때, 실제로 도킹 가능한 게이트 중 번호가 가장 높은 게이트(루트노드).
11+
static int[] parent;
12+
13+
14+
public static void main(String[] args) throws Exception{
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
g = stoi(br.readLine());
17+
p = stoi(br.readLine());
18+
parent = new int [g + 1];
19+
for (int i = 1; i <= g; ++i) parent[i] = i;
20+
while (p-- > 0) {
21+
int now = stoi(br.readLine());
22+
// 실제로 도킹할 게이트(루트노드) 찾기
23+
now = find(now);
24+
if (now <= 0) break; // 더이상 도킹할 곳이 없음
25+
++ans;
26+
parent[now] = now - 1;
27+
}
28+
System.out.print(ans);
29+
}
30+
31+
static int stoi(String s) {return Integer.parseInt(s);}
32+
static int find(int child) {
33+
if (child == parent[child]) return child;
34+
return parent[child] = find(parent[child]);
35+
}
36+
37+
38+
}

0 commit comments

Comments
 (0)
Please sign in to comment.