Problem p03822 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author yassin
 */

#include <fstream>

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pair<int, int>> vii;

class atB {
private:
  /*int dfs(int u, vector<vector<int>> &g, int ind) {
      int depth = 0;
      for (int v : g[u]) {
          depth = max(depth, dfs(v, g)+1);
      }
      if (ind < (int)g[u].size()) {
          depth = max(depth, dfs(u, g, ind+1)+1);
      }
      return depth;
  }*/

  int solve(int i, vector<int> &dp, vector<int> &res, vector<vector<int>> g) {
    if (res[i] != -1) {
      return res[i];
    }

    if (dp[i] == 0) {
      return 0;
    }

    int ans = 0;
    vector<int> sz;
    for (int j = 0; j < (int)g[i].size(); ++j) {
      sz.push_back(solve(g[i][j], dp, res, g));
    }
    sort(sz.begin(), sz.end(), greater<int>());
    for (int j = 0; j < (int)sz.size(); ++j) {
      ans = max(ans, j + 1 + sz[j]);
    }

    return (res[i] = ans);
  }

public:
  void solve(istream &in, ostream &out) {
    int N;
    in >> N;

    vector<int> a(N + 1);
    for (int i = 2; i <= N; ++i) {
      in >> a[i];
    }
    vector<vector<int>> g(N + 1);
    for (int i = 2; i <= N; ++i) {
      g[a[i]].push_back(i);
      // g[a[i]].push_back(a[i]);
    }
    vector<int> dp(N + 1), res(N + 1, -1);
    for (int i = 0; i <= N; ++i) {
      dp[i] = g[i].size();
    }
    int ans = solve(1, dp, res, g);
    /*sort(dp.begin(), dp.end());
    vector<int> dp2(N+1, 0);
    for (int i = 0; i <= N; ++i) {
        if (dp[i].first == 0) {
            dp2[dp[i].second] = 0;
        }
    }*/
    out << ans << endl;
  }
};

int main() {
  std::ios::sync_with_stdio(false);

  atB solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}


Fixed submission:
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author yassin
 */

#include <fstream>

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pair<int, int>> vii;

class atB {
private:
  /*int dfs(int u, vector<vector<int>> &g, int ind) {
      int depth = 0;
      for (int v : g[u]) {
          depth = max(depth, dfs(v, g)+1);
      }
      if (ind < (int)g[u].size()) {
          depth = max(depth, dfs(u, g, ind+1)+1);
      }
      return depth;
  }*/

  int solve(int i, const vector<int> &dp, vector<int> &res,
            const vector<vector<int>> &g) {
    if (res[i] != -1) {
      return res[i];
    }

    if (dp[i] == 0) {
      return 0;
    }

    int ans = 0;
    vector<int> sz;
    for (int j = 0; j < (int)g[i].size(); ++j) {
      sz.push_back(solve(g[i][j], dp, res, g));
    }
    sort(sz.begin(), sz.end(), greater<int>());
    for (int j = 0; j < (int)sz.size(); ++j) {
      ans = max(ans, j + 1 + sz[j]);
    }

    return (res[i] = ans);
  }

public:
  void solve(istream &in, ostream &out) {
    int N;
    in >> N;

    vector<int> a(N + 1);
    for (int i = 2; i <= N; ++i) {
      in >> a[i];
    }
    vector<vector<int>> g(N + 1);
    for (int i = 2; i <= N; ++i) {
      g[a[i]].push_back(i);
      // g[a[i]].push_back(a[i]);
    }
    vector<int> dp(N + 1), res(N + 1, -1);
    for (int i = 0; i <= N; ++i) {
      dp[i] = g[i].size();
    }
    int ans = solve(1, dp, res, g);
    /*sort(dp.begin(), dp.end());
    vector<int> dp2(N+1, 0);
    for (int i = 0; i <= N; ++i) {
        if (dp[i].first == 0) {
            dp2[dp[i].second] = 0;
        }
    }*/
    out << ans << endl;
  }
};

int main() {
  std::ios::sync_with_stdio(false);

  atB solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}