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

// #include <tourist>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> p;
const int INF = 1e9 + 5;
const int LINF = int(1e18);
const int MOD = 1000000007;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1},
          Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define debug(v)                                                               \\
  cout << #v << ":";                                                           \\
  for (auto x : v) {                                                           \\
    cout << x << ' ';                                                          \\
  }                                                                            \\
  cout << 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;
}
// cout<<fixed<<setprecision(15);有効数字15桁
//-std=c++14
//-std=gnu++17
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
int n;
vector<int> a;
vector<vector<int>> v;
vector<int> ans;
void dfs(int k, int prev, vector<int> &dp) {
  // debug(dp);
  // cout<<k<<"\\n";
  int temp;
  auto x = lower_bound(ALL(dp), a[k]);
  bool check = false;
  int j = x - dp.begin();
  if (x == dp.end()) {
    dp.push_back(a[k]);
    check = true;
  } else {
    temp = *x;
    *x = a[k];
  }
  ans[k] = dp.size();
  // cout<<ans[k]<<"\\n";
  /*
  debug(dp);
  cout<<k<<"\\n";
  */
  rep(i, v[k].size()) {
    if (v[k][i] == prev)
      continue;
    dfs(v[k][i], k, dp);
  }
  if (check)
    dp.pop_back();
  else
    dp[j] = temp;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  a.resize(n);
  v.resize(n);
  ans.resize(n);
  rep(i, n) cin >> a[i];
  rep(i, n - 1) {
    int temp, temp1;
    cin >> temp >> temp1;
    temp--;
    temp1--;
    v[temp1].push_back(temp);
    v[temp].push_back(temp1);
  }
  vector<int> dp;
  dfs(0, -1, dp);
  // debug(ans);
  rep(i, n) cout << ans[i] << "\\n";
}


Error: 0

Fixed submission:

// #include <tourist>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> p;
const int INF = 1e9 + 5;
const int LINF = int(1e18);
const int MOD = 1000000007;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1},
          Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define debug(v)                                                               \\
  cout << #v << ":";                                                           \\
  for (auto x : v) {                                                           \\
    cout << x << ' ';                                                          \\
  }                                                                            \\
  cout << 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;
}
// cout<<fixed<<setprecision(15);有効数字15桁
//-std=c++14
//-std=gnu++17
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
int n;
vector<int> a;
vector<vector<int>> v;
vector<int> ans;
void dfs(int k, int prev, vector<int> &dp) {
  // debug(dp);
  // cout<<k<<"\\n";
  int temp;
  auto x = lower_bound(ALL(dp), a[k]);
  bool check = false;
  if (x == dp.end()) {
    dp.push_back(a[k]);
    check = true;
  } else {
    temp = *x;
    *x = a[k];
  }
  ans[k] = dp.size();
  // cout<<ans[k]<<"\\n";
  rep(i, v[k].size()) {
    if (v[k][i] == prev)
      continue;
    dfs(v[k][i], k, dp);
  }
  if (check)
    dp.pop_back();
  else
    *x = temp;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  a.resize(n);
  v.resize(n);
  ans.resize(n);
  rep(i, n) cin >> a[i];
  rep(i, n - 1) {
    int temp, temp1;
    cin >> temp >> temp1;
    temp--;
    temp1--;
    v[temp1].push_back(temp);
    v[temp].push_back(temp1);
  }
  vector<int> dp;
  dfs(0, -1, dp);
  // debug(ans);
  rep(i, n) cout << ans[i] << "\\n";
}