alexjercan/bugnet huggingface dataset:
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

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

ll a[MAX], dp[MAX], ans[MAX];
vector<vector<int>> g(MAX);

void lis_tree(int u, int p) {
  int prep = lower_bound(dp, dp + MAX, a[u]) - dp;
  ll prev = dp[prep];
  dp[prep] = a[u];
  ans[u] = lower_bound(dp, dp + MAX, INF) - dp;
  for (auto e : g[u]) {
    if (e != p)
      lis_tree(e, u);
  }
  dp[prep] = prev;
}

int main() {
  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);
  }
  rep(i, MAX) dp[i] = INF;
  lis_tree(0, -1);
  rep(i, n) cout << ans[i] << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int ans[MAX], dp[MAX], child[MAX], depth[MAX];
ll a[MAX];
vector<vector<int>> g(MAX);

int dfs(int u, int p) {
  int res = 1;
  for (auto e : g[u]) {
    if (e != p) {
      res += dfs(e, u);
      depth[e] = depth[u] + 1;
    }
  }
  return child[u] = res;
}

void lis_tree(int u, int p) {
  int prep = lower_bound(dp, dp + MAX, a[u]) - dp;
  ll prev = dp[prep];
  dp[prep] = a[u];
  ans[u] = find(dp, dp + MAX, INF) - dp;
  for (auto e : g[u]) {
    if (e != p)
      lis_tree(e, u);
  }
  dp[prep] = prev;
}

int main() {
  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);
  }
  rep(i, MAX) dp[i] = INF;
  depth[0] = 1;
  dfs(0, -1);
  lis_tree(0, -1);
  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 SZ(v) ((int)(v).size())
using ll = long long;

const int MAXN = 2e5 + 1;

int ans[MAXN];
vector<int> G[MAXN];
vector<int> best;
bool change[MAXN];
int val[MAXN];
int sol(0);

void dfs(int u, int p) {
  if (SZ(best) == 0 or best.back() < val[u]) {
    best.push_back(val[u]);
    ans[u] = SZ(best);
    for (auto v : G[u])
      if (v != p)
        dfs(v, u);
    best.pop_back();
  } else {
    int pos = lower_bound(best.begin(), best.end(), val[u]) - best.begin();
    int old = best[pos];
    ans[u] = SZ(best);
    best[pos] = val[u];
    for (auto v : G[u])
      if (v != p)
        dfs(v, u);
    best[pos] = old;
  }
}

int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int N;
  cin >> N;
  for (int i(0); i < N; ++i)
    cin >> val[i];
  for (int i(1); i < N; ++i) {
    int _u, _v;
    cin >> _u >> _v;
    --_u, --_v;
    G[_u].push_back(_v);
    G[_v].push_back(_u);
  }

  dfs(0, 0);
  for (int i(0); i < N; ++i)
    cout << ans[i] << '\\n';
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define SZ(v) ((int)(v).size())
using ll = long long;

const int MAXN = 2e5 + 1;

int ans[MAXN];
vector<int> G[MAXN];
vector<int> best[MAXN];
bool change[MAXN];
int val[MAXN];
int sol(0);

void dfs(int u, int p) {
  int lo(0), up(MAXN - 1);
  while (lo < up) {
    int mid = (lo + up + 1) / 2;
    if (SZ(best[mid]) and best[mid].back() < val[u])
      lo = mid;
    else
      up = mid - 1;
  }
  if (best[lo].back() < val[u])
    ++lo;
  if (lo == sol + 1)
    ++sol;
  ans[u] = sol;
  best[lo].push_back(val[u]);
  for (auto v : G[u])
    if (v != p)
      dfs(v, u);
  if (ans[u] == sol and SZ(best[ans[u]]) == 1)
    sol--;
  best[ans[u]].pop_back();
}

int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int N;
  cin >> N;
  for (int i(0); i < N; ++i)
    cin >> val[i];
  for (int i(1); i < N; ++i) {
    int _u, _v;
    cin >> _u >> _v;
    --_u, --_v;
    G[_u].push_back(_v);
    G[_v].push_back(_u);
  }

  ans[0] = 0;
  best[0].push_back(val[0]);
  for (auto v : G[0])
    dfs(v, 0);
  for (int i(0); i < N; ++i)
    cout << ans[i] + 1 << '\\n';
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 2e5 + 10;

int n, w[N], v[N], len, ans[N];
struct edge {
  int y, next;
} a[N << 1];
int Len, last[N];
void ins(int x, int y) {
  a[++Len] = (edge){y, last[x]};
  last[x] = Len;
}

