alexjercan/bugnet huggingface dataset:
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
int a[maxn], tot = 0;
int dp[maxn], head[maxn];
int ans[maxn];
int n;
struct node {
  int next, to;
} edge[maxn * 2];
void add_edge(int u, int v) {
  tot++;
  edge[tot].next = head[u];
  edge[tot].to = v;
  head[u] = tot;
}
// lower bound
int binary_search(int low, int high, int key) {
  int mid, ans = 1;
  while (low <= high) {
    mid = (high + low) >> 1;
    if (dp[mid] >= key)
      ans = mid, high = mid - 1;
    else
      low = mid + 1;
  }
  return ans;
}
void dfs(int u, int fa) {
  int pos = binary_search(1, n, a[u]);
  int tmp = dp[pos];
  dp[pos] = a[u];
  ans[u] = max(ans[fa], pos);
  for (int i = head[u]; i; i = edge[i].next) {
    int v = edge[i].to;
    if (v == fa)
      continue;
    dfs(v, u);
  }
  dp[pos] = tmp;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1, u, v; i <= n - 1; i++) {
    scanf("%d%d", &u, &v);
    add_edge(u, v);
    add_edge(v, u);
  }
  memset(dp, 0x3f, sizeof(dp));
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    printf("%d\\n", ans[i]);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200004;
int head[maxn], tot = 0;
int a[maxn];
int dp[maxn];
vector<int> G;
struct node {
  int next, to;
} edge[maxn];
void add_edge(int u, int v) {
  tot++;
  edge[tot].next = head[u];
  edge[tot].to = v;
  head[u] = tot;
}
void dfs(int u, int fa) {
  dp[u] = 1;
  for (auto i : G) {
    if (a[i] < a[u])
      dp[u] = max(dp[i] + 1, dp[u]);
  }
  for (int i = head[u]; i; i = edge[i].next) {
    int v = edge[i].to;
    if (v == fa)
      continue;
    G.push_back(v);
    dfs(v, u);
    G.pop_back();
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n - 1; i++) {
    int tmp, tmp2;
    scanf("%d%d", &tmp, &tmp2);
    add_edge(tmp, tmp2);
    add_edge(tmp2, tmp);
  }
  G.push_back(1);
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    printf("%d\\n", dp[i]);
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
 * @author:  codancer
 * @createTime:  2020-05-04, 20:44:59
 */
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
#define pb push_back
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define fep(i, a, b) for (int i = (a); i >= (b); i--)
typedef vector<int> VI;
typedef vector<ll> VII;
typedef pair<int, int> pii;
const int N = 2e5 + 100;
vector<int> G[N];
ll dp[N], w[N], ans[N];
int n;
void dfs(int u, int fa) {
  int x = lower_bound(dp, dp + n, w[u]) - dp;
  ll val = dp[x];
  dp[x] = w[u];
  ans[u] = lower_bound(dp, dp + n, inf) - dp;
  for (int v : G[u]) {
    if (v == fa)
      continue;
    dfs(v, u);
  }
  dp[x] = val;
}
int main() {
  cin >> n;
  rep(i, 0, n) dp[i] = inf;
  rep(i, 0, n - 1) { cin >> w[i]; }
  int u, v;
  rep(i, 1, n - 1) {
    cin >> u >> v;
    --u;
    --v;
    G[u].pb(v);
    G[v].pb(u);
  }
  dfs(0, -1);
  rep(i, 0, n - 1) cout << ans[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

/*
 * @author:  codancer
 * @createTime:  2020-05-02, 21:06:46
 */
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
#define pb push_back
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define fep(i, a, b) for (int i = (a); i >= (b); i--)
typedef vector<int> VI;
typedef vector<ll> VII;
typedef pair<int, int> pii;
const int N = 2e5 + 100;
ll n, w[N], u, v;
multiset<int> st[N];
vector<int> G[N];
int ans[N];
void dfs(int x, int rt) {
  if (st[rt].size() == 0) {
    ans[x] = 1;
    st[x].insert(w[x]);
  } else {
    auto it = st[rt].end();
    --it;
    if (w[x] > *it) {
      ans[x] = ans[rt] + 1;
      st[x] = st[rt];
      st[x].insert(w[x]);
    } else {
      st[x] = st[rt];
      ans[x] = ans[rt];
      auto itt = st[x].lower_bound(w[x]);
      st[x].erase(itt);
      st[x].insert(w[x]);
    }
  }
  for (int v : G[x]) {
    if (v == rt)
      continue;
    dfs(v, x);
  }
  st[x].clear();
}
int main() {
  cin >> n;
  rep(i, 1, n) cin >> w[i];
  rep(i, 1, n - 1) {
    cin >> u >> v;
    G[u].pb(v);
    G[v].pb(u);
  }
  ans[0] = 0;
  dfs(1, 0);
  rep(i, 1, n) cout << ans[i] << endl;
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2e5 + 5;
int ans[MAXN];
vector<int> G[MAXN];
vector<pii> tag___[MAXN];
vector<int> pool;
int VV[MAXN];

int bit[MAXN];
int Q(int x) {
  int ret = 0;
  while (x) {
    ret = max(ret, bit[x]);
    x -= x & (-x);
  }
  return ret;
}
void M(int x, int v, int u) {
  while (x <= (int)pool.size()) {
    tag___[u].push_back({x, bit[x]});
    bit[x] = max(bit[x], v);
    x += x & (-x);
  }
}

int magic(int x) {
  return lower_bound(pool.begin(), pool.end(), x) - pool.begin() + 1;
}
void dfs(int u, int p) {
  int x = magic(VV[u]);
  int nt = Q(x - 1) + 1;
  M(x, nt, u);
  ans[u] = Q(pool.size());
  for (auto &v : G[u]) {
    if (v == p)
      continue;
    dfs(v, u);
  }
  for (auto &p : tag___[u]) {
    bit[p.F] = p.S;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> VV[i];
    pool.push_back(VV[i]);
  }
  sort(pool.begin(), pool.end());
  pool.erase(unique(pool.begin(), pool.end()), pool.end());
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << '\\n';
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2e5 + 5;
int ans[MAXN];
vector<int> G[MAXN];
vector<pii> tag___[MAXN];
vector<int> pool;
int VV[MAXN];

int bit[MAXN];
int Q(int x) {
  int ret = 0;
  while (x) {
    ret = max(ret, bit[x]);
    x -= x & (-x);
  }
  return ret;
}
int M(int x, int v, int u) {
  while (x <= (int)pool.size()) {
    tag___[u].push_back({x, bit[x]});
    bit[x] = max(bit[x], v);
    x += x & (-x);
  }
}

int magic(int x) {
  return lower_bound(pool.begin(), pool.end(), x) - pool.begin() + 1;
}
void dfs(int u, int p) {
  int x = magic(VV[u]);
  int nt = Q(x - 1) + 1;
  M(x, nt, u);
  ans[u] = Q(pool.size());
  for (auto &v : G[u]) {
    if (v == p)
      continue;
    dfs(v, u);
  }
  for (auto &p : tag___[u]) {
    bit[p.F] = p.S;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> VV[i];
    pool.push_back(VV[i]);
  }
  sort(pool.begin(), pool.end());
  pool.erase(unique(pool.begin(), pool.end()), pool.end());
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << '\\n';
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
int r[200001] = {0};
ll inf = 1e18;
vec G[200001], A(200001), dp(200001, inf), ans(200001);
int dfs(int x) {
  if (r[x])
    return 0;
  r[x] = 1;
  ll a = lower_bound(all(dp), A[x]) - dp.begin(),
     b = *lower_bound(all(dp), A[x]);
  *lower_bound(all(dp), A[x]) = A[x];
  ans[x] = lower_bound(all(dp), inf) - dp.begin();
  for (auto i : G[x])
    dfs(i);
  dp[a] = b;
  return 0;
}
int main() {
  int n, u, v;
  cin >> n;
  rep(i, n) cin >> A[i + 1];
  rep(i, n - 1) cin >> u >> v, G[u].push_back(v), G[v].push_back(u);
  dfs(1);
  rep(i, n) cout << ans[i + 1] << "\\n";
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
int r[200001] = {0};
ll inf = 1e18;
vec G[200001], A(200001), dp(200001, inf), ans(200001);
int dfs(int x) {
  if (r[x])
    return 0;
  r[x] = 1;
  ll a = lower_bound(all(dp), A[x]) - dp.begin(),
     b = *lower_bound(all(dp), A[x]);
  *lower_bound(all(dp), A[x]) = A[x];
  ans[x] = lower_bound(all(dp), inf) - dp.begin();
  for (auto i : G[x])
    dfs(i);
  dp[a] = b;
}
int main() {
  int n, u, v;
  cin >> n;
  rep(i, n) cin >> A[i + 1];
  rep(i, n - 1) cin >> u >> v, G[u].push_back(v), G[v].push_back(u);
  dfs(1);
  rep(i, n) cout << ans[i + 1] << "\\n";
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 4;
vector<int> g[N];
int n, u, v;
int a[N], ans[N];
map<int, int> mp;
set<int> st;
vector<int> removed(N, -1);
vector<int> run;

void dfs(int x, int p = 0) {
  assert(a[x]);
  int id = -1, val = -1;
  if (run.empty()) {
    run.push_back(a[x]);
  } else if (a[x] > run.back()) {
    run.push_back(a[x]);
  } else {
    auto it = lower_bound(run.begin(), run.end(), a[x]) - run.begin();
    if (it == run.size())
      --it;
    assert(it >= 0 && it < run.size());
    assert(a[x] > 0);
    val = run[it], id = it;
    // assert()
    run[it] = a[x];
  }
  ans[x] = run.size();
  // cout << "Going inside  " << x << '\\n';
  // for (int x: run) {
  //   cout << x << ' ';
  // }
  // cout << '\\n';
  for (auto it : g[x]) {
    if (it != p) {
      dfs(it, x);
    }
  }
  if (id != -1) {
    run[id] = val;
  } else
    run.pop_back();
  // cout << "Going out of " << x << '\\n';
  // for (int x: run) {
  //   cout << x << ' ';
  // }
  // cout << '\\n';
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  for (int i = 1; i < n; ++i) {
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; ++i) {
    cout << ans[i] << '\\n';
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 4;
vector<int> g[N];
int n, u, v;
int a[N], ans[N];
map<int, int> mp;
set<int> st;
vector<int> removed(N, -1);

void dfs(int x, int p = 0) {
  ++mp[a[x]];
  auto it = st.lower_bound(a[x]);
  if (it != st.end()) {
    removed[x] = *it;
    st.erase(it);
    st.insert(a[x]);
  }
  if (it == st.end())
    st.insert(a[x]);
  ans[x] = st.size();
  //  cout << "x: " << x << '\\n';
  // for (int x: st) cout << x << ' ';
  //   cout  << '\\n';
  for (auto it : g[x]) {
    if (it != p) {
      dfs(it, x);
    }
  }
  int t = --mp[a[x]];
  if (t <= 0) {
    st.erase(a[x]);
    if (~removed[x] && removed[x] != a[x])
      st.insert(removed[x]);
  }
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  assert(n <= 1000);
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  for (int i = 1; i < n; ++i) {
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; ++i) {
    cout << ans[i] << '\\n';
  }
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------

int N;
int A[201010];
vector<int> V;
int ret[201010];
vector<int> E[201010];

void dfs(int cur, int pre) {
  int pos = lower_bound(ALL(V), A[cur]) - V.begin();
  int rev;
  if (pos == V.size()) {
    rev = -1;
    V.push_back(A[cur]);
  } else {
    rev = V[pos];
    V[pos] = A[cur];
  }
  ret[cur] = V.size();

  FORR(e, E[cur]) if (e != pre) dfs(e, cur);

  if (rev == -1) {
    V.pop_back();
  } else {
    V[pos] = rev;
  }
}

void solve() {
  int i, j, k, l, r, x, y;
  string s;

  cin >> N;
  FOR(i, N) cin >> A[i];
  FOR(i, N - 1) {
    cin >> x >> y;
    E[x - 1].push_back(y - 1);
    E[y - 1].push_back(x - 1);
  }
  dfs(0, 0);

  FOR(i, N) cout << ret[i] << endl;
}

int main(int argc, char **argv) {
  string s;
  int i;
  if (argc == 1)
    ios::sync_with_stdio(false), cin.tie(0);
  FOR(i, argc - 1) s += argv[i + 1], s += '\\n';
  FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------

int N;
int A[101010];
vector<int> V;
int ret[101010];
vector<int> E[101010];

void dfs(int cur, int pre) {
  int pos = lower_bound(ALL(V), A[cur] + 1) - V.begin();
  int rev;
  if (pos == V.size()) {
    rev = -1;
    V.push_back(A[cur]);
  } else {
    rev = V[pos];
    V[pos] = A[cur];
  }
  ret[cur] = V.size();

  FORR(e, E[cur]) if (e != pre) dfs(e, cur);

  if (rev == -1) {
    V.pop_back();
  } else {
    V[pos] = rev;
  }
}

void solve() {
  int i, j, k, l, r, x, y;
  string s;

  cin >> N;
  FOR(i, N) cin >> A[i];
  FOR(i, N - 1) {
    cin >> x >> y;
    E[x - 1].push_back(y - 1);
    E[y - 1].push_back(x - 1);
  }
  dfs(0, 0);

  FOR(i, N) cout << ret[i] << endl;
}

int main(int argc, char **argv) {
  string s;
  int i;
  if (argc == 1)
    ios::sync_with_stdio(false), cin.tie(0);
  FOR(i, argc - 1) s += argv[i + 1], s += '\\n';
  FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using Graph = vector<vector<int>>;
typedef pair<int, int> P;
typedef long long ll;

const int MAX_N = 200200;
const int INF = 1002003004;

vector<int> a, ans;
vector<int> to[MAX_N];
int dp[MAX_N];

void dfs(int v, int p) {
  int i = lower_bound(dp, dp + MAX_N, a[v]) - dp;
  int old = dp[i];
  dp[i] = a[v];

  ans[v] = i;
  if (p != -1)
    ans[v] = max(ans[v], ans[p]);

  for (auto u : to[v]) {
    if (u == p)
      continue;
    dfs(u, v);
  }
  dp[i] = old;
}

int main() {
  int n;
  cin >> n;

  a.resize(n);
  ans.resize(n);
  rep(i, n) cin >> a[i];

  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    ;

    to[v].push_back(u);
    to[u].push_back(v);
  }

  rep(i, MAX_N) dp[i] = INF;
  dp[0] = -INF;
  dfs(0, -1);

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

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using Graph = vector<vector<int>>;
typedef pair<int, int> P;
typedef long long ll;

const int MAX_N = 200200;
const int INF = 1002003004;
int dp[MAX_N];

vector<int> a, ans;
vector<int> to[200200];

void dfs(int v, int p = -1) {
  int i = lower_bound(dp, dp + MAX_N, a[v]) - dp;
  int old = dp[i];
  dp[i] = a[v];

  if (p != -1)
    ans[v] = max(ans[v], ans[p]);

  for (auto u : to[v]) {
    if (u == p)
      continue;
    dfs(u, v);
  }
}

int main() {
  int n;
  cin >> n;

  a.resize(n);
  rep(i, n) cin >> a[i];

  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    ;

    to[v].push_back(u);
    to[u].push_back(v);
  }

  rep(i, MAX_N) dp[i] = INF;
  dp[0] = -INF;
  dfs(0);

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

  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <string>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int n;
int h[N], e[N * 2], ne[N * 2], w[N], idx;
vector<int> path;
int res[N];

void add(int u, int v) {
  e[idx] = v;
  ne[idx] = h[u];
  h[u] = idx++;
}

void dfs(int curr, int prev) {
  // cout << curr << endl;
  auto it = lower_bound(path.begin(), path.end(), w[curr]);
  int form = -1;
  int pos = it - path.begin();
  if (pos == path.size()) {
    path.push_back(w[curr]);
  } else {
    form = path[pos];
    path[pos] = w[curr];
  }
  res[curr] = path.size();
  for (int i = h[curr]; i != -1; i = ne[i]) {
    int j = e[i];
    if (j != prev) {
      dfs(j, curr);
    }
  }
  if (form == -1) {
    path.pop_back();
  } else {
    path[pos] = form;
  }
}

int main() {
  memset(h, -1, sizeof h);
  int u, v;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> w[i];
  }
  for (int i = 1; i < n; i++) {
    cin >> u >> v;
    add(u, v);
    add(v, u);
  }
  dfs(1, -1);
  for (int i = 1; i <= n; i++) {
    cout << res[i] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int n;
int h[N], e[N * 2], ne[N * 2], w[N], idx;
vector<int> path;
int res[N];

void add(int u, int v) {
  e[idx] = v;
  ne[idx] = h[u];
  h[u] = idx++;
}

void dfs(int curr, int prev) {
  auto it = upper_bound(path.begin(), path.end(), w[curr]);
  bool flag = false;
  int former = -1;
  if (it == path.end()) {
    flag = true;
    path.push_back(w[curr]);
  } else {
    former = *it;
    *it = w[curr];
  }
  res[curr] = path.size();
  for (int i = h[curr]; i != -1; i = ne[i]) {
    int j = e[i];
    if (j != prev) {
      dfs(j, curr);
    }
  }
  if (flag) {
    path.pop_back();
  } else {
    *it = former;
  }
}

int main() {
  memset(h, -1, sizeof h);
  int u, v;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> w[i];
  }
  for (int i = 1; i < n; i++) {
    cin >> u >> v;
    add(u, v);
    add(v, u);
  }
  dfs(1, -1);
  for (int i = 1; i <= n; i++) {
    cout << res[i] << endl;
  }
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int MAX_N = 200005;
const int INF = 1001001001;

vector<int> a, ans;
vector<int> to[MAX_N];
int dp[MAX_N];

void dfs(int v, int p = -1) {
  int i = lower_bound(dp, dp + MAX_N, a[v]) - dp;
  int old = dp[i];
  dp[i] = a[v];
  ans[v] = i;
  if (p != -1)
    ans[v] = max(ans[v], ans[p]);
  for (int u : to[v]) {
    if (u == p)
      continue;
    dfs(u, v);
  }
  dp[i] = old;
}

int main() {
  int n;
  cin >> n;
  a = ans = vector<int>(n);
  rep(i, n) cin >> a[i];
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    to[v].push_back(u);
    to[u].push_back(v);
  }
  rep(i, MAX_N) dp[i] = INF;
  dp[0] = -INF;
  dfs(0);
  rep(i, n) cout << ans[i] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
const int INF = 1001001001;
const int MAX_N = 20005;

vector<int> a, ans;
vector<int> to[MAX_N];
int dp[MAX_N];
void dfs(int s, int p = -1) {
  int i = lower_bound(dp, dp + MAX_N, a[s]) - dp;
  int old = dp[i];
  dp[i] = a[s];
  ans[s] = i;
  if (p != -1)
    ans[s] = max(ans[s], ans[p]); // exclude first turn

  for (int t : to[s]) {
    if (t == p)
      continue; // parent
    dfs(t, s);
  }
  // roll back
  dp[i] = old;
}

int main() {
  int n;
  cin >> n;
  a = ans = vector<int>(n);
  rep(i, n) { cin >> a[i]; }

  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    to[u].push_back(v);
    to[v].push_back(u);
  }

  rep(i, MAX_N) dp[i] = INF;
  dp[0] = -INF;

  dfs(0);
  rep(i, n) { cout << ans[i] << endl; }
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
const int INF = 1001001001;
const int MAX_N = 200005;

vector<int> a, ans;
vector<int> to[MAX_N];
int dp[MAX_N];
void dfs(int v, int p = -1) {
  int i = lower_bound(dp, dp + MAX_N, a[v]) - dp;
  int old = dp[i];
  dp[i] = a[v];
  ans[v] = i;
  if (p != -1)
    ans[v] = max(ans[v], ans[p]); // exclude first turn

  for (int u : to[v]) {
    if (u == p)
      continue; // parent
    dfs(u, v);
  }
  // roll back
  dp[i] = old;
}

int main() {
  int n;
  cin >> n;
  a = ans = vector<int>(n);
  rep(i, n) { cin >> a[i]; }

  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    to[u].push_back(v);
    to[v].push_back(u);
  }

  rep(i, MAX_N) dp[i] = INF;
  dp[0] = -INF;

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


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
const int INF = 1001001001;
const int MAX_N = 20005;

vector<int> a, ans;
vector<int> to[MAX_N];
int dp[MAX_N];
void dfs(int v, int p = -1) {
  int i = lower_bound(dp, dp + MAX_N, a[v]) - dp;
  int old = dp[i];
  dp[i] = a[v];
  ans[v] = i;
  if (p != -1)
    ans[v] = max(ans[v], ans[p]); // exclude first turn

  for (int u : to[v]) {
    if (u == p)
      continue; // parent
    dfs(u, v);
  }
  // roll back
  dp[i] = old;
}

int main() {
  int n;
  cin >> n;
  a = ans = vector<int>(n);
  rep(i, n) { cin >> a[i]; }

  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    to[u].push_back(v);
    to[v].push_back(u);
  }

  rep(i, MAX_N) dp[i] = INF;
  dp[0] = -INF;

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