제출 #1296192

#제출 시각아이디문제언어결과실행 시간메모리
1296192avighnaCat in a tree (BOI17_catinatree)C++20
0 / 100
1 ms576 KiB
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, d;
  cin >> n >> d;
  vector<vector<int>> adj(n);
  for (int i = 1, p; i < n; ++i) {
    cin >> p;
    adj[p].push_back(i);
  }

  if (d >= n) {
    cout << "1\n";
    return 0;
  }

  vector<int> sz(n);
  auto dfs_sz = [&](auto &&self, int u) -> void {
    sz[u] = 1;
    for (int &i : adj[u]) {
      self(self, i);
      sz[u] += sz[i];
    }
  };
  dfs_sz(dfs_sz, 0);

  auto dfs = [&](auto &&self, int u) -> deque<int> {
    deque<int> dp(sz[u]);
    for (int &i : adj[u]) {
      deque<int> ndp = self(self, i);
      ndp.push_front(0);
      deque<int> nndp(sz[u]);
      for (int i = 0; i < ndp.size(); ++i) {
        nndp[i] = max(dp[i], ndp[i]);
      }
      for (int i = 0; i < dp.size(); ++i) {
        for (int j = 0; j < ndp.size(); ++j) {
          if (i + j < d) {
            continue;
          }
          nndp[min(i, j)] = max(nndp[min(i, j)], dp[i] + ndp[j]);
        }
      }
      dp = nndp;
    }
    dp[0] = max(dp[0], (d < dp.size() ? dp[d] : 0) + 1);
    return dp;
  };

  deque<int> dp = dfs(dfs, 0);
  cout << *max_element(dp.begin(), dp.end()) << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...