void dfs(int x, int f) {
  int t = len + 1, val = 0;
  bool flag = 0;
  if (w[len] < v[x])
    w[++len] = v[x], flag = 1;
  else
    t = lower_bound(w + 1, w + len + 1, v[x]) - w, val = w[t], w[t] = v[x];
  ans[x] = len;
  for (int k = last[x], y; k; k = a[k].next)
    if ((y = a[k].y) ^ f)
      dfs(y, x);
  w[t] = val;
  len -= flag;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &v[i]);
  for (int i = 1, x, y; i < n; i++)
    scanf("%d%d", &x, &y), ins(x, y), ins(y, x);
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    printf("%d\\n", ans[i]);
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1e5 + 10;

int n, w[N], v[N], len, ans[N];
struct edge {
  int y, next;
} a[N << 1];
int Len, last[N];
void ins(int x, int y) {
  a[++Len] = (edge){y, last[x]};
  last[x] = Len;
}

void dfs(int x, int f) {
  int t = len + 1, val = 0;
  bool flag = 0;
  if (w[len] < v[x])
    w[++len] = v[x], flag = 1;
  else
    t = upper_bound(w + 1, w + len + 1, v[x]) - w, val = w[t], w[t] = v[x];
  ans[x] = len;
  for (int k = last[x], y; k; k = a[k].next)
    if ((y = a[k].y) ^ f)
      dfs(y, x);
  w[t] = val;
  len -= flag;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &v[i]);
  for (int i = 1, x, y; i < n; i++)
    scanf("%d%d", &x, &y), ins(x, y), ins(y, x);
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    printf("%d\\n", ans[i]);
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<ll> ans;
vector<vector<int>> tree;
vector<ll> a;
ll N;
const int INF = 1001001001;
const int MAXN = 2 * (1e5) + 5;
ll dp[MAXN];

void dfs(int t, int p) {
  int i = lower_bound(dp, dp + MAXN, a[t]) - dp;
  int old = dp[i];
  dp[i] = a[t];
  ans[t] = i;
  if (p >= 0) {
    ans[t] = max(ans[t], ans[p]);
  }

  // cerr << "ans=" <<ans[t] << endl;

  for (int child : tree[t]) {
    if (child == p) {
      continue;
    }
    dfs(child, t);
  }
  dp[i] = old;
}

int main() {
  scanf("%lld", &N);
  ans.resize(N);
  a.resize(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &a[i]);
  }
  tree.resize(N);
  cerr << "a" << endl;
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    tree[u].push_back(v);
    tree[v].push_back(u);
  }
  for (ll i = 0; i < MAXN; i++) {
    dp[i] = INF;
  }
  cerr << "do dfs" << endl;
  dp[0] = -INF;
  dfs(0, -1);
  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}


Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<ll> ans;
vector<vector<int>> tree;
vector<int> a;
ll N;
const ll INF = (ll)(1e7) + 5;
const ll MAXN = 2 * (1e6) + 5;
ll dp[MAXN];

void dfs(int t, int p) {
  int i = lower_bound(dp, dp + MAXN, a[t]) - dp;
  int old = dp[i];
  dp[i] = a[t];
  ans[t] = i;
  if (p >= 0) {
    ans[t] = max(ans[t], ans[p]);
  }

  // cerr << "ans=" <<ans[t] << endl;

  for (int child : tree[t]) {
    if (child == p) {
      continue;
    }
    dfs(child, t);
  }
  dp[i] = old;
}

int main() {
  scanf("%lld", &N);
  ans.resize(N);
  a.resize(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &a[i]);
  }
  tree.resize(N);
  // cerr <<"a"  << endl;
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    tree[u].push_back(v);
    tree[v].push_back(u);
  }
  for (ll i = 0; i < MAXN; i++) {
    dp[i] = INF;
  }
  // cerr <<"do dfs" << endl;
  dp[0] = -INF;
  dfs(0, -1);
  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
