Problem p03824 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#define cmin(a, b) (a > (b) ? a = (b), 1 : 0)
#define cmax(a, b) (a < (b) ? a = (b), 1 : 0)
#define dmin(a, b) ((a) < (b) ? (a) : (b))
#define dmax(a, b) ((a) > (b) ? (a) : (b))
namespace io {
int F() {
  int n = 0, F = 1;
  char ch;
  while ((ch = getchar()) != '-' && (ch < '0' || ch > '9'))
    ;
  ch == '-' ? F = 0 : n = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    n = n * 10 + ch - '0';
  return F ? n : -n;
}
} // namespace io
struct edge {
  int to;
  int next;
} e[666666];
int pe = 222222;
void insert(int a, int to) {
  e[pe] = (edge){to, e[a].next};
  e[a].next = pe++;
}
int f[222222], g[222222];
int top[555555];
void dfs(int o, int fa) {
  int s = 0;
  int min = -1;
  for (register int p = e[o].next; p; p = e[p].next)
    if (e[p].to != fa) {
      dfs(e[p].to, o);
      cmax(min, top[s & g[e[p].to]]);
      s |= g[e[p].to];
    }
  for (register int i = min + 1; i <= 20; ++i)
    if (!(s & 1 << i)) {
      f[o] = i;
      break;
    }
  g[o] = 1 << f[o];
  for (register int p = e[o].next; p; p = e[p].next)
    if (e[p].to != fa)
      g[o] |= g[e[p].to] & ~((1 << f[o]) - 1);
}
int main() {
  freopen("tdc.in", "r", stdin);
  freopen("tdc.out", "w", stdout);
  top[0] = -1;
  for (register int i = 2; i <= 524287; ++i)
    top[i] = top[i >> 1] + 1;
  if (1) {
    int n = io::F();
    if (n == 0) {
      puts("0");
      return 0;
    }
    for (register int i = 1; i < n; ++i) {
      int x = io::F(), y = io::F();
      insert(x, y);
      insert(y, x);
    }
    dfs(1, 0);
    int max = 0;
    for (register int i = 1; i <= n; ++i)
      cmax(max, f[i]);
    printf("%d\n", max);
    pe = 222222;
    for (register int i = 1; i <= n; ++i)
      e[i].next = 0;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#define cmin(a, b) (a > (b) ? a = (b), 1 : 0)
#define cmax(a, b) (a < (b) ? a = (b), 1 : 0)
#define dmin(a, b) ((a) < (b) ? (a) : (b))
#define dmax(a, b) ((a) > (b) ? (a) : (b))
namespace io {
int F() {
  int n = 0, F = 1;
  char ch;
  while ((ch = getchar()) != '-' && (ch < '0' || ch > '9'))
    ;
  ch == '-' ? F = 0 : n = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    n = n * 10 + ch - '0';
  return F ? n : -n;
}
} // namespace io
struct edge {
  int to;
  int next;
} e[666666];
int pe = 222222;
void insert(int a, int to) {
  e[pe] = (edge){to, e[a].next};
  e[a].next = pe++;
}
int f[222222], g[222222];
int top[555555];
void dfs(int o, int fa) {
  int s = 0;
  int min = -1;
  for (register int p = e[o].next; p; p = e[p].next)
    if (e[p].to != fa) {
      dfs(e[p].to, o);
      cmax(min, top[s & g[e[p].to]]);
      s |= g[e[p].to];
    }
  for (register int i = min + 1; i <= 20; ++i)
    if (!(s & 1 << i)) {
      f[o] = i;
      break;
    }
  g[o] = 1 << f[o];
  for (register int p = e[o].next; p; p = e[p].next)
    if (e[p].to != fa)
      g[o] |= g[e[p].to] & ~((1 << f[o]) - 1);
}
int main() {
  top[0] = -1;
  for (register int i = 2; i <= 524287; ++i)
    top[i] = top[i >> 1] + 1;
  if (1) {
    int n = io::F();
    if (n == 0) {
      puts("0");
      return 0;
    }
    for (register int i = 1; i < n; ++i) {
      int x = io::F(), y = io::F();
      insert(x, y);
      insert(y, x);
    }
    dfs(1, 0);
    int max = 0;
    for (register int i = 1; i <= n; ++i)
      cmax(max, f[i]);
    printf("%d\n", max);
    pe = 222222;
    for (register int i = 1; i <= n; ++i)
      e[i].next = 0;
  }
  return 0;
}