Problem p03822 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define TRACE

#ifdef TRACE
template <class T, class U>
ostream &operator<<(ostream &out, const pair<T, U> &a) {
  out << "[" << a.first << " " << a.second << "]";
  return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[ ";
  for (auto &it : a)
    out << it << " ";
  out << "]";
  return out;
}
template <class T> ostream &operator<<(ostream &out, const set<T> &a) {
  out << "[ ";
  for (auto &it : a)
    out << it << " ";
  out << "]";
  return out;
}
template <class T> ostream &operator<<(ostream &out, const multiset<T> &a) {
  out << "[ ";
  for (auto &it : a)
    out << it << " ";
  out << "]";
  return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, const map<T, U> &a) {
  for (auto &it : a)
    out << it.first << " -> " << it.second << " | ";
  return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, const multimap<T, U> &a) {
  for (auto &it : a)
    out << it.first << " -> " << it.second << " | ";
  return out;
}
#define pra(a, n)                                                              \
  cerr << #a << " : ";                                                         \
  for (int i = 0; i < n; ++i)                                                  \
    cerr << a[i] << " ";                                                       \
  cerr << endl;
#define praa(a, n, m)                                                          \
  cerr << #a << " : " << endl;                                                 \
  for (int i = 0; i < n; ++i) {                                                \
    for (int j = 0; j < m; ++j)                                                \
      cerr << a[i][j] << " ";                                                  \
    cerr << endl;                                                              \
  }
#define pr(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define pr(...)
#define pra(a, n)
#define praa(a, n, m)
#endif

int form(int node, vector<vector<int>> g) {
  vector<int> sub;
  for (int next : g[node]) {
    sub.push_back(form(next, g));
  }

  sort(sub.begin(), sub.end());
  int ss = sub.size(), ans = 0;
  for (int i = 0; i < ss; i++) {
    ans = max(ans, ss - i + sub[i]);
  }

  return ans;
}

void Main() {
  int n;
  cin >> n;

  vector<int> a(n + 1);
  vector<vector<int>> g(n + 1);
  for (int i = 2; i <= n; i++) {
    int x;
    cin >> x;

    g[x].push_back(i);
  }

  cout << form(1, g) << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // freopen("", "r", stdin);
  // freopen("", "w", stdout);
  Main();
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define TRACE

#ifdef TRACE
template <class T, class U>
ostream &operator<<(ostream &out, const pair<T, U> &a) {
  out << "[" << a.first << " " << a.second << "]";
  return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[ ";
  for (auto &it : a)
    out << it << " ";
  out << "]";
  return out;
}
template <class T> ostream &operator<<(ostream &out, const set<T> &a) {
  out << "[ ";
  for (auto &it : a)
    out << it << " ";
  out << "]";
  return out;
}
template <class T> ostream &operator<<(ostream &out, const multiset<T> &a) {
  out << "[ ";
  for (auto &it : a)
    out << it << " ";
  out << "]";
  return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, const map<T, U> &a) {
  for (auto &it : a)
    out << it.first << " -> " << it.second << " | ";
  return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, const multimap<T, U> &a) {
  for (auto &it : a)
    out << it.first << " -> " << it.second << " | ";
  return out;
}
#define pra(a, n)                                                              \
  cerr << #a << " : ";                                                         \
  for (int i = 0; i < n; ++i)                                                  \
    cerr << a[i] << " ";                                                       \
  cerr << endl;
#define praa(a, n, m)                                                          \
  cerr << #a << " : " << endl;                                                 \
  for (int i = 0; i < n; ++i) {                                                \
    for (int j = 0; j < m; ++j)                                                \
      cerr << a[i][j] << " ";                                                  \
    cerr << endl;                                                              \
  }
#define pr(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define pr(...)
#define pra(a, n)
#define praa(a, n, m)
#endif

int form(int node, vector<vector<int>> &g) {
  vector<int> sub;
  for (int next : g[node]) {
    sub.push_back(form(next, g));
  }

  sort(sub.begin(), sub.end());
  int ss = sub.size(), ans = 0;
  for (int i = 0; i < ss; i++) {
    ans = max(ans, ss - i + sub[i]);
  }

  return ans;
}

void Main() {
  int n;
  cin >> n;

  vector<int> a(n + 1);
  vector<vector<int>> g(n + 1);
  for (int i = 2; i <= n; i++) {
    int x;
    cin >> x;

    g[x].push_back(i);
  }

  cout << form(1, g) << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // freopen("", "r", stdin);
  // freopen("", "w", stdout);
  Main();
}