Problem p03727 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define fa hi][0
#define sz hi][1
#define son hi][2
#define dep hi][3
#define top hi][4
#define id hi][5
const int N = 1e5 + 7;
int n;
struct edge {
  int to, nx;
} e[N * 2];
int e0[N], ep = 2, idr[N];
int hi[N][8], idp = 0;
void f1(int w, int pa) {
  w[dep] = pa[dep] + 1;
  w[fa] = pa;
  for (int i = e0[w]; i; i = e[i].nx) {
    int u = e[i].to;
    if (u == pa)
      continue;
    f1(u, w);
    w[sz] += u[sz];
    if (u[sz] > w[son][sz])
      w[son] = u;
  }
}
void f2(int w, int tp) {
  w[top] = tp;
  w[id] = ++idp;
  idr[idp] = w;
  if (w[son])
    f2(w[son], tp);
  for (int i = e0[w]; i; i = e[i].nx) {
    int u = e[i].to;
    if (u != w[fa] && u != w[son])
      f2(u, u);
  }
}
int ps[N][2], ed[N];
int min(int a, int b) { return a < b ? a : b; }
int _l, _r, _w;
struct node {
  node *lc, *rc;
  int L, R, M;
  int mn, a;
  std::vector<int> vs;
  void up() { mn = min(lc->mn, rc->mn); }
  void add(int x) { mn += x, a += x; }
  void dn() {
    if (a)
      lc->add(a), rc->add(a), a = 0;
  }
  void ins() {
    if (_l <= L && R <= _r)
      return vs.push_back(_w), add(1);
    dn();
    if (_l <= M)
      lc->ins();
    if (_r > M)
      rc->ins();
    up();
  }
  void del() {
    if (_l <= L && R <= _r)
      return add(-1);
    dn();
    if (_l <= M)
      lc->del();
    if (_r > M)
      rc->del();
    up();
  }
  void gmn() {
    while (vs.size() && ed[vs.back()])
      vs.pop_back();
    if (vs.size())
      _w = vs.back(), vs.pop_back();
    if (L == R) {
      mn = 0x3f3f3f3f;
      return;
    }
    dn();
    (mn == lc->mn ? lc : rc)->gmn();
    up();
  }
} ns[N * 2], *np = ns, *rt;
node *build(int L, int R) {
  node *w = np++;
  w->L = L, w->R = R;
  if (L < R) {
    int M = w->M = L + R >> 1;
    w->lc = build(L, M);
    w->rc = build(M + 1, R);
    w->up();
  };
  return w;
}
void ins(int x, int y) {
  int a = x[top], b = y[top];
  while (a != b) {
    if (a[dep] < b[dep])
      std::swap(a, b), std::swap(x, y);
    _l = a[id], _r = x[id], rt->ins();
    x = a[fa], a = x[top];
  }
  if (x[dep] > y[dep])
    std::swap(x, y);
  if (x != y)
    _l = x[id] + 1, _r = y[id], rt->ins();
}
void del(int x, int y) {
  int a = x[top], b = y[top];
  while (a != b) {
    if (a[dep] < b[dep])
      std::swap(a, b), std::swap(x, y);
    _l = a[id], _r = x[id], rt->del();
    x = a[fa], a = x[top];
  }
  if (x[dep] > y[dep])
    std::swap(x, y);
  if (x != y)
    _l = x[id] + 1, _r = y[id], rt->del();
}
int main() {
  scanf("%d", &n);
  for (int i = 1, a, b; i < n; ++i) {
    scanf("%d%d", &a, &b);
    e[ep] = (edge){b, e0[a]};
    e0[a] = ep++;
    e[ep] = (edge){a, e0[b]};
    e0[b] = ep++;
  }
  f1(1, 0), f2(1, 1);
  rt = build(2, n);
  for (int i = 1, a, b; i < n; ++i) {
    scanf("%d%d", &a, &b);
    ps[i][0] = a, ps[i][1] = b, _w = i;
    ins(a, b);
  }
  for (int i = 1; i < n; ++i) {
    if (rt->mn != 1)
      return puts("NO"), 0;
    rt->gmn();
    ed[_w] = 1;
    del(ps[_w][0], ps[_w][1]);
  }
  return puts("YES"), 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define fa hi][0
#define sz hi][1
#define son hi][2
#define dep hi][3
#define top hi][4
#define id hi][5
const int N = 1e5 + 7;
int n;
struct edge {
  int to, nx;
} e[N * 2];
int e0[N], ep = 2, idr[N];
int hi[N][8], idp = 0;
void f1(int w, int pa) {
  w[dep] = pa[dep] + 1;
  w[fa] = pa;
  w[sz] = 1;
  for (int i = e0[w]; i; i = e[i].nx) {
    int u = e[i].to;
    if (u == pa)
      continue;
    f1(u, w);
    w[sz] += u[sz];
    if (u[sz] > w[son][sz])
      w[son] = u;
  }
}
void f2(int w, int tp) {
  w[top] = tp;
  w[id] = ++idp;
  idr[idp] = w;
  if (w[son])
    f2(w[son], tp);
  for (int i = e0[w]; i; i = e[i].nx) {
    int u = e[i].to;
    if (u != w[fa] && u != w[son])
      f2(u, u);
  }
}
int ps[N][2], ed[N];
int min(int a, int b) { return a < b ? a : b; }
int _l, _r, _w;
struct node {
  node *lc, *rc;
  int L, R, M;
  int mn, a;
  std::vector<int> vs;
  void up() { mn = min(lc->mn, rc->mn); }
  void add(int x) { mn += x, a += x; }
  void dn() {
    if (a)
      lc->add(a), rc->add(a), a = 0;
  }
  void ins() {
    if (_l <= L && R <= _r)
      return vs.push_back(_w), add(1);
    dn();
    if (_l <= M)
      lc->ins();
    if (_r > M)
      rc->ins();
    up();
  }
  void del() {
    if (_l <= L && R <= _r)
      return add(-1);
    dn();
    if (_l <= M)
      lc->del();
    if (_r > M)
      rc->del();
    up();
  }
  void gmn() {
    while (vs.size() && ed[vs.back()])
      vs.pop_back();
    if (vs.size())
      _w = vs.back(), vs.pop_back();
    if (L == R) {
      mn = 0x3f3f3f3f;
      return;
    }
    dn();
    (mn == lc->mn ? lc : rc)->gmn();
    up();
  }
} ns[N * 2], *np = ns, *rt;
node *build(int L, int R) {
  node *w = np++;
  w->L = L, w->R = R;
  if (L < R) {
    int M = w->M = L + R >> 1;
    w->lc = build(L, M);
    w->rc = build(M + 1, R);
    w->up();
  };
  return w;
}
void ins(int x, int y) {
  int a = x[top], b = y[top];
  while (a != b) {
    if (a[dep] < b[dep])
      std::swap(a, b), std::swap(x, y);
    _l = a[id], _r = x[id], rt->ins();
    x = a[fa], a = x[top];
  }
  if (x[dep] > y[dep])
    std::swap(x, y);
  if (x != y)
    _l = x[id] + 1, _r = y[id], rt->ins();
}
void del(int x, int y) {
  int a = x[top], b = y[top];
  while (a != b) {
    if (a[dep] < b[dep])
      std::swap(a, b), std::swap(x, y);
    _l = a[id], _r = x[id], rt->del();
    x = a[fa], a = x[top];
  }
  if (x[dep] > y[dep])
    std::swap(x, y);
  if (x != y)
    _l = x[id] + 1, _r = y[id], rt->del();
}
int main() {
  scanf("%d", &n);
  for (int i = 1, a, b; i < n; ++i) {
    scanf("%d%d", &a, &b);
    e[ep] = (edge){b, e0[a]};
    e0[a] = ep++;
    e[ep] = (edge){a, e0[b]};
    e0[b] = ep++;
  }
  f1(1, 0), f2(1, 1);
  rt = build(2, n);
  for (int i = 1, a, b; i < n; ++i) {
    scanf("%d%d", &a, &b);
    ps[i][0] = a, ps[i][1] = b, _w = i;
    ins(a, b);
  }
  for (int i = 1; i < n; ++i) {
    if (rt->mn != 1)
      return puts("NO"), 0;
    rt->gmn();
    ed[_w] = 1;
    del(ps[_w][0], ps[_w][1]);
  }
  return puts("YES"), 0;
}