Problem p04008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long int llint;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
const int mod = 1000000007;
const llint big = 1e18 + 10;
const long double pai = 3.141592653589793238462643383279502884197;
const long double eps = 1e-9;
template <class T, class U> void mineq(T &a, U b) {
  if (a > b) {
    a = b;
  }
}
template <class T, class U> void maxeq(T &a, U b) {
  if (a < b) {
    a = b;
  }
}
int gcd(int a, int b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
} // a>b
class in_t {
public:
  int a;
  in_t() { cin >> a; }
  operator int() { return a; }
};
class lin_t {
public:
  llint a;
  lin_t() { cin >> a; }
  operator llint() { return a; }
};
class ld_t {
public:
  long double a;
  ld_t() { cin >> a; }
  operator long double() { return a; }
};
vector<int> ro;
vector<int> dis;
vector<vector<int>> ko;
vector<pair<int, int>> zyn;
vector<bool> used;
int ans = 0;
int n, k;
void make_tree(int now = 0, int di = 0) {
  dis[now] = di;
  zyn[now] = mp(di, now);
  for (int i = 0; i < ko[now].size(); i++) {
    make_tree(ko[now][i], di + 1);
  }
}
void tle(int now) {
  if (used[now]) {
    return;
  }
  used[now] = true;
  for (int i = 0; i < ko[now].size(); i++) {
    tle(ko[now][i]);
  }
}
void mle(int now) {
  if (used[now]) {
    return;
  }
  ans++;
  for (int i = 0; i < k - 1; i++) {
    now = ro[now];
  }
  if (ro[now] == 0) {
    now = 0;
  }
  tle(now);
}
int main(void) {
  cin >> n >> k;
  int i;
  ro.res(n);
  dis.res(n);
  ko.res(n);
  zyn.res(n);
  used.res(n);
  for (i = 0; i < n; i++) {
    cin >> ro[i];
    ro[i]--;
    if (i != 0) {
      ko[ro[i]].pub(i);
    }
  }
  if (ro[0] != 0) {
    ro[0] = 0;
    ans++;
  }
  make_tree();
  sort(zyn.rbegin(), zyn.rend());
  for (i = 0; i < n; i++) {
    int kes = zyn[i].sec;
    mle(kes);
  }
  cout << ans - 1 << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long int llint;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
const int mod = 1000000007;
const llint big = 1e18 + 10;
const long double pai = 3.141592653589793238462643383279502884197;
const long double eps = 1e-9;
template <class T, class U> void mineq(T &a, U b) {
  if (a > b) {
    a = b;
  }
}
template <class T, class U> void maxeq(T &a, U b) {
  if (a < b) {
    a = b;
  }
}
int gcd(int a, int b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
} // a>b
class in_t {
public:
  int a;
  in_t() { cin >> a; }
  operator int() { return a; }
};
class lin_t {
public:
  llint a;
  lin_t() { cin >> a; }
  operator llint() { return a; }
};
class ld_t {
public:
  long double a;
  ld_t() { cin >> a; }
  operator long double() { return a; }
};
vector<int> ro;
vector<int> dis;
vector<vector<int>> ko;
vector<pair<int, int>> zyn;
vector<bool> used;
int ans = 0;
int n, k;
void make_tree(int now = 0, int di = 0) {
  dis[now] = di;
  zyn[now] = mp(di, now);
  for (int i = 0; i < ko[now].size(); i++) {
    make_tree(ko[now][i], di + 1);
  }
}
void tle(int now) {
  if (used[now]) {
    return;
  }
  used[now] = true;
  for (int i = 0; i < ko[now].size(); i++) {
    tle(ko[now][i]);
  }
}
void mle(int now) {
  if (used[now]) {
    return;
  }
  ans++;
  for (int i = 0; i < k - 1; i++) {
    now = ro[now];
  }
  if (ro[now] == 0) {
    now = 0;
  }
  tle(now);
}
int main(void) {
  cin >> n >> k;
  if (n < k) {
    k = n;
  }
  int i;
  ro.res(n);
  dis.res(n);
  ko.res(n);
  zyn.res(n);
  used.res(n);
  for (i = 0; i < n; i++) {
    cin >> ro[i];
    ro[i]--;
    if (i != 0) {
      ko[ro[i]].pub(i);
    }
  }
  if (ro[0] != 0) {
    ro[0] = 0;
    ans++;
  }
  make_tree();
  sort(zyn.rbegin(), zyn.rend());
  for (i = 0; i < n; i++) {
    int kes = zyn[i].sec;
    mle(kes);
  }
  cout << ans - 1 << endl;
  return 0;
}