Problem p03824 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 10;
const int lim = 18;
int read() {
  int x = 0, flag = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      flag = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * flag;
}
stack<int> s;
void out(int x) {
  if (!x)
    s.push(0);
  while (x) {
    s.push(x % 2);
    x /= 2;
  }
  for (int i = lim; i > (int)s.size(); i--)
    printf("0");
  while (!s.empty()) {
    printf("%d", s.top() % 2);
    s.pop();
  }
  puts("");
}
int n, ans, cnte;
int f[maxn], f2[maxn];
int ai[maxn], label[maxn];
int head[maxn], to[maxn * 2], nxt[maxn * 2];
int lowbit(int x) { return x & (-x); }
void AddEdge(int x, int y) {
  ++cnte;
  to[cnte] = y;
  nxt[cnte] = head[x];
  head[x] = cnte;
}
void init() {
  ai[0] = (1 << lim) - 1;
  for (int i = 1; i <= lim; i++)
    ai[i] = ai[i - 1] - lowbit(ai[i - 1]);
}
int find(int x, int first) {
  for (int i = first; i <= lim; i++)
    if (!((1 << i) & x))
      return i;
  return -1;
}
void dfs(int x, int fa) {
  for (int i = head[x]; i; i = nxt[i])
    if (to[i] ^ fa) {
      dfs(to[i], x);
      f2[x] |= f[to[i]] & f[x];
      f[x] = f[to[i]] ^ f[x];
    }

  //	printf("pro_x:%d\n",x);
  //	printf("f1:");out(f[x]);
  //	printf("f2:");out(f2[x]);

  label[x] = find(f[x], (!f2[x]) ? 0 : 32 - __builtin_clz(f2[x]));
  //	printf("label[x]:%d\n",label[x]);

  ans = max(ans, label[x]);

  f[x] = f[x] & ai[label[x]];
  f2[x] = f2[x] & ai[label[x]];

  f[x] |= (1 << label[x]);

  //	printf("updated_x:%d\n",x);
  //	printf("f1:");out(f[x]);
  //	printf("f2:");out(f2[x]);
  //	puts("");
}
int main() {
  freopen("test.in", "r", stdin), freopen("test.out", "w", stdout);
  init();

  n = read();
  for (int i = 1; i <= n - 1; i++) {
    int x, y;
    x = read();
    y = read();
    AddEdge(x, y);
    AddEdge(y, x);
  }

  dfs(1, 0);
  printf("%d\n", ans);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 10;
const int lim = 18;
int read() {
  int x = 0, flag = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      flag = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * flag;
}
stack<int> s;
void out(int x) {
  if (!x)
    s.push(0);
  while (x) {
    s.push(x % 2);
    x /= 2;
  }
  for (int i = lim; i > (int)s.size(); i--)
    printf("0");
  while (!s.empty()) {
    printf("%d", s.top() % 2);
    s.pop();
  }
  puts("");
}
int n, ans, cnte;
int f[maxn], f2[maxn];
int ai[maxn], label[maxn];
int head[maxn], to[maxn * 2], nxt[maxn * 2];
int lowbit(int x) { return x & (-x); }
void AddEdge(int x, int y) {
  ++cnte;
  to[cnte] = y;
  nxt[cnte] = head[x];
  head[x] = cnte;
}
void init() {
  ai[0] = (1 << lim) - 1;
  for (int i = 1; i <= lim; i++)
    ai[i] = ai[i - 1] - lowbit(ai[i - 1]);
}
int find(int x, int first) {
  for (int i = first; i <= lim; i++)
    if (!((1 << i) & x))
      return i;
  return -1;
}
void dfs(int x, int fa) {
  for (int i = head[x]; i; i = nxt[i])
    if (to[i] ^ fa) {
      dfs(to[i], x);
      f2[x] |= f[to[i]] & f[x];
      f[x] = f[to[i]] ^ f[x];
    }

  //	printf("pro_x:%d\n",x);
  //	printf("f1:");out(f[x]);
  //	printf("f2:");out(f2[x]);

  label[x] = find(f[x], (!f2[x]) ? 0 : 32 - __builtin_clz(f2[x]));
  //	printf("label[x]:%d\n",label[x]);

  ans = max(ans, label[x]);

  f[x] = f[x] & ai[label[x]];
  f2[x] = f2[x] & ai[label[x]];

  f[x] |= (1 << label[x]);

  //	printf("updated_x:%d\n",x);
  //	printf("f1:");out(f[x]);
  //	printf("f2:");out(f2[x]);
  //	puts("");
}
int main() {
  //	freopen("test.in","r",stdin),freopen("test.out","w",stdout);
  init();

  n = read();
  for (int i = 1; i <= n - 1; i++) {
    int x, y;
    x = read();
    y = read();
    AddEdge(x, y);
    AddEdge(y, x);
  }

  dfs(1, 0);
  printf("%d\n", ans);

  return 0;
}