-
Notifications
You must be signed in to change notification settings - Fork 6
/
JIHANEOL.java
57 lines (46 loc) · 1.27 KB
/
JIHANEOL.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.io.*;
import java.util.*;
public class JIHANEOL{
static BufferedReader br;
static StringTokenizer st;
static int G,P,parents[];
static boolean[] docking;
public static void main(String[] args) throws Exception{
br = new BufferedReader(new InputStreamReader(System.in));
G = Integer.parseInt(br.readLine());
P = Integer.parseInt(br.readLine());
docking = new boolean[G+1];
int answer =0;
parents= new int[G+1];
for(int i=1; i<=G; i++) {
parents[i]=i;
}
for(int i=0; i<P; i++) {
int num = Integer.valueOf(br.readLine());
if(!docking[find(num)]) { // false 면 아직 안갔으니
docking[find(num)] = true;
if(find(num)>1) {
union(find(find(num)-1), find(num));
}
answer++;
}else {
break;
}
// num이 0보다 크고 도킹이 false를 만날때까지 찾는다.
}
System.out.println(answer);
}
public static int find(int x) {
if(x==parents[x]) {
return x;
}
return parents[x]= find(parents[x]);
}
public static void union(int a, int b) {
a = find(a);
b= find(b);
if(a!=b) {
parents[b]=a;
}
}
}