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

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &p) {
  return out << "(" << p.first << ", " << p.second << ")";
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
template <class T> inline void YES(T condition) {
  if (condition)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
template <class T> inline void Yes(T condition) {
  if (condition)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
template <class T> T gcd(T a, T b) {
  if (a && b) {
    return gcd(min(a, b), max(a, b) % min(a, b));
  } else {
    return a;
  }
}
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
const int MOD = 2019;
long long qpow(long long a, long long b) {
  if (b == 0)
    return 1;
  long long res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<int> w(n + 1);
  vector<vector<int>> nei(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> w[i];
  for (int j = 1; j < n; j++) {
    int u, v;
    cin >> u >> v;
    nei[u].push_back(v);
    nei[v].push_back(u);
  }
  vector<int> seen(n + 1, 0);
  seen[1] = 1;
  vector<int> res(n + 1, 0);
  res[1] = 1;
  vector<int> inc = {w[1]};
  function<void(int)> dfs = [&](int x) {
    for (auto &y : nei[x]) {
      if (seen[y])
        continue;
      seen[y] = 1;
      int wy = w[y];
      if (wy > inc.back()) {
        inc.push_back(wy);
        res[y] = (int)inc.size();
        dfs(y);
        inc.pop_back();
      } else {
        int l = 0, r = inc.size() - 1;
        while (l < r) {
          int mid = (l + r) >> 1;
          if (wy <= inc[mid])
            r = mid;
          else
            l = mid + 1;
        }
        int tmp = inc[r];
        inc[r] = wy;
        res[y] = (int)inc.size();
        dfs(y);
        inc[r] = tmp;
      }
    }
  };
  dfs(1);
  for (int i = 1; i <= n; i++)
    cout << res[i] << '\\n';

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &p) {
  return out << "(" << p.first << ", " << p.second << ")";
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
template <class T> inline void YES(T condition) {
  if (condition)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
template <class T> inline void Yes(T condition) {
  if (condition)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
template <class T> T gcd(T a, T b) {
  if (a && b) {
    return gcd(min(a, b), max(a, b) % min(a, b));
  } else {
    return a;
  }
}
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
const int MOD = 2019;
long long qpow(long long a, long long b) {
  if (b == 0)
    return 1;
  long long res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<int> w(n + 1);
  vector<vector<int>> nei(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> w[i];
  for (int j = 1; j < n; j++) {
    int u, v;
    cin >> u >> v;
    nei[u].push_back(v);
    nei[v].push_back(u);
  }
  vector<int> seen(n + 1, 0);
  seen[1] = 1;
  vector<int> res(n + 1, 0);
  res[1] = 1;
  vector<int> inc = {w[1]};
  function<void(int)> dfs = [&](int x) {
    for (auto &y : nei[x]) {
      if (seen[y])
        continue;
      seen[y] = 1;
      int wy = w[y];
      if (wy > inc.back()) {
        inc.push_back(wy);
        res[y] = (int)inc.size();
        dfs(y);
        inc.pop_back();
      } else {
        auto it = lower_bound(inc.begin(), inc.end(), wy);
        int tmp = *it;
        *it = wy;
        res[y] = (int)inc.size();
        dfs(y);
        *it = tmp;
      }
    }
  };
  dfs(1);
  for (int i = 1; i <= n; i++)
    cout << res[i] << '\\n';

  return 0;
}