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;
int n,d;
const int maxn=200000+10;
int dp[maxn];
vector<int>adj[maxn];
vector<pair<int,int>>alln;
void pre(int u=0,int len=0,int par=-1){
alln.push_back(make_pair(len,u));
for(auto x:adj[u]){
if(x!=par){
pre(x,len+1,u);
}
}
}
int res=0;
void upd(int u,int len,int par=-1){
if(len<0||len<=dp[u]){
return ;
}
dp[u]=len;
for(auto x:adj[u]){
if(x!=par){
upd(x,len-1,u);
}
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>d;
d--;
for(int i=1;i<=n-1;i++){
int f;
cin>>f;
adj[f].push_back(i);
adj[i].push_back(f);
}
for(int i=0;i<n;i++){
dp[i]=-1;
}
pre();
sort(alln.rbegin(),alln.rend());
for(int i=0;i<n;i++){
//cout<<alln[i].first<<" "<<alln[i].second<<" "<<dp[alln[i].second]<<"\n";
if(dp[alln[i].second]==-1){
res++;
upd(alln[i].second,d);
}
}
cout<<res<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |