Problem p03824 (C++)

Buggy submission (status: Runtime Error):
// Ho-kago Tea Time

#include <bits/stdc++.h>

using namespace std;

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define fst first
#define snd second
#define SZ(u) ((int)(u).size())
#define ALL(u) (u).begin(), (u).end()

inline void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline T read() {
  register T sum(0), fg(1);
  register char ch(getchar());
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fg = -1;
  for (; isdigit(ch); ch = getchar())
    sum = sum * 10 - '0' + ch;
  return sum * fg;
}

typedef long long LL;
typedef pair<int, int> pii;

const int MAXN = (int)1e5;

int n;

vector<int> adj[MAXN + 5];

inline void input() {
  n = read<int>();
  for (int i = 1; i < n; ++i) {
    int u = read<int>(), v = read<int>();
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
}

const int MAX_LOG = 20;

int f[MAX_LOG + 1][MAXN + 5];

int ans = 0;

inline void dfs(int u, int fa) {
  for (auto v : adj[u])
    if (v != fa) {
      dfs(v, u);
      for (int i = 0; i <= MAX_LOG; ++i)
        f[u][i] += f[v][i];
    }

  int k = 0;
  for (int i = MAX_LOG; i >= 0; --i)
    if (f[u][i] >= 2) {
      k = i + 1;
      break;
    }
  while (f[u][k] == 1)
    ++k;

  chkmax(ans, k), ++f[u][k];
  for (int i = 0; i < k; ++i)
    f[u][i] = 0;
}

inline void solve() {
  dfs(1, 0);
  printf("%d\n", ans);
}

int main() {
#ifdef K_ON // K-ON!
  freopen("D.in", "r", stdin);
  freopen("D.out", "w", stdout);
#endif

  input();
  solve();

  return 0;
}


Fixed submission:
// Ho-kago Tea Time

#include <bits/stdc++.h>

using namespace std;

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define fst first
#define snd second
#define SZ(u) ((int)(u).size())
#define ALL(u) (u).begin(), (u).end()

inline void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline T read() {
  register T sum(0), fg(1);
  register char ch(getchar());
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fg = -1;
  for (; isdigit(ch); ch = getchar())
    sum = sum * 10 - '0' + ch;
  return sum * fg;
}

typedef long long LL;
typedef pair<int, int> pii;

const int MAXN = (int)1e5;

int n;

vector<int> adj[MAXN + 5];

inline void input() {
  n = read<int>();
  for (int i = 1; i < n; ++i) {
    int u = read<int>(), v = read<int>();
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
}

const int MAX_LOG = 20;

int f[MAXN + 5][MAX_LOG + 1];

int ans = 0;

inline void dfs(int u, int fa) {
  for (auto v : adj[u])
    if (v != fa) {
      dfs(v, u);
      for (int i = 0; i <= MAX_LOG; ++i)
        f[u][i] += f[v][i];
    }

  int k = 0;
  for (int i = MAX_LOG; i >= 0; --i)
    if (f[u][i] >= 2) {
      k = i + 1;
      break;
    }
  while (f[u][k] == 1)
    ++k;

  chkmax(ans, k), ++f[u][k];
  for (int i = 0; i < k; ++i)
    f[u][i] = 0;
}

inline void solve() {
  dfs(1, 0);
  printf("%d\n", ans);
}

int main() {
#ifdef K_ON // K-ON!
  freopen("D.in", "r", stdin);
  freopen("D.out", "w", stdout);
#endif

  input();
  solve();

  return 0;
}