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

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
#define FOR(i, begin, end)                                                     \\
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
constexpr int MOD = 1000000007;
constexpr int INF = 2147483647;
void yes(bool expr) { cout << (expr ? "Yes" : "No") << "\\n"; }
struct Node;
vector<Node> V;
vector<int> ans;
vector<int> init;
vector<int> init2;
vector<int> init3;
vector<int> dp;
struct Node {
  int idx;
  int a;
  vector<int> nbr;
  bool vst = false;
  void dfs() {
    vst = true;
    if (dp.size() == 0 || a > dp[dp.size() - 1]) {
      dp.push_back(a);
      init.push_back(1);
    } else {
      int l = 0, r = dp.size() - 1;
      while (r - l >= 0) {
        int m = (l + r) / 2;
        if (a <= dp[m]) {
          r = m - 1;
        } else {
          l = m + 1;
        }
      }
      init.push_back(0);
      init2.push_back(dp[l]);
      init3.push_back(l);
      dp[l] = a;
    }
    ans[idx] = dp.size();
    for (int x : nbr) {
      if (!V[x].vst) {
        V[x].dfs();
      }
    }
    if (init.size() > 0 && init[init.size() - 1] == 0) {
      dp[init3[init3.size() - 1]] = init2[init2.size() - 1];
      init.pop_back();
      init2.pop_back();
      init3.pop_back();
    } else if (init.size() > 0) {
      dp.pop_back();
      init.pop_back();
    }
  };
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int N;
  cin >> N;
  REP(i, N) {
    V.emplace_back();
    V[i].idx = i;
    int a;
    cin >> a;
    V[i].a = a;
  }
  REP(i, N - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    V[u].nbr.push_back(v);
    V[v].nbr.push_back(u);
  }
  ans = vector<int>(N, 0);
  V[0].dfs();
  REP(i, N) cout << ans[i] << "\\n";
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
#define FOR(i, begin, end)                                                     \\
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
constexpr int MOD = 1000000007;
constexpr int INF = 2147483647;
void yes(bool expr) { cout << (expr ? "Yes" : "No") << "\\n"; }
struct Node;
vector<Node> V;
vector<int> ans;
vector<int> init;
vector<int> init2;
vector<int> init3;
vector<int> dp;
struct Node {
  int idx;
  int a;
  vector<int> nbr;
  bool vst = false;
  void dfs() {
    vst = true;
    if (dp.size() == 0 || a > dp[dp.size() - 1]) {
      dp.push_back(a);
      init.push_back(1);
    } else if (nbr.size() != 1) {
      int l = 0, r = dp.size() - 1;
      while (r - l >= 0) {
        int m = (l + r) / 2;
        if (a <= dp[m]) {
          r = m - 1;
        } else {
          l = m + 1;
        }
      }
      init.push_back(0);
      init2.push_back(dp[l]);
      init3.push_back(l);
      dp[l] = a;
    }
    ans[idx] = dp.size();
    for (int x : nbr) {
      if (!V[x].vst) {
        V[x].dfs();
      }
    }
    if (init[init.size() - 1] == 0) {
      dp[init3[init3.size() - 1]] = init2[init2.size() - 1];
      init.pop_back();
      init2.pop_back();
    } else {
      dp.pop_back();
      init.pop_back();
    }
  };
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int N;
  cin >> N;
  REP(i, N) {
    V.emplace_back();
    V[i].idx = i;
    int a;
    cin >> a;
    V[i].a = a;
  }
  REP(i, N - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    V[u].nbr.push_back(v);
    V[v].nbr.push_back(u);
  }
  ans = vector<int>(N, 0);
  V[0].dfs();
  REP(i, N) cout << ans[i] << "\\n";
}