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

#include <bits/stdc++.h>

using namespace std;

typedef long long i64;
typedef i64 int_t;
typedef vector<int_t> vi;
typedef vector<vi> vvi;

typedef pair<int_t, int_t> pi;

#define tr(c, i) for (auto i = (c).begin(); i != (c).end(); i++)
#define pb push_back
#define sz(a) int((a).size())
#define all(c) (c).begin(), (c).end()
#define REP(s, e, i) for (i = (s); i < (e); ++i)

int main(int argc, char *argv[]) {
  i64 N, i;
  cin >> N;
  vi a(N);
  REP(0, N, i) { cin >> a[i]; }
  vvi edges(N);
  REP(0, N - 1, i) {
    i64 u, v;
    cin >> u >> v;
    u--;
    v--;
    edges[u].pb(v);
    edges[v].pb(u);
  }

  i64 INF = *max_element(all(a)) + 1;
  vi min_vals(N, INF); // the minimum final element of increasing subsequence of
                       // length (i+1)
  vi visited_from(N, -1);
  vi ans(N, -1);
  vector<pi> backup(N);
  stack<i64> st;
  st.push(0);
  i64 prev = -1;
  while (!st.empty()) {
    i64 cur = st.top();
    st.pop();

    // cout << "visit " << cur << endl;

    // restore if necessary
    while (prev != visited_from[cur]) {
      i64 idx = backup[prev].first;
      i64 val = backup[prev].second;
      min_vals[idx] = val;

      // cout << "restore node " << prev << " idx " << idx << " val " << val <<
      // endl;
      prev = visited_from[prev];
    }

    // update
    auto lb = lower_bound(all(min_vals), a[cur]);
    i64 idx = lb - min_vals.begin();
    backup[cur] = pi(idx, min_vals[idx]);
    min_vals[idx] = a[cur];

    ans[cur] = lower_bound(all(min_vals), INF) - min_vals.begin();

    for (auto e : edges[cur]) {
      if (e != visited_from[cur]) {
        st.push(e);
        visited_from[e] = cur;
      }
    }

    prev = cur;
  }

  REP(0, N, i) { cout << ans[i] << endl; }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long i64;
typedef i64 int_t;
typedef vector<int_t> vi;
typedef vector<vi> vvi;

typedef pair<int_t, int_t> pi;

#define tr(c, i) for (auto i = (c).begin(); i != (c).end(); i++)
#define pb push_back
#define sz(a) int((a).size())
#define all(c) (c).begin(), (c).end()
#define REP(s, e, i) for (i = (s); i < (e); ++i)

int main(int argc, char *argv[]) {
  i64 N, i;
  cin >> N;
  vi a(N);
  REP(0, N, i) { cin >> a[i]; }
  vvi edges(N);
  REP(0, N - 1, i) {
    i64 u, v;
    cin >> u >> v;
    u--;
    v--;
    edges[u].pb(v);
    edges[v].pb(u);
  }

  i64 INF = 100000000;
  vi min_vals(N, INF); // the minimum final element of increasing subsequence of
                       // length (i+1)
  vi visited_from(N, -1);
  vi ans(N, -1);
  vector<pi> backup(N);
  stack<i64> st;
  st.push(0);
  i64 prev = -1;
  while (!st.empty()) {
    i64 cur = st.top();
    st.pop();

    // cout << "visit " << cur << endl;

    // restore if necessary
    while (prev != visited_from[cur]) {
      i64 idx = backup[prev].first;
      i64 val = backup[prev].second;
      min_vals[idx] = val;

      prev = visited_from[prev];
      // cout << "restore node " << prev << " idx " << idx << " val " << val <<
      // endl;
    }

    // update
    auto lb = lower_bound(all(min_vals), a[cur]);
    i64 idx = lb - min_vals.begin();
    backup[cur] = pi(idx, min_vals[idx]);
    min_vals[idx] = a[cur];

    ans[cur] = lower_bound(all(min_vals), INF) - min_vals.begin();

    for (auto e : edges[cur]) {
      if (e != visited_from[cur]) {
        st.push(e);
        visited_from[e] = cur;
      }
    }

    prev = cur;
  }

  REP(0, N, i) { cout << ans[i] << endl; }

  return 0;
}