File tree 1 file changed +38
-0
lines changed
1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments