-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1068.cpp
More file actions
42 lines (35 loc) · 745 Bytes
/
1068.cpp
File metadata and controls
42 lines (35 loc) · 745 Bytes
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int root, del, leaf;
vector<int> v[51];
int dfs(int node){
if(node == del)
return -1;
if(!v[node].size()){
leaf++;
return 0;
}
for(int i = 0; i < v[node].size(); i++){
int tmp = dfs(v[node][i]);
if(tmp == -1 && v[node].size() == 1)
leaf++;
}
return 0;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int N; cin >> N;
int num;
int idx = 0;
for(int i = 0; i < N; i++){
cin >> num;
if(num == -1)
root = i;
else v[num].push_back(i);
}
cin >> del;
dfs(root);
cout << leaf << '\n';
}