Problem p03204 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define N (100010)
#define P ()
#define M (N << 1)
#define inf (0x7f7f7f7f)
#define rg register int
#define Label puts("NAIVE")
#define spa print(' ')
#define ent print('\n')
#define rand() (((rand()) << (15)) ^ (rand()))
#define file(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout)
typedef long double ld;
typedef long long LL;
typedef unsigned long long ull;
using namespace std;
namespace fastIO1 {
inline char read() {
  static const int IN_LEN = 1000000;
  static char buf[IN_LEN], *s, *t;
  return (s == t               ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin),
          (s == t ? -1 : *s++) : *s++);
}
template <class T> inline void read(T &x) {
  static bool iosig;
  static char c;
  for (iosig = false, c = read(); !isdigit(c); c = read()) {
    if (c == '-')
      iosig = true;
    if (c == -1)
      return;
  }
  for (x = 0; isdigit(c); c = read())
    x = ((x + (x << 2)) << 1) + (c ^ '0');
  if (iosig)
    x = -x;
}
inline char readc(char &c) {
  for (c = read(); !isalpha(c) && !isdigit(c); c = read())
    if (c == -1)
      return 0;
}
const int OUT_LEN = 10000000;
char obuf[OUT_LEN], *ooh = obuf;
inline void print(char c) {
  if (ooh == obuf + OUT_LEN)
    fwrite(obuf, 1, OUT_LEN, stdout), ooh = obuf;
  *ooh++ = c;
}
template <class T> inline void print(T x) {
  static int buf[30], cnt;
  if (x == 0)
    print('0');
  else {
    if (x < 0)
      print('-'), x = -x;
    for (cnt = 0; x; x /= 10)
      buf[++cnt] = x % 10 + 48;
    while (cnt)
      print((char)buf[cnt--]);
  }
}
inline void flush() { fwrite(obuf, 1, ooh - obuf, stdout); }
} // namespace fastIO1
namespace fastIO2 {
template <class T> inline void read(T &x) {
  static bool iosig;
  static char c;
  for (iosig = false, c = getchar(); !isdigit(c); c = getchar()) {
    if (c == '-')
      iosig = true;
    if (c == -1)
      return;
  }
  for (x = 0; isdigit(c); c = getchar())
    x = ((x + (x << 2)) << 1) + (c ^ '0');
  if (iosig)
    x = -x;
}
} // namespace fastIO2
using namespace fastIO1;
int n, m, fi[N], ne[M], b[M], fa[N], mx[N], p[N], siz[N];
int st[N], dep[N], val[N], ans[N], top, E;
vector<int> s[N];
int ask(int x) { return p[x] == x ? x : p[x] = ask(p[x]); }
void dfs(int u, int pre) {
  fa[u] = pre;
  for (int i = fi[u]; i; i = ne[i]) {
    int v = b[i];
    if (v == pre)
      continue;
    mx[v] = max(mx[u], u);
    dfs(v, u);
  }
}
void dfs2(int u, int pre) {
  st[++top] = u;
  dep[u] = top;
  //	for(int i=1;i<=top;i++)cout<<st[i]<<" ";
  //	cout<<":"<<" "<<mx[u]<<st[dep[mx[u]]+1]<<"\n";
  if (u > 1)
    ans[u] += ans[mx[u]] - val[st[dep[mx[u]] + 1]];
  for (int i = fi[u]; i; i = ne[i]) {
    int v = b[i];
    if (v == pre)
      continue;
    dfs2(v, u);
  }
  top--;
}
void add(int x, int y) { ne[++E] = fi[x], fi[x] = E, b[E] = y; }
int main() {
  read(n);
  for (int i = 1; i < n; i++) {
    int x, y;
    read(x), read(y);
    add(x, y), add(y, x);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    p[i] = i, siz[i] = (i != 1);
  for (int i = 2; i <= n; i++)
    s[mx[i]].push_back(i);
  //	puts("faq1");
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < s[i].size(); j++) {
      int u = s[i][j];
      //			cout<<i<<","<<u<<":";
      if (u < i) {
        ans[u] = siz[ask(u)];
        //				cout<<i<<" "<<u<<" "<<ask(u)<<"
        //"<<siz[ask(u)]<<endl;
      } else {
        ans[u] = 1;
        for (int t = fi[u]; t; t = ne[t]) {
          int v = b[t];
          if (v >= i)
            continue;
          if (v != fa[u])
            val[v] = siz[ask(v)];
          ans[u] += siz[ask(v)];
          //					cout<<v<<" ";
        }
      }
      //			puts("");
    }

    for (int j = fi[i]; j; j = ne[j]) {
      int v = b[j], t = ask(i);
      if (v >= i)
        continue;
      v = ask(v), p[v] = t, siz[t] += siz[v];
    }
  }
  //	puts("faq2");
  //	for(int i=1;i<=n;i++)cout<<ans[i]<<" "; puts("");
  //	for(int i=1;i<=n;i++)cout<<val[i]<<" "; puts("");
  dfs2(1, 0);
  for (int i = 2; i <= n; i++)
    printf("%d ", ans[i]);
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define N (200010)
#define P ()
#define M (N << 1)
#define inf (0x7f7f7f7f)
#define rg register int
#define Label puts("NAIVE")
#define spa print(' ')
#define ent print('\n')
#define rand() (((rand()) << (15)) ^ (rand()))
#define file(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout)
typedef long double ld;
typedef long long LL;
typedef unsigned long long ull;
using namespace std;
namespace fastIO1 {
inline char read() {
  static const int IN_LEN = 1000000;
  static char buf[IN_LEN], *s, *t;
  return (s == t               ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin),
          (s == t ? -1 : *s++) : *s++);
}
template <class T> inline void read(T &x) {
  static bool iosig;
  static char c;
  for (iosig = false, c = read(); !isdigit(c); c = read()) {
    if (c == '-')
      iosig = true;
    if (c == -1)
      return;
  }
  for (x = 0; isdigit(c); c = read())
    x = ((x + (x << 2)) << 1) + (c ^ '0');
  if (iosig)
    x = -x;
}
inline char readc(char &c) {
  for (c = read(); !isalpha(c) && !isdigit(c); c = read())
    if (c == -1)
      return 0;
}
const int OUT_LEN = 10000000;
char obuf[OUT_LEN], *ooh = obuf;
inline void print(char c) {
  if (ooh == obuf + OUT_LEN)
    fwrite(obuf, 1, OUT_LEN, stdout), ooh = obuf;
  *ooh++ = c;
}
template <class T> inline void print(T x) {
  static int buf[30], cnt;
  if (x == 0)
    print('0');
  else {
    if (x < 0)
      print('-'), x = -x;
    for (cnt = 0; x; x /= 10)
      buf[++cnt] = x % 10 + 48;
    while (cnt)
      print((char)buf[cnt--]);
  }
}
inline void flush() { fwrite(obuf, 1, ooh - obuf, stdout); }
} // namespace fastIO1
namespace fastIO2 {
template <class T> inline void read(T &x) {
  static bool iosig;
  static char c;
  for (iosig = false, c = getchar(); !isdigit(c); c = getchar()) {
    if (c == '-')
      iosig = true;
    if (c == -1)
      return;
  }
  for (x = 0; isdigit(c); c = getchar())
    x = ((x + (x << 2)) << 1) + (c ^ '0');
  if (iosig)
    x = -x;
}
} // namespace fastIO2
using namespace fastIO1;
int n, m, fi[N], ne[M], b[M], fa[N], mx[N], p[N], siz[N];
int st[N], dep[N], val[N], ans[N], top, E;
vector<int> s[N];
int ask(int x) { return p[x] == x ? x : p[x] = ask(p[x]); }
void dfs(int u, int pre) {
  fa[u] = pre;
  for (int i = fi[u]; i; i = ne[i]) {
    int v = b[i];
    if (v == pre)
      continue;
    mx[v] = max(mx[u], u);
    dfs(v, u);
  }
}
void dfs2(int u, int pre) {
  st[++top] = u;
  dep[u] = top;
  //	for(int i=1;i<=top;i++)cout<<st[i]<<" ";
  //	cout<<":"<<" "<<mx[u]<<st[dep[mx[u]]+1]<<"\n";
  if (u > 1)
    ans[u] += ans[mx[u]] - val[st[dep[mx[u]] + 1]];
  for (int i = fi[u]; i; i = ne[i]) {
    int v = b[i];
    if (v == pre)
      continue;
    dfs2(v, u);
  }
  top--;
}
void add(int x, int y) { ne[++E] = fi[x], fi[x] = E, b[E] = y; }
int main() {
  read(n);
  for (int i = 1; i < n; i++) {
    int x, y;
    read(x), read(y);
    add(x, y), add(y, x);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    p[i] = i, siz[i] = (i != 1);
  for (int i = 2; i <= n; i++)
    s[mx[i]].push_back(i);
  //	puts("faq1");
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < s[i].size(); j++) {
      int u = s[i][j];
      //			cout<<i<<","<<u<<":";
      if (u < i) {
        ans[u] = siz[ask(u)];
        //				cout<<i<<" "<<u<<" "<<ask(u)<<"
        //"<<siz[ask(u)]<<endl;
      } else {
        ans[u] = 1;
        for (int t = fi[u]; t; t = ne[t]) {
          int v = b[t];
          if (v >= i)
            continue;
          if (v != fa[u])
            val[v] = siz[ask(v)];
          ans[u] += siz[ask(v)];
          //					cout<<v<<" ";
        }
      }
      //			puts("");
    }

    for (int j = fi[i]; j; j = ne[j]) {
      int v = b[j], t = ask(i);
      if (v >= i)
        continue;
      v = ask(v), p[v] = t, siz[t] += siz[v];
    }
  }
  //	puts("faq2");
  //	for(int i=1;i<=n;i++)cout<<ans[i]<<" "; puts("");
  //	for(int i=1;i<=n;i++)cout<<val[i]<<" "; puts("");
  dfs2(1, 0);
  for (int i = 2; i <= n; i++)
    printf("%d ", ans[i]);
}