-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1068.java
More file actions
78 lines (75 loc) · 1.41 KB
/
1068.java
File metadata and controls
78 lines (75 loc) · 1.41 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package javaBasic;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;
class Lf{
int element;
Lf prt = null;
int cnt = 0;
ArrayList<Lf> child = new ArrayList<Lf>();
public Lf(int ele) {
element = ele;
}
public void add(Lf lf) {
child.add(lf);
}
public void remove(int ele) {
int idx = 0;
for(Lf item: child) {
if(item.element==ele) {
break;
}
idx++;
}
child.remove(idx);
}
}
class LfTree{
int root = 0;
Lf []arr = new Lf[52];
public LfTree() {
for(int i = 0; i < 52; i++) {
arr[i] = new Lf(0);
}
}
public int cntCd() {
int cnt = 0;
Stack<Lf> stk = new Stack<>();
stk.add(arr[root]);
while(!stk.empty()) {
Lf temp = stk.pop();
if(temp.child.size()==0) cnt++;
for(Lf item : temp.child) {
stk.add(item);
}
}
return cnt;
}
}
public class Test020 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
LfTree tree = new LfTree();
int n = sc.nextInt();
int total = 0;
for(int i = 0; i < n; i++) {
int prt = sc.nextInt();
if(prt==-1) {
tree.arr[i].element = i;
tree.root = i;
}
else {
tree.arr[i].element = i;
tree.arr[prt].add(tree.arr[i]);
tree.arr[i].prt = tree.arr[prt];
}
}
int rm = sc.nextInt();
if(tree.arr[rm].prt==null)System.out.println(0);
else {
tree.arr[rm].prt.remove(rm);
System.out.println(tree.cntCd());
}
}
}