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

// g++ -std=c++14 test.cpp -o test.out

#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define LL long long int
const LL INF = (1LL << 60);
const int INF_INT = 2147483647 - 1e6 - 1;
const LL mod = 1000000007ll;
const int mod_int = 1000000007;

LL N;
LL A[200001];
vector<vector<LL>> adj;

vector<LL> ans;

vector<LL> LIS_array;
bool used[200001] = {};
void dfs(LL now) {
  used[now] = 1;
  bool pop_flag = false;
  int insert_index =
      int(lower_bound(LIS_array.begin(), LIS_array.end(), A[now]) -
          LIS_array.begin());
  LL prev_num;

  if (insert_index == LIS_array.size()) {
    pop_flag = true;
    LIS_array.push_back(A[now]);
  } else {
    prev_num = LIS_array[insert_index];
    LIS_array[insert_index] = A[now];
  }

  ans[now] = LIS_array.size();

  // cout << now << endl;
  // for(int i=0;i<LIS_array.size();i++) cout << LIS_array[i] << " ";
  // cout << endl;

  for (LL next : adj[now]) {
    if (used[next])
      continue;
    // cout << now << "->" << next << endl;
    dfs(next);
    // cout << now << "<-" << next << endl;
  }

  if (pop_flag)
    LIS_array.pop_back();
  else
    LIS_array[insert_index] = prev_num;
}

void solve() {
  ans = vector<LL>(N + 1);
  dfs(1);
}

int main() {
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> A[i];
  adj = vector<vector<LL>>(N + 1);
  for (int i = 0; i <= N - 2; i++) {
    LL u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  solve();

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

Error: 0

Fixed submission:

// g++ -std=c++14 test.cpp -o test.out
// 問題URL:
// https://atcoder.jp/contests/abc165/tasks/abc165_f

// 参考：
// https://www.youtube.com/watch?v=C5_NnCp1CRI&feature=youtu.be

// ポイント：
// LISの考え方

#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define LL long long int
const LL INF = (1LL << 60);
const int INF_INT = 2147483647 - 1e6 - 1;
const LL mod = 1000000007ll;
const int mod_int = 1000000007;

LL N;
LL A[200000];
vector<vector<LL>> adj;

LL ans[200001];

vector<LL> LIS_array;
vector<LL> used; // 頂点iに訪れたかどうか
void dfs(LL now) {
  vector<LL> LIS_array_preserved;
  if (adj[now].size() >= 2)
    LIS_array_preserved = LIS_array;

  auto iter = lower_bound(LIS_array.begin(), LIS_array.end(), A[now]);
  int index = int(iter - LIS_array.begin());
  if (index < LIS_array.size())
    LIS_array[index] = A[now];
  else
    LIS_array.push_back(A[now]);
  used[now] = 1;

  // for(int i=0;i<LIS_array.size();i++){
  //     cout << LIS_array[i] << " ";
  // }
  // cout << endl;

  ans[now] = LIS_array.size(); // 頂点nowの時点での最長部分列の長さを格納

  for (LL next : adj[now]) {
    if (!used[next])
      dfs(next);
  }
  if (adj[now].size() >= 2)
    LIS_array = LIS_array_preserved;
}

void solve() {
  used = vector<LL>(N + 1, 0);
  dfs(1);
}

int main() {
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> A[i];
  adj = vector<vector<LL>>(N + 1);
  for (int i = 0; i < N - 1; i++) {
    LL u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  solve();

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