-
Notifications
You must be signed in to change notification settings - Fork 0
/
P1068.cpp
71 lines (50 loc) · 1.05 KB
/
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
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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<int> graph[51];
int check[51] = {};
int root = 0;
int children = 0;
int result = 0;
void bfs(int start) {
queue<int> q;
q.push(start);
check[start] = 1;
while(!q.empty()) {
int front = q.front();
q.pop();
children = 0;
for(int i = 0; i < graph[front].size(); i++) {
int node = graph[front][i];
if(check[node]) continue;
children++;
check[node] = 1;
q.push(node);
}
if(children == 0) result++;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N;
cin >> N;
for(int i = 0; i < N; i++) {
int v;
cin >> v;
if(v == -1) {
root = i;
} else {
graph[i].push_back(v);
graph[v].push_back(i);
}
}
int remove;
cin >> remove;
check[remove] = 1;
if(remove != root) bfs(root);
cout << result;
return 0;
}