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