제출 #547780

#제출 시각아이디문제언어결과실행 시간메모리
547780Soumya1Cat in a tree (BOI17_catinatree)C++17
0 / 100
4 ms5028 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, int p) {
  dp[u] = {0, 1e9};
  for (int v : ad[u]) {
    if (v == p) continue;
    dfs(v, u);
    auto [cnt, dd] = dp[v];
    if (dd + 1 + dp[u].second >= d) dp[u].first += cnt, dp[u].second = min(dp[u].second, dd + 1);
    else dp[u].first += cnt - 1;
  }
  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, -1);
  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...