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<iostream>
#include<stdio.h>
#include<math.h>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#include<string.h>
#include<utility>
#include<set>
#include<cmath>
#include<queue>
#include<deque>
#include<functional>
#include<stack>
#include<limits.h>
#include<iomanip>
#include<unordered_map>
#include<numeric>
#include<tuple>
#include<bitset>
using namespace std;
#define MOD 1000000007
#define int long long
#define ss second
#define ff first
#define endl '\n'
int n,d;
const int mxN=2e5+1;
vector<int> v[mxN];
vector<vector<int> > dp(mxN,vector<int>(2));
void dfs(int a, int p){
int mx=1e9,cnt=0;
for(auto node: v[a]){
if(node!=p){
dfs(node,a);
if(mx+dp[node][1]>=d){
cnt=cnt+dp[node][0];
mx=min(mx,dp[node][1]);
}else{
cnt=cnt+dp[node][0]-1;
mx=max(mx,dp[node][1]);
}
}
}
if(mx>=d){
dp[a][1]=1;
dp[a][0]=cnt+1;
}else{
dp[a][0]=cnt;
dp[a][1]=mx+1;
}
}
void solve(){
cin>>n>>d;
for(int i=1; i<n; i++){
int a; cin>>a;
v[a].push_back(i);
v[i].push_back(a);
}
dfs(0,0);
cout<<dp[0][0];
}
signed main(){
// freopen("file.in", "r", stdin);
// freopen("file.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T=1;
// cin>>T;
while(T--){
solve();
cout<<endl;
}
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... |