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 main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, d;
    cin >> n >> d;
    vector<vector<int>> g (n);
    vector<int> o (n, 0);
    for (int i = 1; i < n; i++) {
        int x;
        cin >> x;
        g[x].push_back(i);
        o[i] = x;
    }
    vector<pair<int, int>> dp (n, make_pair(0, 1000000));
    for (int i = n-1; i >= 0; i--) {
        int s = 0, naj = 1000000;
        for (int v : g[i]) {
            if (dp[v].first > 0) {
                if (naj+dp[v].second >= d) {
                    s += dp[v].first;
                    naj = min(naj, dp[v].second);
                }
                else {
                    s += dp[v].first-1;
                    naj = max(naj, dp[v].second);
                }
            }
        }
        if (naj >= d) {
            dp[i] = make_pair(s+1, 1);
        }
        else {
            dp[i] = make_pair(s, naj+1);
        }
    }
    cout << dp[0].first;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |