Problem p02698 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) #VariableName
#define LOG(x)                                                                 \
  if (DEBUG)                                                                   \
    cout << TO_STRING(x) << "=" << x << " " << endl;
#define LOG2(x, y)                                                             \
  if (DEBUG)                                                                   \
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << endl;
#define LOG3(x, y, z)                                                          \
  if (DEBUG)                                                                   \
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << " " \
         << TO_STRING(z) << "=" << z << endl;
#define LOG4(w, x, y, z)                                                       \
  if (DEBUG)                                                                   \
    cout << TO_STRING(w) << "=" << w << " " << TO_STRING(x) << "=" << x << " " \
         << TO_STRING(y) << "=" << y << " " << TO_STRING(z) << "=" << z        \
         << endl;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

vector<lli> dp;
vector<lli> G[100100];
lli N;
vector<lli> a, u, v;

map<lli, lli> ans;

void dfs(lli from, lli now) {

  lli updateMethod;
  lli updateIndex;
  lli prevNum;

  auto itr = lower_bound(dp.begin(), dp.end(), a[now]);

  if (itr == dp.end()) {
    dp.push_back(a[now]);
    updateMethod = 1;
  } else {
    updateMethod = 2;
    updateIndex = (itr)-dp.begin();
    prevNum = *itr;
    *itr = a[now];
  }
  ans[now] = dp.size();

  for (auto e : G[now]) {
    if (e == from)
      continue;
    dfs(now, e);
  }

  if (updateMethod == 1) {
    dp.pop_back();
  } else {
    dp[updateIndex] = prevNum;
  }
}

void func() {

  REP(i, 0, N) dp[i] = INF;
  dfs(-1, 0);

  for (auto e : ans)
    cout << e.second << endl;
}

int main() {
  // cout << fixed << setprecision(5);

  long long N;
  scanf("%lld", &N);
  a.resize(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &a[i]);
  }
  u.resize(N - 1);
  v.resize(N - 1);
  for (int i = 0; i < N - 1; i++) {
    scanf("%lld", &u[i]);
    scanf("%lld", &v[i]);
    u[i]--, v[i]--;
    G[u[i]].push_back(v[i]);
    G[v[i]].push_back(u[i]);
  }
  func();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) #VariableName
#define LOG(x)                                                                 \
  if (DEBUG)                                                                   \
    cout << TO_STRING(x) << "=" << x << " " << endl;
#define LOG2(x, y)                                                             \
  if (DEBUG)                                                                   \
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << endl;
#define LOG3(x, y, z)                                                          \
  if (DEBUG)                                                                   \
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << " " \
         << TO_STRING(z) << "=" << z << endl;
#define LOG4(w, x, y, z)                                                       \
  if (DEBUG)                                                                   \
    cout << TO_STRING(w) << "=" << w << " " << TO_STRING(x) << "=" << x << " " \
         << TO_STRING(y) << "=" << y << " " << TO_STRING(z) << "=" << z        \
         << endl;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

vector<lli> dp;
vector<lli> G[200100];
lli N;
vector<lli> a, u, v;

map<lli, lli> ans;

void dfs(lli from, lli now) {

  lli updateMethod;
  lli updateIndex;
  lli prevNum;

  auto itr = lower_bound(dp.begin(), dp.end(), a[now]);

  if (itr == dp.end()) {
    dp.push_back(a[now]);
    updateMethod = 1;
  } else {
    updateMethod = 2;
    updateIndex = (itr)-dp.begin();
    prevNum = *itr;
    *itr = a[now];
  }
  ans[now] = dp.size();

  for (auto e : G[now]) {
    if (e == from)
      continue;
    dfs(now, e);
  }

  if (updateMethod == 1) {
    dp.pop_back();
  } else {
    dp[updateIndex] = prevNum;
  }
}

void func() {

  REP(i, 0, N) dp[i] = INF;
  dfs(-1, 0);

  for (auto e : ans)
    cout << e.second << endl;
}

int main() {
  // cout << fixed << setprecision(5);

  long long N;
  scanf("%lld", &N);
  a.resize(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &a[i]);
  }
  u.resize(N - 1);
  v.resize(N - 1);
  for (int i = 0; i < N - 1; i++) {
    scanf("%lld", &u[i]);
    scanf("%lld", &v[i]);
    u[i]--, v[i]--;
    G[u[i]].push_back(v[i]);
    G[v[i]].push_back(u[i]);
  }
  func();
  return 0;
}