alexjercan/bugnet huggingface dataset:
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++)
#define N 200010
#define INF 1000000005
typedef long long ll;
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) int(x.size())
#define IN(a) cin >> (a)
#define OUT(a) cout << (a) << endl
typedef pair<int, int> P;
const int MAX = 1000005;
const int MOD = 1000000007;
vector<int> a(N);
int length = 1;
stack<P> st;
vector<int> to[N];
vector<int> l(N, INF);
vector<int> ans(N);
void dfs(int no, int p) {
  for (auto s : to[no]) {
    if (s == p)
      continue;
    int t = lower_bound(ALL(l), a[s]) - l.begin();
    st.push({t, l[t]});
    if (l[t] == INF)
      length++;
    l[t] = a[s];
    ans[s] = length;
    dfs(s, no);
    P q = st.top();
    st.pop();
    l[q.first] = q.second;
    if (q.second == INF)
      length--;
  }
}
int main() {
  int n;
  cin >> n;
  REP(i, n) cin >> a[i];
  REP(i, n - 1) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    to[x].push_back(y);
    to[y].push_back(x);
  }
  l[0] = a[0];
  dfs(0, -1);
  ans[0] = 1;
  REP(i, n) OUT(ans[i]);
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define N 200010
#define INF 1000000005
typedef long long ll;
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) int(x.size())
#define IN(a) cin >> (a)
#define OUT(a) cout << (a) << endl
typedef pair<int, int> P;
const int MAX = 1000005;
const int MOD = 1000000007;
vector<int> a(N);
int length = 1;
stack<P> st;
vector<int> to[N];
vector<int> l(N, INF);
vector<int> ans(N);
void dfs(int no, int p) {
  for (auto s : to[no]) {
    if (s == p)
      continue;
    int t = lower_bound(ALL(l), a[s]) - l.begin();
    st.push({t, l[t]});
    if (l[t] == INF)
      length++;
    l[t] = a[s];
    ans[s] = length;
    dfs(s, no);
  }
  P q = st.top();
  st.pop();
  l[q.first] = q.second;
  if (q.second == INF)
    length--;
}
int main() {
  int n;
  cin >> n;
  REP(i, n) cin >> a[i];
  REP(i, n - 1) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    to[x].push_back(y);
    to[y].push_back(x);
  }
  dfs(0, -1);
  ans[0] = 1;
  REP(i, n) OUT(ans[i]);
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long
#define ff first
#define ss second
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define mp make_pair
#define inf 2000000009
#define mod 1000000007
#define ld long double
using namespace std;
ll n;
vector<ll> a;
vector<vector<ll>> adj(300009);
vector<ll> ans;
vector<ll> dp;

pair<ll, pair<ll, ll>> fun(ll x) {
  auto it = lower_bound(dp.begin(), dp.end(), x);
  ll f;
  if (it == dp.end())
    dp.pb(x), f = -1;
  else
    f = *it, *it = x;

  ll z = dp.size();
  return {f, {z, it - dp.begin()}};
}

void dfs(ll node, ll par) {
  pair<ll, pair<ll, ll>> p = fun(a[node]);
  ans[node] = p.ss.ff;

  for (auto j : adj[node])
    if (j != par)
      dfs(j, node);

  if (p.ff == -1)
    dp.pop_back();
  else
    dp[p.ss.ss] = p.ff;
}

