#include "bits/stdc++.h"
using namespace std;
#define SZ(s) (int)s.size()
#define ss second
int n, d, mn, cnt;
vector <int> h, p, vis, v1;
vector <vector <int>> v, dp, sp;
void dfs(int x, int y){
p[x] = y;
for(auto i : v[x]){
if(i == y) continue;
h[i] = h[x]+1;
dfs(i,x);
}
}
void f(int x, int y, int dis){
if(vis[x]) mn = min(mn, dis);
for(auto i : v[x]){
if(i == y) continue;
f(i,x,dis+1);
}
}
int main(){
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> d;
v.resize(n), h.assign(n, 0), p.assign(n+1, n+1), vis.assign(n,0);
for(int i = 1; i < n; i++){
int x;
cin >> x;
v[i].push_back(x);
v[x].push_back(i);
}
dfs(0,0);
vector <pair<int,int>> v1;
for(int i = 0; i < n; i++){
v1.push_back({h[i],i});
}
sort(v1.rbegin(), v1.rend());
int ans = 0;
for(int i = 0; i < n; i++){
int x = v1[i].ss;
mn = 1e9;
f(x,x,0);
if(mn < d) continue;
vis[x] = true;
ans++;
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |