Problem p03204 (C++)

Buggy submission (status: Runtime Error):
// This amazing code is by Eric Sunli Chen.
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool get_int(T &x) {
  char t = getchar();
  bool neg = false;
  x = 0;
  for (; (t > '9' || t < '0') && t != '-' && t != EOF; t = getchar())
    ;
  if (t == '-')
    neg = true, t = getchar();
  if (t == EOF)
    return false;
  for (; t <= '9' && t >= '0'; t = getchar())
    x = x * 10 + t - '0';
  if (neg)
    x = -x;
  return true;
}
template <typename T> void print_int(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  short a[20] = {}, sz = 0;
  while (x > 0)
    a[sz++] = x % 10, x /= 10;
  if (sz == 0)
    putchar('0');
  for (int i = sz - 1; i >= 0; i--)
    putchar('0' + a[i]);
}
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define get1(a) get_int(a)
#define get2(a, b) (get1(a) && get1(b))
#define get3(a, b, c) (get1(a) && get2(b, c))
#define printendl(a) print_int(a), puts("")
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;
const LL Linf = 1ll << 61;
const double pi = acos(-1.0);

const int maxn = 100111;

vector<int> g[maxn], ng[maxn];
int n, fa[maxn], mx[maxn], sz[maxn], nsz[maxn], dt[maxn], ans[maxn];
void dfs(int x) {
  sz[x] = 1;
  nsz[x] = 1;
  for (auto &v : g[x])
    if (v != fa[x]) {
      fa[v] = x;
      mx[v] = max(mx[x], x);
      dfs(v);
      sz[x] += sz[v];
      nsz[x] += nsz[v];
    }

  if (x > mx[x]) {
    nsz[x] = 0;
    if (mx[x])
      ng[mx[x]].pb(x);
  }
  if (mx[x] == fa[x])
    dt[x] = nsz[x];
}

void dfs2(int x) {
  for (auto &v : g[x])
    if (v != fa[x]) {
      ans[v] = ans[x] + dt[v];
      dfs2(v);
    }
}

int f[maxn], fsz[maxn];
int gf(int x) { return x == f[x] ? x : f[x] = gf(f[x]); }
void merge(int u, int v) {
  u = gf(u);
  v = gf(v);
  if (u == v)
    return;
  f[u] = v;
  fsz[v] += fsz[u];
}
int main() {
  get1(n);
  for (int i = 1, u, v; i < n; i++) {
    get2(u, v);
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1);

  for (int i = 1; i <= n; i++) {
    f[i] = i;
    fsz[i] = 1;
  }
  for (int x = 1; x <= n; x++) {
    if (x > mx[x]) {
      for (auto &v : ng[x]) {
        int sum = 0;
        for (auto &v2 : g[v])
          if (v2 != fa[v] && v2 <= x) {
            sum += fsz[gf(v2)];
            dt[v2] -= fsz[gf(v2)];
          }
        dt[v] += sum + 1;
      }
    }
    for (auto &v : g[x])
      if (v <= x)
        merge(x, v);
  }
  dfs2(1);
  for (int i = 2; i <= n; i++)
    printf("%d ", ans[i]);
  return 0;
}

Fixed submission:
// This amazing code is by Eric Sunli Chen.
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool get_int(T &x) {
  char t = getchar();
  bool neg = false;
  x = 0;
  for (; (t > '9' || t < '0') && t != '-' && t != EOF; t = getchar())
    ;
  if (t == '-')
    neg = true, t = getchar();
  if (t == EOF)
    return false;
  for (; t <= '9' && t >= '0'; t = getchar())
    x = x * 10 + t - '0';
  if (neg)
    x = -x;
  return true;
}
template <typename T> void print_int(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  short a[20] = {}, sz = 0;
  while (x > 0)
    a[sz++] = x % 10, x /= 10;
  if (sz == 0)
    putchar('0');
  for (int i = sz - 1; i >= 0; i--)
    putchar('0' + a[i]);
}
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define get1(a) get_int(a)
#define get2(a, b) (get1(a) && get1(b))
#define get3(a, b, c) (get1(a) && get2(b, c))
#define printendl(a) print_int(a), puts("")
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;
const LL Linf = 1ll << 61;
const double pi = acos(-1.0);

const int maxn = 200111;

vector<int> g[maxn], ng[maxn];
int n, fa[maxn], mx[maxn], sz[maxn], nsz[maxn], dt[maxn], ans[maxn];
void dfs(int x) {
  sz[x] = 1;
  nsz[x] = 1;
  for (auto &v : g[x])
    if (v != fa[x]) {
      fa[v] = x;
      mx[v] = max(mx[x], x);
      dfs(v);
      sz[x] += sz[v];
      nsz[x] += nsz[v];
    }

  if (x > mx[x]) {
    nsz[x] = 0;
    if (mx[x])
      ng[mx[x]].pb(x);
  }
  if (mx[x] == fa[x])
    dt[x] = nsz[x];
}

void dfs2(int x) {
  for (auto &v : g[x])
    if (v != fa[x]) {
      ans[v] = ans[x] + dt[v];
      dfs2(v);
    }
}

int f[maxn], fsz[maxn];
int gf(int x) { return x == f[x] ? x : f[x] = gf(f[x]); }
void merge(int u, int v) {
  u = gf(u);
  v = gf(v);
  if (u == v)
    return;
  f[u] = v;
  fsz[v] += fsz[u];
}
int main() {
  get1(n);
  for (int i = 1, u, v; i < n; i++) {
    get2(u, v);
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1);

  for (int i = 1; i <= n; i++) {
    f[i] = i;
    fsz[i] = 1;
  }
  for (int x = 1; x <= n; x++) {
    if (x > mx[x]) {
      for (auto &v : ng[x]) {
        int sum = 0;
        for (auto &v2 : g[v])
          if (v2 != fa[v] && v2 <= x) {
            sum += fsz[gf(v2)];
            dt[v2] -= fsz[gf(v2)];
          }
        dt[v] += sum + 1;
      }
    }
    for (auto &v : g[x])
      if (v <= x)
        merge(x, v);
  }
  dfs2(1);
  for (int i = 2; i <= n; i++)
    printf("%d ", ans[i]);
  return 0;
}