Submission #1125487

#TimeUsernameProblemLanguageResultExecution timeMemory
1125487zNatsumiCat in a tree (BOI17_catinatree)C++20
100 / 100
124 ms29256 KiB
#include <bits/stdc++.h>

using namespace std;

using ii = pair<int, int>;
const int N = 2e5 + 5;

int n, d, depth[N];
vector<int> ad[N];
priority_queue<ii, vector<ii>, greater<>> pq[N];

void dfs(int u){
  for(auto v : ad[u]){
    depth[v] = depth[u] + 1;
    dfs(v);
    if(pq[v].size() > pq[u].size()) swap(pq[u], pq[v]);

    while(pq[u].size() && pq[v].size() && pq[u].top().first + pq[v].top().first - 2*depth[u] < d){
      if(pq[u].top().first < pq[v].top().first) pq[u].pop();
      else pq[v].pop();
    }
    while(pq[v].size()){
      pq[u].push(pq[v].top());
      pq[v].pop();
    }
  }
  if(pq[u].empty() || pq[u].top().first - depth[u] >= d) pq[u].push({depth[u], u});
}

int32_t main(){
  cin.tie(0)->sync_with_stdio(0);

  cin >> n >> d;
  for(int i = 2; i <= n; i++){
    int p; cin >> p; p += 1;
    ad[p].push_back(i);
  }
  dfs(1);
  cout << pq[1].size() << "\n";

  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...