#define repl(i, l, r) for (ll i = l; i < r; i++)
#define rep(i, n) repl(i, 0, n)
using ll = long long;
vector<int> lis(200005, 1e9), G[200005];
int ans[200005], a[200005];
void DFS(const int &v, stack<pair<int, int>> &s, int &cnt) {
  int k = lower_bound(lis.begin(), lis.end(), a[v]) - lis.begin();
  cnt = max(cnt, k + 1);
  s.push({k, lis[k]});
  lis[k] = a[v];
  ans[v] = cnt;
  for (auto nv : G[v]) {
    if (ans[nv] == 0)
      DFS(nv, s, cnt);
  }
  int p = s.top().first, q = s.top().second;
  if (p == cnt - 1 and q == 1e9)
    cnt--;
  s.pop();
  lis[p] = q;
  return;
}
int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) scanf("%d", &a[i]);
  rep(i, n - 1) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--, v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  stack<pair<int, int>> s;
  int cnt = 0;
  DFS(0, s, cnt);
  rep(i, n) printf("%d\\n", ans[i]);
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
#define repl(i, l, r) for (ll i = l; i < r; i++)
#define rep(i, n) repl(i, 0, n)
using ll = long long;
vector<int> lis(200005, 1e9), G[200005];
int ans[200005], a[200005];
void DFS(const int &v, stack<pair<int, int>> &s, int &cnt) {
  int k = lower_bound(lis.begin(), lis.end(), a[v]) - lis.begin();
  cnt = max(cnt, k);
  s.push({k, lis[k]});
  lis[k] = a[v];
  ans[v] = cnt;
  for (auto nv : G[v]) {
    if (ans[nv] == 0)
      DFS(nv, s, cnt);
  }
  int p = s.top().first, q = s.top().second;
  if (p == cnt and q == 1e9)
    cnt--;
  s.pop();
  lis[p] = q;
  return;
}
int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) scanf("%d", &a[i]);
  rep(i, n - 1) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--, v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  stack<pair<int, int>> s;
  int cnt = 0;
  DFS(0, s, cnt);
  rep(i, n) printf("%d\\n", ans[i]);
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n;
vector<int> a;
vector<vector<int>> e;
vector<int> dp;
vector<int> ans;

void dfs(int pre, int crt, int size) {
  auto ud = lower_bound(dp.begin(), dp.end(), a[crt]);
  int tmp = *ud;
  *ud = a[crt];
  if (tmp == INT_MAX)
    size++;
  ans[crt] = size;
  for (int i : e[crt])
    if (i != pre)
      dfs(crt, i, size);
  *ud = tmp;
}

int main() {
  cin >> n;
  a.resize(n);
  rep(i, n) cin >> a[i];
  e.resize(n);
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    e[u - 1].push_back(v - 1);
    e[v - 1].push_back(u - 1);
  }
  ans.resize(n);
  dp.resize(n);
  fill(dp.begin(), dp.end(), INT_MAX);
  dfs(-1, 0, 0);
  rep(i, n) printf("%d\\n", ans[i]);
  return 0;
}

Error: TLE

Fixed submission:

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

int n;
vector<int> a;
vector<vector<int>> e;
vector<int> ans;

void dfs(int pre, int crt, vector<int> dp) {
  if (dp.back() < a[crt])
    dp.push_back(a[crt]);
  else
    *lower_bound(dp.begin(), dp.end(), a[crt]) = a[crt];
  ans[crt] = dp.size();
  for (int i : e[crt])
    if (i != pre)
      dfs(crt, i, dp);
}

int main() {
  cin >> n;
  a.resize(n);
  rep(i, n) cin >> a[i];
  e.resize(n);
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    e[u - 1].push_back(v - 1);
    e[v - 1].push_back(u - 1);
  }
  ans.resize(n);
  vector<int> dp(1, INT_MAX);
  dfs(-1, 0, dp);
  rep(i, n) printf("%d\\n", ans[i]);
  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 < (int)(n); ++i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); ++i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define fi first
#define se second
#define INF 1000000009
#define LLINF 1000000000000000009LL
#define MAX_N 200009
using ll = long long;

vector<int> a;
vector<int> to[MAX_N];
int dp[MAX_N];
int ans[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 (auto u : to[v]) {
    if (u == p)
      continue;
    dfs(u, v);
  }
  dp[i] = old;
}

int main() {
  int n;
  cin >> n;
  a = vector<int>(n);
  rep(i, n) cin >> a[i];
  rep(i, n - 1) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    to[s].push_back(t);
    to[t].push_back(s);
  }
  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 < (int)(n); ++i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); ++i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define fi first
#define se second
#define INF 1000000009
#define LLINF 1000000000000000009LL
#define MAX_N 100009
using ll = long long;

vector<int> a;
vector<int> to[MAX_N];
int dp[MAX_N];
int ans[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 (auto u : to[v]) {
    if (u == p)
      continue;
    dfs(u, v);
  }
  dp[i] = old;
}

int main() {
  int n;
  cin >> n;
  a = vector<int>(n);
  rep(i, n) cin >> a[i];
  rep(i, n - 1) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    to[s].push_back(t);
    to[t].push_back(s);
  }
  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: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int nax = 2e5 + 5;
