Language: C++

#include <iostream>
#include <cassert>
#include <climits>
#include <bitset>
#include <stack>
#include <queue>
#include <iomanip>
#include <limits>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string.h>
#include <tuple>

using namespace std;

typedef long long ll;
typedef pair<ll,ll> P;

long long int INF = 1e18;
long long int mod = 1000000007;
double Pi = 3.1415926535897932384626;

vector<ll> G[500005];
vector<P> tree[500010];
priority_queue <ll> pql;
priority_queue <P> pqp;
//big priority queue
priority_queue <ll,vector<ll>,greater<ll> > pqls;
priority_queue <P,vector<P>,greater<P> > pqps;
//small priority queue
//top pop

int dx[8]={1,0,-1,0,1,1,-1,-1};
int dy[8]={0,1,0,-1,1,-1,-1,1};
char dir[] = "RULD";
//↓,→,↑,←

#define p(x) cout<<x<<endl;
#define el cout<<endl;
#define pe(x) cout<<x<<" ";
#define ps(x) cout<<fixed<<setprecision(25)<<x<<endl;
#define pu(x) cout<<x;
#define re(i,n) for(i=0;i<n;i++);
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define deba(x) cout<< #x << " = " << x <<endl

ll rui(ll number1,ll number2){
    if(number2 == 0){
        return 1;
    }else{
        ll number3 = rui(number1,number2 / 2);
        number3 *= number3;
        number3 %= mod;
        if(number2%2==1){
            number3 *= number1;
            number3 %= mod;
        }
        return number3;
    }
}
ll gcd(ll number1,ll number2){
    if(number1 > number2){
        swap(number1 , number2);
    }
    if(number1 == 0 || number1 == number2){
        return number2;
    }else{
        return gcd(number2 % number1 , number1);
    }
}

ll i,j,k,ii,jj,n,m,a,b,c,d,e,g,h,w,num,sum,ans;
ll x[500005],y[500005],z[500005];
ll l[500005],r[500005];
ll dp[100005][105];
char s[500005],t[500005];

ll dfs(ll v,ll p,ll cost){
    ll ans1 = 0,ans2 = 0;
    if(cost <= 1000 && dp[v][cost] != -1){
        return dp[v][cost];
    }

    if(cost > k){
        ans2 = 1;
        for(int i=0;i<G[v].size();i++){
            if(G[v][i] == p)continue;
            ans2 += dfs(G[v][i],v,2);
        }
       // pe(v);pe("cost > k");p(ans2);
        dp[v][cost] = ans2;
        return ans2;
    }else{
        for(int i=0;i<G[v].size();i++){
            if(G[v][i] == p)continue;
            ans1 += dfs(G[v][i],v,cost + 1);
        }
        if(ans1 == 0 || cost <= 1){
            
           // pe(v);pe("ans1 == 0 || cost <= 1")p(ans1);
            dp[v][cost] = ans1;
            return ans1;
        }else{
            ans2 = 1;
            for(int i=0;i<G[v].size();i++){
                if(G[v][i] == p)continue;
                ans2 += dfs(G[v][i],v,2);
            }
           // pe(v);pe("min(ans1,ans2)");p(min(ans1,ans2));
            dp[v][cost] = min(ans1,ans2);
            return min(ans1,ans2);
        }
    }
}



int main(){
    cin >> n >> k;
    for(i=0;i<100000;i++){
        for(j=0;j<=100:j++){
            dp[i][j] = -1;
        }
    }
    if(k >= n){
        cin >> a;
        if(a == 1){
            p(0);
        }else{
            p(1);
        }
        return 0;
    }else{
        for(i=0;i<n;i++){
            cin >> x[i];
        }
        if(x[0] != 1){
            ans++;
            x[0] = 1;
        }
        for(i=1;i<n;i++){
            G[x[i]].pb(i+1);
        }
       p(dfs(1,-1,0) + ans);
    }
    return 0;
}





Compiler output:
a.cc: In function 'int main()':
a.cc:128:17: warning: range-based 'for' loops with initializer only available with '-std=c++20' or '-std=gnu++20' [-Wc++20-extensions]
  128 |         for(j=0;j<=100:j++){
      |                 ^
a.cc:128:23: error: expected ';' before ':' token
  128 |         for(j=0;j<=100:j++){
      |                       ^
      |                       ;
a.cc:131:5: error: expected primary-expression before '}' token
  131 |     }
      |     ^
a.cc:130:10: error: expected ';' before '}' token
  130 |         }
      |          ^
      |          ;
  131 |     }
      |     ~     
a.cc:131:5: error: expected primary-expression before '}' token
  131 |     }
      |     ^
a.cc:130:10: error: expected ')' before '}' token
  130 |         }
      |          ^
      |          )
  131 |     }
      |     ~     
a.cc:128:12: note: to match this '('
  128 |         for(j=0;j<=100:j++){
      |            ^
a.cc:131:5: error: expected primary-expression before '}' token
  131 |     }
      |     ^