Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << x << endl
#define debugArray(x, n)                                                       \\
  cerr << __LINE__ << ": " << #x << " = {";                                    \\
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \\
    cerr << ((hoge) ? "," : "") << x[hoge];                                    \\
  cerr << "}" << endl
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#endif

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  int a[N];
  for (int i = 0; i < N; i++)
    cin >> a[i];
  vector<vector<int>> graph(N);
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    graph[u].push_back(v);
    graph[v].push_back(u);
  }
  int ans[N];
  vector<int> lis;
  function<void(int, int)> dfs = [&](int v, int p) {
    int i = lower_bound(lis.begin(), lis.end(), a[v]) - lis.begin();
    int cs;
    int pre;
    if (i == lis.size()) {
      lis.push_back(a[v]);
      cs = 1;
    } else {
      pre = lis[i];
      lis[i] = a[v];
      cs = 2;
    }
    ans[v] = lis.size();
    for (int u : graph[v])
      if (u != p) {
        dfs(u, v);
      }
    if (cs == 1)
      lis.pop_back();
    if (cs == 2)
      lis[i] = pre;
  };
  dfs(0, -1);
  for (int i = 0; i < N; i++)
    cout << ans[i] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << x << endl
#define debugArray(x, n)                                                       \\
  cerr << __LINE__ << ": " << #x << " = {";                                    \\
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \\
    cerr << ((hoge) ? "," : "") << x[hoge];                                    \\
  cerr << "}" << endl
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#endif

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  int a[N];
  for (int i = 0; i < N; i++)
    cin >> a[i];
  vector<vector<int>> graph(N);
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    graph[u].push_back(v);
    graph[v].push_back(u);
  }
  int ans[N];
  vector<int> lis;
  function<void(int, int)> dfs = [&](int v, int p) {
    auto itr = lower_bound(lis.begin(), lis.end(), a[v]);
    int cs;
    int pre;
    if (itr == lis.end()) {
      lis.push_back(a[v]);
      cs = 1;
    } else {
      pre = *itr;
      *itr = a[v];
      cs = 2;
    }
    ans[v] = lis.size();
    for (int u : graph[v])
      if (u != p) {
        dfs(u, v);
      }
    if (cs == 1)
      lis.pop_back();
    if (cs == 2)
      *itr = pre;
  };
  dfs(0, -1);
  for (int i = 0; i < N; i++)
    cout << ans[i] << endl;
  return 0;
}