Problem p03204 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define For(i, l, r) for (int i = (l), i##end = (r); i <= i##end; i++)
#define Fordown(i, r, l) for (int i = (r), i##end = (l); i >= i##end; i--)
#define debug(x) cout << #x << " = " << x << endl

using namespace std;

typedef long long ll;

template <typename T> inline bool chkmin(T &x, T y) {
  return y < x ? x = y, 1 : 0;
}
template <typename T> inline bool chkmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}

const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;

int to[N << 1], nex[N << 1], beg[N], Max[N], ans[N];
int e, n;

inline int read() {
  int x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    flag = -1, ch = getchar();
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + (ch - '0'), ch = getchar();
  return x * flag;
}

inline void add(int x, int y) { to[++e] = y, nex[e] = beg[x], beg[x] = e; }

inline void dfs(int x, int fa, int Las, int Sec) {
  Max[x] = Max[fa];
  if (fa > Max[x])
    Max[x] = fa, Las = Sec = x;
  else
    chkmax(Sec, x);
  if (x > Max[x])
    ans[x]++;
  else if (Max[Max[x]] > Sec)
    ans[Max[x]]++;
  else
    ans[Las]++;
  for (int i = beg[x]; i; i = nex[i]) {
    if (to[i] == fa)
      continue;
    dfs(to[i], x, Las, Sec);
  }
}

inline void dfs_ans(int x, int fa) {
  ans[x] += ans[fa];
  for (int i = beg[x]; i; i = nex[i]) {
    if (to[i] == fa)
      continue;
    dfs_ans(to[i], x);
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("AGC029E.in", "r", stdin);
  freopen("AGC029E.out", "w", stdout);
#endif

  n = read();
  For(i, 2, n) {
    int x = read(), y = read();
    add(x, y), add(y, x);
  }
  For(i, 1, n) Max[i] = i;
  dfs(1, 0, 0, 0), ans[1] = 0, dfs_ans(1, 0);
  For(i, 2, n) printf("%d%c", ans[i], i == n ? '\n' : ' ');
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define For(i, l, r) for (int i = (l), i##end = (r); i <= i##end; i++)
#define Fordown(i, r, l) for (int i = (r), i##end = (l); i >= i##end; i--)
#define debug(x) cout << #x << " = " << x << endl

using namespace std;

typedef long long ll;

template <typename T> inline bool chkmin(T &x, T y) {
  return y < x ? x = y, 1 : 0;
}
template <typename T> inline bool chkmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}

const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;

int to[N << 1], nex[N << 1], beg[N], Max[N], ans[N];
int e, n;

inline int read() {
  int x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    flag = -1, ch = getchar();
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + (ch - '0'), ch = getchar();
  return x * flag;
}

inline void add(int x, int y) { to[++e] = y, nex[e] = beg[x], beg[x] = e; }

inline void dfs(int x, int fa, int Las, int Sec) {
  Max[x] = Max[fa];
  if (fa > Max[x])
    Max[x] = fa, Las = Sec = x;
  else
    chkmax(Sec, x);
  if (x > Max[x])
    ans[x]++;
  else if (Max[Max[x]] > Sec)
    ans[Max[x]]++;
  else
    ans[Las]++;
  for (int i = beg[x]; i; i = nex[i]) {
    if (to[i] == fa)
      continue;
    dfs(to[i], x, Las, Sec);
  }
}

inline void dfs_ans(int x, int fa) {
  ans[x] += ans[fa];
  for (int i = beg[x]; i; i = nex[i]) {
    if (to[i] == fa)
      continue;
    dfs_ans(to[i], x);
  }
}

int main() {

  n = read();
  For(i, 2, n) {
    int x = read(), y = read();
    add(x, y), add(y, x);
  }
  For(i, 1, n) Max[i] = i;
  dfs(1, 0, 0, 0), ans[1] = 0, dfs_ans(1, 0);
  For(i, 2, n) printf("%d%c", ans[i], i == n ? '\n' : ' ');
  return 0;
}