Problem p03204 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \
  {                                                                            \
    REP(WW, n) cerr << v[WW] << ' ';                                           \
    cerr << endl << endl;                                                      \
  }
#define SHOW2d(v, WW, HH)                                                      \
  {                                                                            \
    REP(W_, WW) {                                                              \
      REP(H_, HH) cerr << v[W_][H_] << ' ';                                    \
      cerr << endl;                                                            \
    }                                                                          \
    cerr << endl;                                                              \
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007

typedef long long ll;
typedef pair<ll, ll> P;

vector<vector<ll>> v(222222);
ll ma[222222];
ll ans[222222];
map<pair<int, int>, int> mp;

int seica(ll num, ll lim, ll pre) {
  if (mp[MP(num, lim)] != 0)
    return mp[MP(num, lim)];
  ll ret = 0;
  REP(i, v[num].size()) {
    if (v[num][i] != pre && v[num][i] < lim) {
      ret += seica(v[num][i], lim, num);
    }
  }
  return ret + 1;
}

void dfs(ll num, ll pre) {
  ll ret = ans[pre];
  ma[num] = max(ma[pre], pre);
  ret += seica(num, ma[num], pre);
  if (ma[pre] > num)
    ret -= seica(num, ma[pre], pre);
  ans[num] = ret;
  REP(i, v[num].size()) {
    if (v[num][i] != pre) {
      dfs(v[num][i], num);
    }
  }
}

int main() {

  int n;
  cin >> n;
  REP(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].EB(b);
    v[b].EB(a);
  }

  REP(i, v[0].size()) { dfs(v[0][i], 0); }

  REP(i, n - 1) cout << ans[i + 1] << " ";
  cout << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \
  {                                                                            \
    REP(WW, n) cerr << v[WW] << ' ';                                           \
    cerr << endl << endl;                                                      \
  }
#define SHOW2d(v, WW, HH)                                                      \
  {                                                                            \
    REP(W_, WW) {                                                              \
      REP(H_, HH) cerr << v[W_][H_] << ' ';                                    \
      cerr << endl;                                                            \
    }                                                                          \
    cerr << endl;                                                              \
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007

typedef long long ll;
typedef pair<ll, ll> P;

vector<vector<ll>> v(222222);
ll ma[222222];
ll ans[222222];
map<pair<int, int>, int> mp;

int seica(ll num, ll lim, ll pre) {
  if (mp[MP(num, lim)] != 0)
    return mp[MP(num, lim)];
  ll ret = 0;
  REP(i, v[num].size()) {
    if (v[num][i] != pre && v[num][i] < lim) {
      ret += seica(v[num][i], lim, num);
    }
  }
  return mp[MP(num, lim)] = ret + 1;
}

void dfs(ll num, ll pre) {
  ll ret = ans[pre];
  ma[num] = max(ma[pre], pre);
  ret += seica(num, ma[num], pre);
  if (ma[pre] > num)
    ret -= seica(num, ma[pre], pre);
  ans[num] = ret;
  REP(i, v[num].size()) {
    if (v[num][i] != pre) {
      dfs(v[num][i], num);
    }
  }
}

int main() {

  int n;
  cin >> n;
  REP(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].EB(b);
    v[b].EB(a);
  }

  REP(i, v[0].size()) { dfs(v[0][i], 0); }

  REP(i, n - 1) cout << ans[i + 1] << " ";
  cout << endl;

  return 0;
}