This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std ;
const int MAX = 2e5 + 10 ;
int dep[MAX] ;
int n , d ;
vector< vector<int> >adj(MAX) ;
int ans = 0 ;
int dfs(int node)
{
vector<int>v ;
ans++ ;
v.push_back(dep[node]) ;
for(auto &child : adj[node])
{
dep[child] = dep[node] + 1 ;
v.push_back(dfs(child)) ;
}
sort(v.begin() , v.end()) ;
reverse(v.begin() , v.end()) ;
while(v.size() >= 2 && v[v.size()-1] + v[v.size()-2] - 2*dep[node] < d)
v.pop_back() , ans-- ;
return v.back() ;
}
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n>>d ;
for(int i = 2 ; i <= n ; ++i)
{
int p ;
cin>>p ;
adj[p+1].push_back(i) ;
}
dfs(1) ;
return cout<<ans<<"\n" , 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... |