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

#pragma GCC optimize("O3")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
using P = pair<int, int>;
using T = tuple<int, int, int>;

template <class T> inline T chmax(T &a, const T b) {
  return a = (a < b) ? b : a;
}
template <class T> inline T chmin(T &a, const T b) {
  return a = (a > b) ? b : a;
}

constexpr int MOD = 1e9 + 7;
constexpr int inf = 1e9;
constexpr long long INF = 1e18;

#define all(a) (a).begin(), (a).end()

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

vector<int> a(200010);
vector<int> ans(200010);
vector<int> dp(200010, inf);

void dfs(int cv, int pv, vector<vector<int>> &G) {
  int idx = lower_bound(dp.begin(), dp.end(), a[cv]) - dp.begin();
  int pre = dp[idx];
  dp[idx] = a[cv];

  ans[cv] = lower_bound(dp.begin(), dp.end(), inf) - dp.begin();

  for (auto nv : G[cv]) {
    if (nv == pv)
      continue;

    dfs(nv, cv, G);
  }

  dp[idx] = pre;

  return;
}

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

  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<vector<int>> G(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }

  dfs(0, -1, G);

  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;

  return 0;
}


Error: 0

Fixed submission:

#pragma GCC optimize("O3")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
using P = pair<int, int>;
using T = tuple<int, int, int>;

template <class T> inline T chmax(T &a, const T b) {
  return a = (a < b) ? b : a;
}
template <class T> inline T chmin(T &a, const T b) {
  return a = (a > b) ? b : a;
}

constexpr int MOD = 1e9 + 7;
constexpr int inf = 1e9;
constexpr long long INF = 1e18;

#define all(a) (a).begin(), (a).end()

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

vector<int> a(200010);
vector<int> ans(200010);

int LIS(vector<int> &a) {
  // dp[i] := 長さが i+1 であるような増加部分列における最終要素の最小値
  int n = a.size();
  vector<int> dp(n, inf);
  for (int i = 0; i < n; i++) {
    *lower_bound(dp.begin(), dp.end(), a[i]) = a[i];
  }

  return lower_bound(dp.begin(), dp.end(), inf) - dp.begin();
}

void dfs(int cv, int pv, vector<vector<int>> &G, vector<int> dp) {
  ans[cv] = lower_bound(dp.begin(), dp.end(), inf) - dp.begin();

  for (auto nv : G[cv]) {
    if (nv == pv)
      continue;

    int tmp_num = *lower_bound(dp.begin(), dp.end(), a[nv]);
    int tmp_id = lower_bound(dp.begin(), dp.end(), a[nv]) - dp.begin();
    *lower_bound(dp.begin(), dp.end(), a[nv]) = a[nv];

    dfs(nv, cv, G, dp);

    dp[tmp_id] = tmp_num;
  }

  return;
}

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

  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<vector<int>> G(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }

  vector<int> dp(n, inf);
  dfs(0, -1, G, dp);

  for (int i = 0; i < n; i++)
    cout << ans[i] + 1 << endl;

  return 0;
}