int arr[nax];
int n;
vector<int> edges[nax];
int lis[nax];
vector<int> last;
stack<pair<int, int>> st;

int getIndex(vector<int> &last, int val) {
  int index = 0;
  int l = 0, r = n;
  while (l <= r) {
    int mid = l + (r - l) / 2;
    if (last[mid] < val) {
      index = mid;
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  return index;
}

void dfs(int v, int parent, int ans) {
  int index = getIndex(last, arr[v]);
  int newans = ans;
  pair<int, int> temp = make_pair(index + 1, last[index + 1]);
  if (last[index + 1] > arr[v]) {
    last[index + 1] = arr[v];
    newans = max(ans, index + 1);
  }
  lis[v] = newans;
  st.push(temp);

  for (int nei : edges[v]) {
    if (nei != parent) {
      dfs(nei, v, newans);
    }
  }
  pair<int, int> p = st.top();
  st.pop();
  last[p.first] = p.second;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  last.resize(nax, INT_MAX);
  last[0] = INT_MIN;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }

  dfs(1, -1, 0);

  for (int i = 1; i <= n; i++) {
    cout << lis[i] << " ";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int nax = 2e5 + 5;
int arr[nax];
int n;
vector<int> edges[nax];
int lis[nax];

int getIndex(vector<int> &last, int val) {
  int index = 0;
  int l = 0, r = (int)last.size() - 1;
  while (l <= r) {
    int mid = l + (r - l) / 2;
    if (last[mid] < val) {
      index = mid;
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  return index;
}

void dfs(int v, int parent, vector<int> last, int ans) {
  /*cout << "In dfs " << v << ", " << parent << endl;
  for(int i = 1; i < (int)last.size(); i++) {
      cout << last[i] << " ";
  }
  cout << endl;*/
  int index = getIndex(last, arr[v]);
  // cout << "index, arr[v] = " << index << ", " << arr[v] << endl;
  if (index == (int)last.size() - 1) {
    last.push_back(INT_MAX);
  }
  int newans = ans;
  if (last[index + 1] > arr[v]) {
    last[index + 1] = arr[v];
    newans = max(ans, index + 1);
  }
  lis[v] = newans;
  for (int nei : edges[v]) {
    if (nei != parent) {
      dfs(nei, v, last, newans);
    }
  }
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }

  for (int v : edges[1]) {
    vector<int> last = {INT_MIN, arr[1]};
    int ans = 1;
    dfs(v, 1, last, ans);
  }
  lis[1] = 1;
  for (int i = 1; i <= n; i++) {
    cout << lis[i] << " ";
  }
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 1000000000
#define pb push_back
vector<ll> ans(2000001, 0);
vector<int> dp(2000001, INF);
vector<int> val(2000001);
vector<ll> adj[200001];

void dfs(ll u, ll p) {
  // cout<<"U::"<<u<<"P::"<<p<<endl;
  ll k = lower_bound(dp.begin(), dp.end(), val[u]) - dp.begin();
  ll temp = dp[k];
  dp[k] = val[u];
  ans[u] = max(k, ans[p]);
  for (int j = 0; j < adj[u].size(); j++) {
    if (adj[u][j] == p)
      continue;
    dfs(adj[u][j], u);
  }
  dp[k] = temp;
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> val[i];
  for (int i = 1; i < n; i++) {
    ll u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  dp[0] = -INF;
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 1000000000
#define pb push_back
vector<ll> ans(1000001, 0);
vector<int> dp(1000001, INF);
vector<int> val(1000001);
vector<ll> adj[100001];

void dfs(ll u, ll p) {
  // cout<<"U::"<<u<<"P::"<<p<<endl;
  ll k = lower_bound(dp.begin(), dp.end(), val[u]) - dp.begin();
  ll temp = dp[k];
  dp[k] = val[u];
  ans[u] = max(k, ans[p]);
  for (int j = 0; j < adj[u].size(); j++) {
    if (adj[u][j] == p)
      continue;
    dfs(adj[u][j], u);
  }
  dp[k] = temp;
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> val[i];
  for (int i = 1; i < n; i++) {
    ll u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  dp[0] = -INF;
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define for1(i, n) for (int i = 1; i <= int(n); ++i)
#define ms(a, x) memset(a, x, sizeof(a))
#define F first
#define S second
#define all(x) (x).begin(), (x).end()

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());
template <typename... Args> void rd(Args &...args) { ((cin >> args), ...); }
template <typename... Args> void write(Args... args) {
  ((cout << args << " "), ...);
  cout << endl;
}

vector<int> a, ans;
const int N = 2e5 + 5;
vector<int> G[N];
int stk[N];
void dfs(int u, int fa, vector<int> &lis) {
  int flag;
  int pos = lower_bound(all(lis), a[u]) - lis.begin();
  if (pos == lis.size())
    lis.push_back(a[u]), flag = -1;
  else
    flag = lis[pos], lis[pos] = a[u];
  ans[u] = lis.size();
  for (auto it : G[u]) {
    if (it == fa)
      continue;
    dfs(it, u, lis);
  }
  if (flag == -1)
    lis.pop_back();
  else
    lis[pos] = flag;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  a = ans = vector<int>(n + 1);
  for1(i, n) cin >> a[i];
  forn(i, n - 1) {
    int x, y;
    cin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  vector<int> v{};
  dfs(1, -1, v);
  for1(i, n) cout << ans[i] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define for1(i, n) for (int i = 1; i <= int(n); ++i)
#define ms(a, x) memset(a, x, sizeof(a))
#define F first
#define S second
#define all(x) (x).begin(), (x).end()

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());
template <typename... Args> void rd(Args &...args) { ((cin >> args), ...); }
template <typename... Args> void write(Args... args) {
  ((cout << args << " "), ...);
  cout << endl;
}

vector<int> a, ans;
const int N = 2e5 + 5;
vector<int> G[N];
int stk[N];
void dfs(int u, int fa, vector<int> &lis, int mx) {
  auto tmp = lis;
  int pos = lower_bound(all(lis), a[u]) - lis.begin();
  if (pos == lis.size())
    tmp.push_back(a[u]);
  else
    tmp[pos] = a[u];
  ans[u] = max(mx, pos + 1);
  for (auto it : G[u]) {
    if (it == fa)
      continue;
    dfs(it, u, tmp, ans[u]);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  a = ans = vector<int>(n + 1);
  for1(i, n) cin >> a[i];
  forn(i, n - 1) {
    int x, y;
    cin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  vector<int> v{};
  dfs(1, -1, v, 0);
  for1(i, n) cout << ans[i] << endl;
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
const int INF = 100010001;
const ll LINF = (ll)INF * INF * 10;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) { cin >> a[i]; }
  vvi G(n);
  rep(i, n - 1) {
    int j, k;
    cin >> j >> k;
    --j;
    --k;
    G[j].push_back(k);
    G[k].push_back(j);
  }

  vi lis(n, INT_MAX);
  lis[0] = a[0];
  vi ans(n);
  function<void(int, int)> dfs = [&](int i, int j) {
    ans[i] = distance(lis.begin(), lower_bound(all(lis), INT_MAX));
    for (int ne : G[i]) {
      if (ne == j)
        continue;
      vi::iterator itr = lower_bound(all(lis), a[ne]);
      int num = *itr;
      *itr = a[ne];
      dfs(ne, i);
      *itr = num;
    }
  };

  dfs(0, -1);

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

Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
const int INF = 100010001;
const ll LINF = (ll)INF * INF * 10;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) { cin >> a[i]; }
  vvi G(n);
  rep(i, n - 1) {
    int j, k;
    cin >> j >> k;
    --j;
    --k;
    G[j].push_back(k);
    G[k].push_back(j);
  }

  vi siz(n);
  function<void(int, int)> ddf = [&](int i, int j) {
    for (int ne : G[i]) {
      if (ne == j)
        continue;
      ddf(ne, i);
      siz[i] = max(siz[i], siz[ne] + 1);
    }
  };
  ddf(0, -1);

  vi ans(n);
  vvi x(1, vi(1, a[0]));

  function<void(int, int, int)> dfs = [&](int i, int j, int poi) {
    ans[i] = x[poi].size();
    int num = G[i].size() - 1;
    if (num > 1 or !i and num) {
      rep(k, num) x.push_back(x[poi]);
      num = x.size() - num;
    }
    vector<P> cou;
    for (int ne : G[i]) {
      if (ne == j)
        continue;
      cou.emplace_back(siz[ne], ne);
    }
    sort(rall(cou));
    rep(j, cou.size()) {
      int ne = cou[j].second;
      auto itr = lower_bound(all(x[poi]), a[ne]);
      if (itr == x[poi].end())
        x[poi].push_back(a[ne]);
      else
        *itr = a[ne];
      dfs(ne, i, poi);
      if (poi < num) {
        poi = num;
      } else
        poi++;
    }
  };

  dfs(0, -1, 0);
  rep(i, n) { cout << ans[i] << '\\n'; }
}