Problem p04008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) (void)0
#endif
#define mp make_pair
#define pb push_back
#define LL long long
#define pii pair<int, int>
#define PII pair<long long, long long>
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
const int inf = 0x7fffffff;        // beware overflow
const LL INF = 0x7fffffffffffffff; // beware overflow
#define mem(x, y) memset(x, (y), sizeof(x));
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
template <typename A, typename B>
ostream &operator<<(ostream &s, const pair<A, B> &p) {
  return s << "(" << p.first << "," << p.second << ")";
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &c) {
  s << "[ ";
  for (auto it : c)
    s << it << " ";
  s << "]";
  return s;
}
template <typename T> ostream &operator<<(ostream &o, const set<T> &st) {
  o << "{";
  for (auto it = st.begin(); it != st.end(); it++)
    o << (it == st.begin() ? "" : ", ") << *it;
  return o << "}";
}
template <typename T1, typename T2>
ostream &operator<<(ostream &o, const map<T1, T2> &mp) {
  o << "{";
  for (auto it = mp.begin(); it != mp.end(); it++) {
    o << (it == mp.begin() ? "" : ", ") << it->fi << ":" << it->se;
  }
  o << "}";
  return o;
}
#define maxn 100005
int par[maxn], n, k;
bool vis[maxn];
vector<int> G[maxn];
vector<pii> depth;

void dfs(int u, int dep) {
  depth.pb(mp(dep, u));
  for (int v : G[u]) {
    dfs(v, dep + 1);
  }
}
void mark(int u) {
  vis[u] = true;
  for (int v : G[u])
    if (!vis[v])
      mark(v);
}
int main() {
  cin >> n >> k;
  mem(vis, 0);
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    x--;
    par[i] = x;
    if (i)
      G[par[i]].pb(i);
  }
  int ans = (par[0] != 0);
  par[0] = 0;
  dfs(0, 0);
  sort(all(depth), greater<pii>());

  for (auto p : depth)
    if (!vis[p.se]) {
      int dep = 0;
      int cur = p.se;
      if (dep == k - 1 && par[cur] != 0) {
        par[cur] = 0, ans++;
        mark(cur);
        continue;
      }
      while (cur != 0) {
        cur = par[cur], dep++;
        if (dep == k - 1 && par[cur] != 0) {
          par[cur] = 0, ans++;
          mark(cur);
          break;
        }
      }
    }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) (void)0
#endif
#define mp make_pair
#define pb push_back
#define LL long long
#define pii pair<int, int>
#define PII pair<long long, long long>
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
const int inf = 0x7fffffff;        // beware overflow
const LL INF = 0x7fffffffffffffff; // beware overflow
#define mem(x, y) memset(x, (y), sizeof(x));
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
template <typename A, typename B>
ostream &operator<<(ostream &s, const pair<A, B> &p) {
  return s << "(" << p.first << "," << p.second << ")";
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &c) {
  s << "[ ";
  for (auto it : c)
    s << it << " ";
  s << "]";
  return s;
}
template <typename T> ostream &operator<<(ostream &o, const set<T> &st) {
  o << "{";
  for (auto it = st.begin(); it != st.end(); it++)
    o << (it == st.begin() ? "" : ", ") << *it;
  return o << "}";
}
template <typename T1, typename T2>
ostream &operator<<(ostream &o, const map<T1, T2> &mp) {
  o << "{";
  for (auto it = mp.begin(); it != mp.end(); it++) {
    o << (it == mp.begin() ? "" : ", ") << it->fi << ":" << it->se;
  }
  o << "}";
  return o;
}
#define maxn 100005
int par[maxn], n, k;
bool vis[maxn];
vector<int> G[maxn];
vector<pii> depth;

void dfs(int u, int dep) {
  depth.pb(mp(dep, u));
  for (int v : G[u]) {
    dfs(v, dep + 1);
  }
}
void mark(int u) {
  vis[u] = true;
  for (int v : G[u])
    if (!vis[v])
      mark(v);
}
int main() {
  cin >> n >> k;
  mem(vis, 0);
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    x--;
    par[i] = x;
    if (i)
      G[par[i]].pb(i);
  }
  int ans = (par[0] != 0);
  par[0] = 0;
  dfs(0, 0);
  sort(all(depth), greater<pii>());

  for (auto p : depth)
    if (!vis[p.se]) {
      if (p.fi <= k)
        break;
      int dep = 0;
      int cur = p.se;
      if (dep == k - 1 && par[cur] != 0) {
        par[cur] = 0, ans++;
        mark(cur);
        continue;
      }
      while (cur != 0) {
        cur = par[cur], dep++;
        if (dep == k - 1 && par[cur] != 0) {
          par[cur] = 0, ans++;
          mark(cur);
          break;
        }
      }
    }
  cout << ans << endl;
  return 0;
}