Problem p03204 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

const int _ = 1e5 + 7;
struct Edge {
  int end, upEd;
} Ed[_ << 1];
int head[_], cntEd, N;

void addEd(int a, int b) {
  Ed[++cntEd] = (Edge){b, head[a]};
  head[a] = cntEd;
}

int fa[_], mx[_], ts;
vector<int> nxt[_];
void dfs(int x, int p, int m) {
  fa[x] = p;
  mx[x] = m;
  if (m < x) {
    nxt[m].push_back(x);
    m = x;
  }
  for (int i = head[x]; i; i = Ed[i].upEd)
    if (Ed[i].end != p)
      dfs(Ed[i].end, x, m);
}

int sum[_], tmp[_];
void down(int x, int p) {
  for (int i = head[x]; i; i = Ed[i].upEd)
    if (Ed[i].end != p) {
      sum[Ed[i].end] += sum[x];
      down(Ed[i].end, x);
    }
}

int pre[_], sz[_];
int find(int x) { return pre[x] == x ? x : (pre[x] = find(pre[x])); }

signed main() {
  ios::sync_with_stdio(0);
  cin >> N;
  for (int i = 1; i < N; ++i) {
    int a, b;
    cin >> a >> b;
    addEd(a, b);
    addEd(b, a);
  }
  dfs(1, 0, 0);
  for (int i = 1; i <= N; ++i)
    pre[i] = i;
  for (int i = 1; i <= N; ++i) {
    if (mx[i] < i) {
      for (auto x : nxt[i]) {
        int cur = 0;
        for (int j = head[x]; j; j = Ed[j].upEd)
          if (Ed[j].end != fa[x])
            cur += sz[find(Ed[j].end)];
        tmp[x] = 1 + cur;
        for (int j = head[x]; j; j = Ed[j].upEd)
          if (Ed[j].end != fa[x])
            sum[Ed[j].end] += cur - sz[find(Ed[j].end)];
      }
      for (int j = head[i]; j; j = Ed[j].upEd)
        if (Ed[j].end != fa[i])
          sum[Ed[j].end] += sz[find(Ed[j].end)] + 1;
    }
    sz[i] = 1;
    for (int j = head[i]; j; j = Ed[j].upEd)
      if (Ed[j].end < i) {
        sz[find(i)] += sz[find(Ed[j].end)];
        pre[find(Ed[j].end)] = find(i);
      }
  }
  down(1, 0);
  for (int i = 2; i <= N; ++i)
    printf("%d ", sum[i] + tmp[i] - 1);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

const int _ = 2e5 + 7;
struct Edge {
  int end, upEd;
} Ed[_ << 1];
int head[_], cntEd, N;

void addEd(int a, int b) {
  Ed[++cntEd] = (Edge){b, head[a]};
  head[a] = cntEd;
}

int fa[_], mx[_], ts;
vector<int> nxt[_];
void dfs(int x, int p, int m) {
  fa[x] = p;
  mx[x] = m;
  if (m < x) {
    nxt[m].push_back(x);
    m = x;
  }
  for (int i = head[x]; i; i = Ed[i].upEd)
    if (Ed[i].end != p)
      dfs(Ed[i].end, x, m);
}

int sum[_], tmp[_];
void down(int x, int p) {
  for (int i = head[x]; i; i = Ed[i].upEd)
    if (Ed[i].end != p) {
      sum[Ed[i].end] += sum[x];
      down(Ed[i].end, x);
    }
}

int pre[_], sz[_];
int find(int x) { return pre[x] == x ? x : (pre[x] = find(pre[x])); }

signed main() {
  ios::sync_with_stdio(0);
  cin >> N;
  for (int i = 1; i < N; ++i) {
    int a, b;
    cin >> a >> b;
    addEd(a, b);
    addEd(b, a);
  }
  dfs(1, 0, 0);
  for (int i = 1; i <= N; ++i)
    pre[i] = i;
  for (int i = 1; i <= N; ++i) {
    if (mx[i] < i) {
      for (auto x : nxt[i]) {
        int cur = 0;
        for (int j = head[x]; j; j = Ed[j].upEd)
          if (Ed[j].end != fa[x])
            cur += sz[find(Ed[j].end)];
        tmp[x] = 1 + cur;
        for (int j = head[x]; j; j = Ed[j].upEd)
          if (Ed[j].end != fa[x])
            sum[Ed[j].end] += cur - sz[find(Ed[j].end)];
      }
      for (int j = head[i]; j; j = Ed[j].upEd)
        if (Ed[j].end != fa[i])
          sum[Ed[j].end] += sz[find(Ed[j].end)] + 1;
    }
    sz[i] = 1;
    for (int j = head[i]; j; j = Ed[j].upEd)
      if (Ed[j].end < i) {
        sz[find(i)] += sz[find(Ed[j].end)];
        pre[find(Ed[j].end)] = find(i);
      }
  }
  down(1, 0);
  for (int i = 2; i <= N; ++i)
    printf("%d ", sum[i] + tmp[i] - 1);
  return 0;
}