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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
#define yn                                                                     \\
  { puts("Yes"); }                                                             \\
  else {                                                                       \\
    puts("No");                                                                \\
  }

#define MAX_N 200005
vector<int> pre_G[MAX_N];
vector<int> G[MAX_N];
int par_[MAX_N];
int flag_netsukigi[MAX_N];
void make_netsukigi(int r) {
  queue<int> que;
  que.push(r);
  flag_netsukigi[r] = 1;
  par_[r] = -1;

  while (que.size() > 0) {
    int x = que.front();
    que.pop();
    flag_netsukigi[x] = 1;
    rep(i, pre_G[x].size()) {
      if (flag_netsukigi[pre_G[x][i]] == 0) {
        par_[pre_G[x][i]] = x;
        G[x].push_back(pre_G[x][i]);
        que.push(pre_G[x][i]);
      }
    }
  }
}

const int INF = 1001001001;
int n;
int a[MAX_N];
int dp[MAX_N];
int ans[MAX_N];

void dfs(int x) {
  int ite = lower_bound(dp, dp + n, a[x]) - dp;
  int keep = dp[ite];
  dp[ite] = a[x];
  ans[x] = lower_bound(dp, dp + n, INF) - dp;

  rep(i, G[x].size()) {
    int y = G[x][i];
    dfs(y);
  }

  dp[ite] = keep;
}

int main() {
  cin >> n;
  rep(i, n) { scanf("%d", &a[i]); }

  rep(i, n - 1) {
    int x, y;
    scanf("%d%d", &x, &y);
    x--;
    y--; // 0-indexにするため.
    pre_G[x].push_back(y);
    pre_G[y].push_back(x);
  }

  make_netsukigi(0);

  fill(dp, dp + n, INF);

  dfs(0);

  rep(i, n) { printf("%d\\n", ans[i]); }

  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
#define yn                                                                     \\
  { puts("Yes"); }                                                             \\
  else {                                                                       \\
    puts("No");                                                                \\
  }

#define MAX_N 200005
vector<int> pre_G[MAX_N];
vector<int> G[MAX_N];
int par_[MAX_N];
int flag_netsukigi[MAX_N];
void make_netsukigi(int r) {
  queue<int> que;
  que.push(r);
  flag_netsukigi[r] = 1;
  par_[r] = -1;

  while (que.size() > 0) {
    int x = que.front();
    que.pop();
    flag_netsukigi[x] = 1;
    rep(i, pre_G[x].size()) {
      if (flag_netsukigi[pre_G[x][i]] == 0) {
        par_[pre_G[x][i]] = x;
        G[x].push_back(pre_G[x][i]);
        que.push(pre_G[x][i]);
      }
    }
  }
}

const int INF = 1001001001;
int n;
int a[MAX_N];
int dp[MAX_N];
int ans[MAX_N];

void dfs(int x) {
  int ite = lower_bound(dp, dp + n, a[x]) - dp;
  int keep = dp[ite];
  dp[ite] = a[x];
  ans[x] = lower_bound(dp, dp + n, INF) - dp;

  rep(i, G[x].size()) {
    int y = G[x][i];
    dfs(y);
  }

  dp[ite] = keep;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  rep(i, n) { cin >> a[i]; }

  rep(i, n - 1) {
    int x, y;
    scanf("%d%d", &x, &y);
    x--;
    y--; // 0-indexにするため.
    pre_G[x].push_back(y);
    pre_G[y].push_back(x);
  }

  make_netsukigi(0);

  fill(dp, dp + n, INF);

  dfs(0);

  rep(i, n) { cout << ans[i] << endl; }

  return 0;
}