제출 #547786

#제출 시각아이디문제언어결과실행 시간메모리
547786Soumya1Cat in a tree (BOI17_catinatree)C++17
100 / 100
71 ms25020 KiB
#include <bits/stdc++.h>
#ifdef __LOCAL__
#include <debug_local.h>
#endif
using namespace std;
const int mxN = 2e5 + 5;
vector<int> ad[mxN];
pair<int, int> dp[mxN];
int n, d;
void dfs(int u) {
  dp[u] = {0, d};
  int s = 0;
  vector<int> vv;
  for (int v : ad[u]) {
    dfs(v);
    s += dp[v].first;
    vv.push_back(dp[v].second + 1);
  }
  sort(vv.begin(), vv.end());
  // debug(vv);
  for (int i = 0; i < (int) vv.size(); i++) {
    int j = lower_bound(vv.begin(), vv.end(), d - vv[i]) - vv.begin();
    int cnt;
    if (j > i) {
      cnt = j - 1;
    } else {
      cnt = i;
    }
    dp[u] = max(dp[u], {s - cnt, vv[i]});
  }
  if (dp[u].second >= d) dp[u].first++, dp[u].second = 0;
  // debug(u, dp[u]);
}
void testCase() {
  cin >> n >> d;
  for (int i = 1; i < n; i++) {
    int p;
    cin >> p;
    ad[p].push_back(i);
  }
  dfs(0);
  cout << dp[0].first << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  testCase();
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...