Problem p04008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "algorithm"
#include "bitset"
#include "climits"
#include "cmath"
#include "cstdio"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

const int MOD = 1000000007;
// const int MOD = 998244353;

long long int N, M, K, H, W, L, R;
// int N, M, K, H, W, L, R;

void Calculate_Depth(vector<vector<int>> &edge, vector<int> &depth,
                     int stnode) {
  depth[stnode] = 0;
  queue<int> Q;
  Q.push(stnode);
  while (!Q.empty()) {
    int cn = Q.front();
    Q.pop();
    for (auto i : edge[cn]) {
      if (depth[i] > depth[cn] + 1) {
        depth[i] = depth[cn] + 1;
        Q.push(i);
      }
    }
  }
  return;
}

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

  cin >> N >> K;
  vector<int> v(N);
  vector<vector<int>> edge(N);
  vector<int> depth(N, MOD);
  depth[0] = 0;
  int ans = 0;
  for (auto &i : v) {
    cin >> i;
    i--;
  }
  for (int i = 1; i < N; i++) {
    edge[v[i]].push_back(i);
  }
  if (v[0]) {
    v[0] = 0;
    ans++;
  }
  Calculate_Depth(edge, depth, 0);
  priority_queue<pair<int, int>> PQ;
  for (int i = 1; i < N; i++) {
    if (depth[i] > K)
      PQ.push({depth[i], i});
  }
  while (!PQ.empty()) {
    int cn = PQ.top().second;
    int c = PQ.top().first;
    PQ.pop();
    if (c != depth[cn])
      continue;
    ans++;
    while (depth[cn] > c - K + 1) {
      cn = v[cn];
    }
    depth[cn] = 1;
    queue<int> Q;
    Q.push(cn);
    while (!Q.empty()) {
      int ccn = Q.front();
      Q.pop();
      for (auto i : edge[ccn]) {
        depth[i] = depth[ccn] + 1;
        Q.push(i);
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include "algorithm"
#include "bitset"
#include "climits"
#include "cmath"
#include "cstdio"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

const int MOD = 1000000007;
// const int MOD = 998244353;

long long int N, M, K, H, W, L, R;
// int N, M, K, H, W, L, R;

void Calculate_Depth(vector<vector<int>> &edge, vector<int> &depth,
                     int stnode) {
  depth[stnode] = 0;
  queue<int> Q;
  Q.push(stnode);
  while (!Q.empty()) {
    int cn = Q.front();
    Q.pop();
    for (auto i : edge[cn]) {
      if (depth[i] > depth[cn] + 1) {
        depth[i] = depth[cn] + 1;
        Q.push(i);
      }
    }
  }
  return;
}

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

  cin >> N >> K;
  vector<int> v(N);
  vector<vector<int>> edge(N);
  vector<int> depth(N, MOD);
  depth[0] = 0;
  int ans = 0;
  for (auto &i : v) {
    cin >> i;
    i--;
  }
  for (int i = 1; i < N; i++) {
    edge[v[i]].push_back(i);
  }
  if (v[0]) {
    v[0] = 0;
    ans++;
  }
  Calculate_Depth(edge, depth, 0);
  priority_queue<pair<int, int>> PQ;
  for (int i = 1; i < N; i++) {
    if (depth[i] > K)
      PQ.push({depth[i], i});
  }
  while (!PQ.empty()) {
    int cn = PQ.top().second;
    int c = PQ.top().first;
    PQ.pop();
    if (c != depth[cn])
      continue;
    ans++;
    while (depth[cn] > c - K + 1) {
      cn = v[cn];
    }
    depth[cn] = 1;
    queue<int> Q;
    Q.push(cn);
    while (!Q.empty()) {
      int ccn = Q.front();
      Q.pop();
      for (auto i : edge[ccn]) {
        if (depth[i] > depth[ccn] + 1) {
          depth[i] = depth[ccn] + 1;
          Q.push(i);
        }
      }
    }
  }
  cout << ans << endl;
}