-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP1068.cpp
44 lines (43 loc) · 908 Bytes
/
P1068.cpp
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
#include <iostream>
using namespace std;
int main() {
// [parent][child]
bool tree[50][50] = {0, };
int nodes;
cin >> nodes;
int root = -1;
for (int i = 0; i < nodes; ++i) {
int parent;
cin >> parent;
if (parent >= 0) {
tree[parent][i] = true;
} else {
root = i;
}
}
int remove;
cin >> remove;
int stack[50];
stack[0] = root;
int top = 1;
int leaves = 0;
if (remove == root) {
cout << "0";
return 0;
}
while (top) {
int current = stack[--top];
bool found = false;
for (int i = 0; i < nodes; ++i) {
if (tree[current][i] && i != remove) {
found = true;
stack[top] = i;
++top;
}
}
if (!found) {
++leaves;
}
}
cout << leaves;
}