-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ1068_0.java
More file actions
55 lines (46 loc) · 1.36 KB
/
Copy pathJ1068_0.java
File metadata and controls
55 lines (46 loc) · 1.36 KB
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
package TIL;
import java.io.*;
import java.util.*;
public class J1068_0 {
static int n;
static int[] array;
static boolean[] visited;
static int cnt = 0;
public static void main(String[] args) throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(buffer.readLine());
array = new int[n];
visited = new boolean[n];
int root = 0;
String[] input = buffer.readLine().split(" ");
for(int i = 0; i< n; i++){
array[i] = Integer.parseInt(input[i]);
if(array[i] == -1) root = i;
}
int removeNode = Integer.parseInt(buffer.readLine());
removeNode(removeNode);
findLeaf(root);
System.out.println(cnt);
}
public static void removeNode(int node){
array[node] = -2;
for(int i = 0 ; i< n; i++){
if(array[i] == node){
removeNode(i);
}
}
}
public static void findLeaf(int node){
boolean check = true;
visited[node] = true;
if(array[node] != -2) {
for (int i = 0; i < n; i++) {
if (array[i] == node && !visited[i]) {
findLeaf(i);
check = false;
}
}
if(check) cnt++;
}
}
}