int main() {

  fast;
  cin >> n;
  a.assign(n, 0);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ans.assign(n, 1);

  for (ll i = 0; i < n - 1; i++) {
    ll x, y;
    cin >> x >> y;
    x--, y--;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  dfs(0, -1);

  for (ll i = 0; i < n; i++)
    cout << ans[i] << '\\n';
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long
#define ff first
#define ss second
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define mp make_pair
#define inf 2000000009
#define mod 1000000007
#define ld long double
using namespace std;
ll n;
vector<ll> a;
vector<vector<ll>> adj(300009);
vector<ll> ans;
vector<ll> dp;

pair<ll, pair<ll, vector<ll>::iterator>> fun(ll x) {
  auto it = lower_bound(dp.begin(), dp.end(), x);
  ll f;
  if (it == dp.end())
    dp.pb(x), f = -1;
  else
    f = *it, *it = x;

  ll z = dp.size();
  return {f, {z, it}};
}

void dfs(ll node, ll par) {
  pair<ll, pair<ll, vector<ll>::iterator>> p = fun(a[node]);
  ans[node] = p.ss.ff;

  for (auto j : adj[node])
    if (j != par)
      dfs(j, node);

  if (p.ff == -1)
    dp.pop_back();
  else
    *(p.ss.ss) = p.ff;
}

int main() {

  fast;

  cin >> n;
  a.assign(n, 0);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ans.assign(n, 1);

  for (ll i = 0; i < n - 1; i++) {
    ll x, y;
    cin >> x >> y;
    x--, y--;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  dfs(0, -1);

  for (ll i = 0; i < n; i++)
    cout << ans[i] << '\\n';
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

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

const int INF = 1234567890;
const int MAX_N = 200005;
vector<int> v, ans;
vector<int> g[MAX_N];
int dp[MAX_N];

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  v = ans = vector<int>(n);
  rep(i, n) cin >> v[i];
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  rep(i, MAX_N) dp[i] = INF;
  dp[0] = 0;
  dfs(0);
  rep(i, n) cout << ans[i] << endl;
}

Error: 0

Fixed submission:

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

vector<int> v, ans;
vector<int> g[200005];

void dfs(int from, int p, vector<int> s) {
  if (s.empty() || v[from] > s.back()) {
    s.push_back(v[from]);
  } else {
    auto it = upper_bound(s.begin(), s.end(), v[from]);
    *it = v[from];
  }
  ans[from] = s.size();
  for (auto to : g[from]) {
    if (to == p)
      continue;
    dfs(to, from, s);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  v.resize(n);
  ans.resize(n);
  rep(i, n) cin >> v[i];
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(0, -1, {});
  rep(i, n) { cout << ans[i] << endl; }
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
#define int ll
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
const int MAX = 2e5 + 10;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
vector<int> graph[MAX];
int n, arr[MAX];
int ans[MAX];
set<int> lis;

void dfs(int u, int par) {
  ans[u] = ans[par];
  auto it = lis.lower_bound(arr[u]);
  int rem = -1;
  if (it != lis.end()) {
    rem = *it;
    lis.erase(it);
  }
  lis.insert(arr[u]);
  ans[u] = lis.size();
  for (auto v : graph[u]) {
    if (v == par)
      continue;
    dfs(v, u);
  }

  lis.erase(arr[u]);
  if (rem != -1)
    lis.insert(rem);
}

signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);

#ifndef ONLINE_JUDGE
  time_t t1 = clock(), t2;
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  // freopen("err.txt", "w", stderr);
#endif

  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    graph[u].pb(v);
    graph[v].pb(u);
  }

  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
#define int ll
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
const int MAX = 2e5 + 10;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
vector<int> graph[MAX];
int n, arr[MAX];
set<int> ans[MAX];

void dfs(int u, int par) {
  ans[u] = ans[par];
  auto it = ans[u].lower_bound(arr[u]);
  if (it != ans[u].end())
    ans[u].erase(it);
  ans[u].insert(arr[u]);

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

signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    graph[u].pb(v);
    graph[v].pb(u);
  }

  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    cout << ans[i].size() << "\\n";
  return 0;
}
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;
const int SIZE = 200000;

vector<int> G[SIZE];
vector<int> ans(SIZE), a(SIZE);
vector<int> L;
void dfs(int k = 0, int p = -1) {
  int flag = -1;
  int itr = lower_bound(L.begin(), L.end(), a[k]) - L.begin();
  if (itr == L.size()) {
    L.push_back(a[k]);
  } else {
    flag = L[itr];
    L[itr] = a[k];
  }
  ans[k] = L.size();
  for (auto &&i : G[k]) {
    if (i == p)
      continue;
    dfs(i, k);
  }
  if (!L.empty() && flag == -1)
    L.pop_back();
  else
    L[itr] = flag;
}
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs();
  rep(i, n) cout << ans[i] << endl;
}

Error: 0

Fixed submission:

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

vector<int> G[SIZE];
vector<int> ans(SIZE), a(SIZE);
vector<int> L;
void dfs(int k = 0, int p = -1) {
  int flag = -1;
  vector<int>::iterator itr = lower_bound(L.begin(), L.end(), a[k]);
  if (itr == L.end()) {
    L.push_back(a[k]);
  } else {
    flag = *itr;
    *itr = a[k];
  }
  ans[k] = L.size();
  for (auto &&i : G[k]) {
    if (i == p)
      continue;
    dfs(i, k);
  }
  if (flag == -1)
    L.pop_back();
  else
    *itr = flag;
}
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs();
  rep(i, n) cout << ans[i] << endl;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
vector<int> a[200010];
multiset<pair<int, int>> st;
int ans[200010], b[200010];
void dfs(int x, int pa) {
  auto tmp = st.lower_bound(mk(b[x], -1));
  --tmp;
  int xx = (tmp->sc) + 1;
  vector<pair<int, int>> v;
  auto store_it = mk(b[x], xx);
  ans[x] = max(xx, (*--st.end()).sc);
  ++tmp;
  while (1) {
    if (tmp == st.end()) {
      break;
    }
    if (xx >= (tmp->sc)) {
      auto it1 = tmp;
      ++it1;
      v.push_back(*tmp);
      st.erase(tmp);
      tmp = it1;
    } else {
      break;
    }
  }
  tmp = st.insert(mk(b[x], xx));
  for (auto it : a[x]) {
    if (it == pa) {
      continue;
    }
    dfs(it, x);
  }
  st.erase(st.find(store_it));
  for (auto it : v) {
    st.insert(it);
  }
}
int main() {
  int n;
  while (cin >> n) {
    for (int i = 1; i <= n; ++i) {
      scanf("%d", b + i);
    }
    int x, y;
    for (int i = 0; i < n - 1; ++i) {
      scanf("%d%d", &x, &y);
      a[x].push_back(y);
      a[y].push_back(x);
    }
    st.insert(mk(-1, 0));
    dfs(1, -1);
    for (int i = 1; i <= n; ++i) {
      printf("%d\\n", ans[i]);
    }
  }
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
vector<int> a[200010];
multiset<pair<int, int>> st;
int ans[200010], b[200010];
void dfs(int x, int pa) {
  auto tmp = st.lower_bound(mk(b[x], -1));
  --tmp;
  int xx = tmp->sc + 1;
  tmp = st.insert(mk(b[x], xx));
  vector<pair<int, int>> v;
  auto store_it = tmp;
  ans[x] = (tmp->sc);
  ++tmp;
  while (1) {
    if (tmp == st.end()) {
      break;
    }
    if ((tmp->sc) >= (tmp->sc)) {
      auto it1 = tmp;
      ++it1;
      v.push_back(*tmp);
      st.erase(tmp);
      tmp = it1;
    }
  }
  for (auto it : a[x]) {
    if (it == pa) {
      continue;
    }
    dfs(it, x);
  }
  st.erase(store_it);
  for (auto it : v) {
    st.insert(it);
  }
}
int main() {
  int n;
  while (cin >> n) {
    for (int i = 1; i <= n; ++i) {
      scanf("%d", b + i);
    }
    int x, y;
    for (int i = 0; i < n - 1; ++i) {
      scanf("%d%d", &x, &y);
      a[x].push_back(y);
      a[y].push_back(x);
    }
    st.insert(mk(-1, 0));
    dfs(1, -1);
    for (int i = 1; i <= n; ++i) {
      printf("%d\\n", ans[i]);
    }
  }
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdint.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using ldouble = long double;

#define repi(i, a, b) for (ll i = (a); i < (b); i++)
#define rep(i, n) for (ll i = 0; i < (n); ++i)

int N;
struct node {
  vector<int> link;
  int a;
};
vector<node> nod;
vector<int> res;
void f(vector<int> &dp, int t, int par) {
  auto it = lower_bound(dp.begin(), dp.end(), nod[t].a);
  bool pop;
  int buf;
  int idx;
  if (it == dp.end()) {
    dp.push_back(nod[t].a);
    pop = true;
  } else {
    buf = *it;
    idx = distance(dp.begin(), it);
    *it = nod[t].a;
    pop = false;
  }
  res[t] = dp.size();
  for (int i : nod[t].link) {
    if (i == par)
      continue;
    f(dp, i, t);
  }
  if (pop)
    dp.pop_back();
  else
    dp[idx] = buf;
}
int main() {
  cin >> N;
  nod.resize(N);
  res.resize(N);
  rep(i, N) cin >> nod[i].a;
  rep(i, N - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    nod[u].link.push_back(v);
    nod[v].link.push_back(u);
  }
  vector<int> v;
  f(v, 0, -1);
  rep(i, N) cout << res[i] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdint.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using ldouble = long double;

#define repi(i, a, b) for (ll i = (a); i < (b); i++)
#define rep(i, n) for (ll i = 0; i < (n); ++i)

int N;
struct node {
  vector<int> link;
  int a;
};
vector<node> nod;
vector<int> res;
void f(vector<int> dp, int t, int par) {
  auto it = lower_bound(dp.begin(), dp.end(), nod[t].a);
  if (it == dp.end())
    dp.push_back(nod[t].a);
  else
    *it = nod[t].a;
  res[t] = dp.size();
  for (int i : nod[t].link) {
    if (i == par)
      continue;
    f(dp, i, t);
  }
}
int main() {
  cin >> N;
  nod.resize(N);
  res.resize(N);
  rep(i, N) cin >> nod[i].a;
  rep(i, N - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    nod[u].link.push_back(v);
    nod[v].link.push_back(u);
  }
  f(vector<int>(), 0, -1);
  rep(i, N) cout << res[i] << endl;
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define inf 0x7fffffff
#define infll 0x7fffffffffffffff
#define clr(c) memset(c, 0, sizeof(c))
using namespace std;
int mod = 998244353;
const int M = 1e6 + 10;
const int N = 2e5 + 10;
inline ll read() {
  ll b = 1, sum = 0;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      b = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return b * sum;
}
int T, n, m, t, k;
int ans;
int dp[N], a[N];
vector<int> e[N];
vector<int> s;
void dfs(int x, int _fa, int sz) {
  int i, j;
  s.push_back(inf);
  for (int y : e[x]) {
    if (y == _fa)
      continue;
    int loc = lower_bound(s.begin(), s.begin() + sz, a[y]) - s.begin();
    if (loc == sz) {
      s[loc] = a[y];
      dp[y] = loc + 1;
      dp[y] = max(dp[y], dp[x]);
      dfs(y, x, sz + 1);
    } else {
      int tmp = s[loc];
      s[loc] = a[y];
      dp[y] = loc + 1;
      dp[y] = max(dp[y], dp[x]);
      dfs(y, x, sz);
      s[loc] = tmp;
    }
  }
}
int main() {
  int i, j;
  n = read();
  for (i = 1; i <= n; i++) {
    a[i] = read();
  }
  for (i = 1; i <= n - 1; i++) {
    int x = read(), y = read();
    e[x].push_back(y);
    e[y].push_back(x);
  }
  s.resize(n + 1);
  dp[1] = 1;
  s[0] = a[1];
  dfs(1, 0, 1);

  for (i = 1; i <= n; i++) {
    printf("%d\\n", dp[i]);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define inf 0x7fffffff
#define infll 0x7fffffffffffffff
#define clr(c) memset(c, 0, sizeof(c))
using namespace std;
int mod = 998244353;
const int M = 1e6 + 10;
const int N = 2e5 + 10;
inline ll read() {
  ll b = 1, sum = 0;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      b = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return b * sum;
}
int T, n, m, t, k;
int ans;
int dp[N], a[N];
vector<int> e[N];
void dfs(int x, int _fa, vector<int> s, int sz) {
  int i, j;
  s.push_back(inf);
  for (int y : e[x]) {
    if (y == _fa)
      continue;
    int loc = lower_bound(s.begin(), s.begin() + sz, a[y]) - s.begin();
    if (loc == sz) {
      s.push_back(inf);
      s[loc] = a[y];
      dp[y] = loc + 1;
      dp[y] = max(dp[y], dp[x]);
      dfs(y, x, s, sz + 1);
    } else {
      int tmp = s[loc];
      s[loc] = a[y];
      dp[y] = loc + 1;
      dp[y] = max(dp[y], dp[x]);
      dfs(y, x, s, sz);
      s[loc] = tmp;
    }
  }
}
int main() {
  int i, j;
  n = read();
  for (i = 1; i <= n; i++) {
    a[i] = read();
  }
  for (i = 1; i <= n - 1; i++) {
    int x = read(), y = read();
    e[x].push_back(y);
    e[y].push_back(x);
  }
  vector<int> ss(2, inf);
  dp[1] = 1;
  ss[0] = a[1];
  dfs(1, 0, ss, 1);

  for (i = 1; i <= n; i++) {
    printf("%d\\n", dp[i]);
  }
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
const int MR = 2e5 + 10;
typedef long long LL;

int a[MR], p[MR], ans[MR], plen;
bool vis[MR];
vector<int> d[MR];

void dfs(int idx) {
  int rawplen = plen, rawlow, rawp;
  int *low = lower_bound(p, p + plen, a[idx]);
  if (low == p + plen) {
    p[plen] = a[idx];
    plen++;
  } else {
    rawlow = low - p;
    rawp = p[rawlow];
    p[rawlow] = a[idx];
  }
  ans[idx] = plen;
  vis[idx] = true;
  int len = d[idx].size();
  for (int j = 0; j < len; j++) {
    if (vis[d[idx][j]])
      continue;
    dfs(d[idx][j]);
  }

  if (plen != rawplen)
    plen--;
  else
    p[rawlow] = rawp;
}

int main() {
  int n, u, v;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i < n; i++) {
    cin >> u >> v;
    d[u].push_back(v);
    d[v].push_back(u);
  }
  plen = 0;
  dfs(1);
  for (int i = 1; i <= n; i++) {
    printf("%d\\n", ans[i]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
const int MR = 2e5 + 10;
typedef long long LL;

int a[MR], ans[MR];
bool vis[MR];
vector<int> d[MR];

struct NODE {
  int idx;
  vector<int> p;
};
queue<NODE> q;

vector<int> inserta(vector<int> p, int x) {
  vector<int>::iterator low;
  low = lower_bound(p.begin(), p.end(), x);
  if (low == p.end())
    p.push_back(x);
  else
    p[low - p.begin()] = x;
  return p;
}

int main() {
  int n, u, v;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i < n; i++) {
    cin >> u >> v;
    d[u].push_back(v);
    d[v].push_back(u);
  }
  NODE r;
  r.idx = 1;
  vector<int> p;
  p.push_back(a[1]);
  r.p = p;
  q.push(r);
  while (!q.empty()) {
    r = q.front();
    q.pop();
    int i = r.idx;
    p = r.p;
    ans[i] = p.size();
    vis[i] = true;
    int len = d[i].size();
    for (int j = 0; j < len; j++) {
      if (vis[d[i][j]])
        continue;
      r.idx = d[i][j];
      r.p = inserta(p, a[r.idx]);
      q.push(r);
    }
  }
  for (int i = 1; i <= n; i++) {
    printf("%d\\n", ans[i]);
  }
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>

using namespace std;
using ll = long long;
const int INF = 1e9;
typedef pair<int, int> P;
const int mx = 200000;

vector<int> v[mx];
int an[mx], a[mx];
int n;
int pp[mx];

void dfs(int from, int now, int pc) {
  // cout<<from<<" "<<now<<" "<<pc<<" ";
  auto it = lower_bound(pp, pp + mx, a[now]);
  int pc2 = max(pc, (int)(it - pp));
  // cout<<pc2<<" ";
  int bf = *it;
  // cout<<bf<<endl;
  *it = a[now];
  an[now] = pc2;

  for (int x : v[now]) {
    if (x == from)
      continue;
    dfs(now, x, pc2);
  }

  *it = bf;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < n - 1; i++) {
    int s, b;
    cin >> s >> b;
    s--;
    b--;
    v[s].push_back(b);
    v[b].push_back(s);
  }

  fill(pp, pp + mx, INF);

  dfs(-1, 0, 0);

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

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>

using namespace std;
using ll = long long;
const int INF = 1e9;
typedef pair<int, int> P;
const int mx = 200000;

vector<int> v[mx];
int an[mx], a[mx];
int n;
int pp[mx];

int dfs(int from, int now, int pc) {
  // cout<<from<<" "<<now<<" "<<pc<<" ";
  auto it = lower_bound(pp, pp + mx, a[now]);
  int pc2 = max(pc, (int)(it - pp));
  // cout<<pc2<<" ";
  int bf = *it;
  // cout<<bf<<endl;
  *it = a[now];
  an[now] = pc2;

  for (int x : v[now]) {
    if (x == from)
      continue;
    dfs(now, x, pc2);
  }

  *it = bf;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < n - 1; i++) {
    int s, b;
    cin >> s >> b;
    s--;
    b--;
    v[s].push_back(b);
    v[b].push_back(s);
  }

  fill(pp, pp + mx, INF);

  dfs(-1, 0, 0);

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