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

#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
const int N = 2e5 + 5;
ll arr[N], ans[N];
vector<int> adj[N];
void dfs(int node, int par, vector<ll> &lis) {
  bool add = 0;
  ll val = arr[node], idx = -1, old = -1;
  auto it = lower_bound(lis.begin(), lis.end(), val);
  if (it == lis.end()) {
    add = 1;
    lis.push_back(val);
  } else {
    old = *it;
    *it = val;
  }
  idx = it - lis.begin();
  ans[node] = (int)lis.size();
  for (auto child : adj[node])
    if (child != par)
      dfs(child, node, lis);
  if (add)
    lis.pop_back();
  else
    lis[idx] = old;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> arr[i];
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  vector<ll> lis;
  dfs(1, 0, lis);
  for (int i = 1; i <= n; ++i)
    cout << ans[i] << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
const int N = 2e5 + 5;
ll arr[N], ans[N];
vector<int> adj[N];
void dfs(int node, int par, vector<ll> lis) {
  ll val = arr[node];
  auto it = lower_bound(lis.begin(), lis.end(), val);
  if (it == lis.end())
    lis.push_back(val);
  else
    *it = val;
  ans[node] = (int)lis.size();
  for (auto child : adj[node])
    if (child != par)
      dfs(child, node, lis);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> arr[i];
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  vector<ll> lis;
  dfs(1, 0, lis);
  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>
#define endl '\\n'
#define pb push_back
#define fr first
#define sc second
#define ll long long int
#define bit(idx) idx & (-idx)
#define bin(x, a) bitset<a>(x)
#define all(A) A.begin(), A.end()
#define debug(x) cout << #x << " = " << x << endl;
using namespace std;

const int MAXN = 2e5 + 5;

ll mask, BIT[MAXN], ans[MAXN];
vector<ll> G[MAXN], A;
unordered_map<ll, ll> um;
stack<vector<pair<ll, ll>>> changes;

void upd(ll idx, ll val) {
  vector<pair<ll, ll>> curr_changes;
  for (ll _idx = idx + 1; _idx < MAXN; _idx += bit(_idx)) {
    if (val > BIT[_idx]) {
      curr_changes.pb({_idx, BIT[_idx]});
    }
    BIT[_idx] = max(BIT[_idx], val);
  }
  changes.push(curr_changes);
}

ll qry(ll idx) {
  ll ret = 0;
  for (int _idx = idx + 1; _idx > 0; _idx -= bit(_idx)) {
    ret = max(ret, BIT[_idx]);
  }
  return ret;
}

void roll() {
  vector<pair<ll, ll>> curr = changes.top();
  changes.pop();
  for (auto [_, __] : curr) {
    BIT[_] = __;
  }
}

void dfs(int u, int p) {
  int curr_ans = qry(A[u] - 1) + 1;
  upd(A[u], curr_ans);
  ans[u] = qry(MAXN);
  for (auto v : G[u]) {
    if (v != p) {
      dfs(v, u);
    }
  }
  roll();
}

int main() {
  ios_base::sync_with_stdio(NULL);
  cin.tie();
  cout.tie();
  int n;
  cin >> n;
  A.pb(-1);
  int tmp[MAXN] = {0}, sz = 0;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    A.pb(x);
    tmp[sz++] = x;
  }
  sort(tmp, tmp + sz);
  sz = unique(tmp, tmp + sz) - tmp;
  for (int i = 1; i <= n; i++)
    A[i] = lower_bound(tmp, tmp + sz, A[i]) - tmp;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    G[u].pb(v);
    G[v].pb(u);
  }
  dfs(1, 1);
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << endl;
  }
}
/**
10
1 2 5 3 4 6 7 3 2 4
1 2
2 3
3 4
4 5
3 6
6 7
1 8
8 9
9 10

1
2
3
3
4
4
5
2
2
3

*/

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define endl '\\n'
#define pb push_back
#define fr first
#define sc second
#define ll long long int
#define bit(idx) idx & (-idx)
#define bin(x, a) bitset<a>(x)
#define all(A) A.begin(), A.end()
#define debug(x) cout << #x << " = " << x << endl;
using namespace std;

const int MAXN = 2e5 + 5e3;

vector<int> G[MAXN], dp[MAXN], A;

void dfs(int u, int p) {
  for (int i = 0; i < (int)G[u].size(); i++) {
    int v = G[u][i];
    if (v != p) {
      dp[v] = dp[u];
      if (A[v] > dp[v].back())
        dp[v].pb(A[v]);
      else
        *upper_bound(all(dp[v]), A[v]) = A[v];
      dfs(v, u);
    }
  }
}

int main() {
  ios_base::sync_with_stdio(NULL);
  cin.tie();
  cout.tie();
  int n;
  cin >> n;
  A.pb(-1);
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    A.pb(x);
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    G[u].pb(v);
    G[v].pb(u);
  }
  dp[1].pb(A[1]);
  dfs(1, 1);
  for (int i = 1; i <= n; i++) {
    /// cout << i << " = ";	for(auto x : dp[i]) cout << x << " "; cout <<
    /// endl;
    cout << dp[i].size() << endl;
  }
}
/**
10
1 2 5 3 4 6 7 3 2 4
1 2
2 3
3 4
4 5
3 6
6 7
1 8
8 9
9 10

1
2
3
3
4
4
5
2
2
3

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

#include <bits/stdc++.h>
using namespace std;
const int maxm = 2e5 + 5;
vector<int> g[maxm];
int ans[maxm];
int d[maxm], len;
int a[maxm];
void dfs(int x, int fa) {
  int f = 0;
  int pos = 0;
  int temp = 0;
  if (a[x] > d[len]) {
    d[++len] = a[x];
    f = 1;
  } else {
    int l = 1, r = len;
    while (l <= r) { // 二分插入位置
      int mid = (l + r) / 2;
      if (a[x] <= d[mid])
        pos = mid, r = mid - 1;
      else
        l = mid + 1;
    }
    temp = d[pos];
    d[pos] = a[x];
  }
  ans[x] = len;
  for (int v : g[x]) {
    if (v == fa)
      continue;
    dfs(v, x);
  }
  if (f) { // 回溯的时候撤销
    len--;
  } else {
    d[pos] = temp;
  }
}
signed main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs(1, -1);
  for (int i = 1; i <= n; i++) {
    printf("%d\\n", ans[i]);
  }
  return 0;
}

/*

*/


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxm = 1e5 + 5;
vector<int> g[maxm];
int ans[maxm];
int d[maxm], len;
int a[maxm];
void dfs(int x, int fa) {
  int f = 0;
  int pos = 0;
  int temp = 0;
  if (a[x] > d[len]) {
    d[++len] = a[x];
    f = 1;
  } else {
    int l = 1, r = len;
    while (l <= r) {
      int mid = (l + r) / 2;
      if (a[x] <= d[mid])
        pos = mid, r = mid - 1;
      else
        l = mid + 1;
    }
    temp = d[pos];
    d[pos] = a[x];
  }
  ans[x] = len;
  for (int v : g[x]) {
    if (v == fa)
      continue;
    dfs(v, x);
  }
  if (f) { // 回溯的时候撤销
    len--;
  } else {
    d[pos] = temp;
  }
}
signed main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    g[a].push_back(b);
    g[b].push_back(a);
  }
  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 <bits/stdc++.h>
using namespace std;

constexpr int MAX = 200200;
constexpr int INF = (1 << 30);

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int &e : a)
    cin >> e;

  vector<int> g[n];
  for (int i = 0; i < n - 1; i++) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    g[s].emplace_back(t);
    g[t].emplace_back(s);
  }

  vector<int> dp(MAX, INF);
  vector<int> ans(n);
  stack<pair<int, pair<int, int>>> sta;
  sta.emplace(0, make_pair(0, 0));

  while (not sta.empty()) {
    int type = sta.top().first;
    if (type == 0) {
      int v, p;
      tie(v, p) = sta.top().second;
      sta.pop();
      auto it = lower_bound(dp.begin(), dp.end(), a[v]);
      sta.emplace(1, make_pair(it - dp.begin(), *it));
      *it = a[v];
      ans[v] = lower_bound(dp.begin(), dp.end(), INF) - dp.begin();
      for (const int &u : g[v])
        if (u != p) {
          sta.emplace(0, make_pair(u, v));
        }
    } else {
      int id, val;
      tie(id, val) = sta.top().second;
      sta.pop();
      dp[id] = val;
    }
  }

  for (const int &e : ans)
    cout << e << '\\n';

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int &e : a)
    cin >> e;

  vector<int> g[n];
  for (int i = 0; i < n - 1; i++) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    g[s].emplace_back(t);
    g[t].emplace_back(s);
  }

  constexpr int INF = (1 << 30);
  vector<int> ans(n);

  stack<pair<pair<int, int>, vector<int>>> sta;
  sta.emplace(make_pair(0, 0), vector<int>(1, INF));

  while (not sta.empty()) {
    int v, p;
    tie(v, p) = sta.top().first;
    vector<int> dat = sta.top().second;
    sta.pop();
    *lower_bound(dat.begin(), dat.end(), a[v]) = a[v];
    dat.emplace_back(INF);
    ans[v] = lower_bound(dat.begin(), dat.end(), INF) - dat.begin();
    for (const int &u : g[v])
      if (u != p) {
        sta.emplace(make_pair(u, v), dat);
      }
  }

  for (const int &e : ans)
    cout << e << ' ';

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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

ll N, M, Q;
ll A[200005];
ll ans[200005];
vector<ll> E[200005];
ll V[200005];
ll dp[200005];
stack<P> a;
P p;

void dfs(ll s) {
  auto it = lower_bound(dp, dp + N, A[s]);
  ll k = distance(dp, it);

  a.push(P(k, dp[k]));
  dp[k] = A[s];
  auto itt = lower_bound(dp, dp + N, 10000000000000000);
  ll l = distance(dp, itt);
  ans[s] = l;

  V[s] = 1;
  for (int i = 0; i < E[s].size(); i++) {
    if (V[E[s][i]] == 0) {
      dfs(E[s][i]);
    }
  }
  p = a.top();
  dp[p.first] = p.second;
  a.pop();
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    E[u - 1].push_back(v - 1);
    E[v - 1].push_back(u - 1);
  }
  for (int i = 0; i < N + 1; i++) {
    dp[i] = 10000000000000001;
  }
  dfs(0);
  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

ll N, M, Q;
ll A[101];
ll ans[101];
vector<ll> E[101];
int V[101];
ll dp[101];
bool found = false;
stack<P> a;
P p;

void dfs(ll s) {
  auto it = lower_bound(dp, dp + N, A[s]);
  ll k = distance(dp, it);

  a.push(P(k, dp[k]));
  dp[k] = A[s];

  auto itt = lower_bound(dp, dp + N, 1000000000);
  ll l = distance(dp, itt);
  ans[s] = l - 1;

  V[s] = 1;
  for (int i = 0; i < E[s].size(); i++) {
    if (V[E[s][i]] == 0) {
      dfs(E[s][i]);
    }
  }
  p = a.top();
  dp[p.first] = p.second;
  a.pop();
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    E[u - 1].push_back(v - 1);
    E[v - 1].push_back(u - 1);
  }
  for (int i = 0; i < N; i++) {
    dp[i] = 10000000001;
  }
  dp[0] = 0;
  //    while(!s.empty()){
  //        ll now = s.top();
  //        s.pop();
  //        auto it = lower_bound(dp,dp+N, A[now]);
  //        ll k = distance(dp,it);
  //        auto itt = lower_bound(dp,dp+N, 1000000000);
  //        ll l = distance(dp,itt);
  //        ans[now] = l;
  //        a.push(P(k,dp[k]));
  //        dp[k] = A[now];
  //        found = false;
  //        V[now] = 1;
  //        cout << now <<  endl;
  //
  //        for(int i=0;i<E[now].size();i++){
  //            if(V[E[now][i]]==0){
  //                s.push(E[now][i]);
  //                found = true;
  //            }
  //        }
  //        if(!found){
  //            P p = a.top();
  //            dp[p.first] = p.second;
  //            a.pop();
  //        }
  //    }
  dfs(0);
  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

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

#define ll long long
#define ld long double
#define xx first
#define yy second
#define vl vector<ll>
#define pll pair<ll, ll>
#define all(v) v.begin(), v.end()

#define MAX 400005

vector<ll> g[MAX];
vector<ll> a(MAX);

ll kth[MAX];

multiset<ll> haha;

void dfs(ll node, ll par) {
  haha.insert(a[node]);

  auto it = haha.lower_bound(a[node]);

  ll xd = -1;

  it++;

  if (it != haha.end())
    xd = *it, haha.erase(it);

  kth[node] = haha.size();

  for (ll x : g[node]) {
    if (x != par) {
      dfs(x, node);
    }
  }

  if (xd != -1)
    haha.insert(xd);

  auto lol = haha.find(a[node]);
  if (lol != haha.end())
    haha.erase(lol);
}

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

  ll n, u, v;
  cin >> n;

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

  for (int i = 0; i < n - 1; 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 << kth[i] << endl;

  return 0;
}


Error: 0

Fixed submission:

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

#define ll long long
#define ld long double
#define xx first
#define yy second
#define vl vector<ll>
#define pll pair<ll, ll>
#define all(v) v.begin(), v.end()

#define MAX 400005

vector<ll> g[MAX];
vector<ll> a(MAX);

ll kth[MAX];

set<ll> haha;

void dfs(ll node, ll par) {
  haha.insert(a[node]);

  auto it = haha.upper_bound(a[node]);

  ll xd = 0;

  if (it != haha.end())
    xd = *it, haha.erase(it);

  kth[node] = haha.size();

  for (ll x : g[node]) {
    if (x != par) {
      dfs(x, node);
    }
  }

  if (xd)
    haha.insert(xd);
  haha.erase(haha.find(a[node]));
}

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

  ll n, u, v;
  cin >> n;

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

  for (int i = 0; i < n - 1; 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 << kth[i] << endl;

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

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

#define watch(x) cout << (#x) << " is " << (x) << endl
#define fr(i, n) for (int i = 0; i < n; i++)
#define rep(i, st, en) for (int i = st; i <= en; i++)
#define repn(i, st, en) for (int i = st; i >= en; i--)
#define sq(a) (a * a)
typedef long long ll;
#define int ll
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll mod = 1e9 + 7;

vector<vector<int>> g;
vector<int> a;
vector<int> ans;

void dfs(int v, int p, vector<int> &en) {
  auto it = lower_bound(en.begin(), en.end(), a[v]);
  ll pos = it - en.begin();
  ll temp = -1;
  if (it != en.end()) {
    temp = *it;
    *it = a[v];
  } else {
    en.push_back(a[v]);
  }
  ans[v] = en.size();

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

  /// restore
  if (temp == -1) {
    if (en.size())
      en.pop_back();
  } else {
    en[pos] = temp;
  }
}

void solve() {
  int n;
  cin >> n;
  g.resize(n + 1);
  a.resize(n + 1);
  ans.resize(n + 1);
  rep(i, 1, n) {
    cin >> a[i];
    /// coordinate compress to be done
  }

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

  vector<int> el;
  dfs(1, 1, el);

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

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

  ll t = 1;
  //  cin>>t;
  while (t--) {
    solve();
  }

  return 0;
}


Error: 0

Fixed submission:

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

#define watch(x) cout << (#x) << " is " << (x) << endl
#define fr(i, n) for (int i = 0; i < n; i++)
#define rep(i, st, en) for (int i = st; i <= en; i++)
#define repn(i, st, en) for (int i = st; i >= en; i--)
#define sq(a) (a * a)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll mod = 1e9 + 7;

vector<vector<int>> g;
vector<int> a;
vector<int> ans;

void dfs(int v, int p, vector<int> &en) {
  auto it = lower_bound(en.begin(), en.end(), a[v]);
  ll temp = -1;
  if (it != en.end()) {
    temp = *it;
    *it = a[v];
  } else {
    en.push_back(a[v]);
  }
  ans[v] = en.size();

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

  /// restore
  if (temp == -1) {
    if (en.size())
      en.pop_back();
  } else {
    *it = temp;
  }
}

void solve() {
  int n;
  cin >> n;
  g.resize(n + 1);
  a.resize(n + 1);
  ans.resize(n + 1);
  rep(i, 1, n) {
    cin >> a[i];
    /// coordinate compress to be done
  }

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

  vector<int> el;
  dfs(1, 1, el);

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

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

  ll t = 1;
  //  cin>>t;
  while (t--) {
    solve();
  }

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

#include <bits/stdc++.h>

using namespace std;

vector<vector<int>> edges;
vector<int> length;
vector<int> active;
vector<int> a;

void dfs(int u, int parent) {
  auto p = lower_bound(active.begin(), active.end(), a[u]);
  bool back = false;
  int prev = 0;
  if (p == active.end()) {
    active.push_back(a[u]);
    back = true;
  } else {
    prev = *p;
    *p = a[u];
  }
  length[u] = active.size();
  for (int v : edges[u]) {
    if (v != parent) {
      dfs(v, u);
    }
  }
  if (back) {
    active.pop_back();
  } else {
    *p = prev;
  }
}
int main() {
  int n;
  cin >> n;
  a.resize(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  edges.resize(n);
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    edges[u - 1].push_back(v - 1);
    edges[v - 1].push_back(u - 1);
  }
  length.resize(n);
  active.reserve(n);
  dfs(0, -1);
  for (int x : length) {
    cout << x << "\\n";
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

vector<vector<int>> edges;
vector<int> length;
vector<int> active;
vector<int> a;

void dfs(int u, int parent) {
  auto p = lower_bound(active.begin(), active.end(), a[u]);
  bool back = false;
  int prev = 0;
  if (p == active.end()) {
    active.push_back(a[u]);
    back = true;
  } else {
    prev = *p;
    *p = a[u];
  }
  length[u] = active.size();
  for (int v : edges[u]) {
    if (v != parent) {
      dfs(v, u);
    }
  }
  if (back) {
    active.pop_back();
  } else {
    *p = prev;
  }
}
int main() {
  int n;
  cin >> n;
  a.resize(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  edges.resize(n);
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    edges[u - 1].push_back(v - 1);
    edges[v - 1].push_back(u - 1);
  }
  vector<bool> visited(n);
  length.resize(n);
  dfs(0, -1);
  for (int i = 0; i < (int)length.size(); ++i) {
    cout << length[i] << "\\n";
  }
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (auto &i : A)
    cin >> i;
  vector<vector<int>> G(N);
  for (auto i = 1; i < N; ++i) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }

  vector<int> d(N, 1);
  vector<int> dp = {A[0]};
  function<void(int, int)> dfs = [&](auto pre, auto cur) {
    auto i = -1, p = -1;
    if (dp.back() < A[cur]) {
      dp.emplace_back(A[cur]);
    } else {
      i = lower_bound(begin(dp), end(dp), A[cur]) - begin(dp);
      p = dp[i];
      dp[i] = A[cur];
    }
    d[cur] = dp.size();
    for (auto nex : G[cur])
      if (pre != nex)
        dfs(cur, nex);
    if (i < 0)
      dp.pop_back();
    else
      dp[i] = p;
  };

  dfs(0, 0);

  for (auto i : d)
    cout << i << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (auto &i : A)
    cin >> i;
  vector<vector<int>> G(N);
  for (auto i = 1; i < N; ++i) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }

  vector<int> d(N, 1);
  function<void(int, int, vector<int>)> dfs = [&](auto pre, auto cur, auto dp) {
    if (dp.back() < A[cur])
      dp.emplace_back(A[cur]);
    else
      *lower_bound(begin(dp), end(dp), A[cur]) = A[cur];
    d[cur] = dp.size();
    for (auto nex : G[cur])
      if (pre != nex)
        dfs(cur, nex, dp);
  };

  dfs(0, 0, {A[0]});

  for (auto i : d)
    cout << i << endl;
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1e9 + 7, INF = mod * mod * 3; // M_PI
#define rep(i, N) for (ll i = 0; i < (N); i++)
#define rep1(i, N) for (ll i = 1; i < (N); i++)
#define pb push_back

ll N;
ll a[200005];
ll dp[200005];
ll ans[200005];
vector<ll> vec[200005];
void dfs(ll in, ll nm, ll noINF) {
  ll x = upper_bound(dp, dp + N + 1, a[nm]) - dp;
  ll be = dp[x];
  if (dp[x - 1] < a[nm])
    dp[x] = a[nm];
  if (dp[x] != INF)
    noINF = max(noINF, x);
  if (dp[in] != INF)
    noINF = in;
  ans[nm] = noINF;
  for (auto &i : vec[nm]) {
    if (ans[i] == INF)
      dfs(in + 1, i, noINF);
  }
  dp[x] = be;
  return;
}

int main() {

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

  cin >> N;
  rep1(i, N + 1) cin >> a[i];
  rep(i, N - 1) {
    ll u, v;
    cin >> u >> v;
    vec[u].pb(v);
    vec[v].pb(u);
  }
  fill(ans, ans + N + 1, INF);
  fill(dp, dp + N + 1, INF);
  dp[0] = -1;
  dfs(1, 1, 0);
  rep1(i, N + 1) cout << ans[i] << endl;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1e9 + 7, INF = mod * mod * 3; // M_PI
#define rep(i, N) for (ll i = 0; i < (N); i++)
#define rep1(i, N) for (ll i = 1; i < (N); i++)
#define pb push_back

ll N;
ll a[200005];
ll dp[200005];
P b[200005]; // first:ほんとのindex second:aの値
ll ans[200005];
vector<ll> vec[200005];
ll parent[200005];
bool leaf[200005]; // falseなら葉
void tree(ll par, ll n) {
  if (parent[n] != 0)
    return;
  parent[n] = par;
  for (auto &i : vec[n])
    tree(n, i);
  return;
}
ll sz;
void make(ll n, ll i) {
  b[n] = P(i, a[i]);
  dp[n] = INF;
  if (parent[i] == -1) {
    sz = n;
    return;
  }
  make(n + 1, parent[i]);
  return;
}
void solve(ll nm) {
  dp[0] = -1;
  ll noINF = 0;
  rep1(i, nm + 1) {
    ll x = upper_bound(dp, dp + nm + 1, b[i].second) - dp;
    if (dp[x - 1] < b[i].second)
      dp[x] = b[i].second;
    if (dp[x] != INF)
      noINF = max(noINF, x);
    if (dp[i] != INF)
      noINF = i;
    ans[b[i].first] = noINF;
  }
}

int main() {

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

  cin >> N;
  rep1(i, N + 1) cin >> a[i];
  rep(i, N - 1) {
    ll u, v;
    cin >> u >> v;
    vec[u].pb(v);
    vec[v].pb(u);
  }
  tree(-1, 1);
  rep1(i, N + 1) {
    ll n = parent[i];
    if (n != -1)
      leaf[n] = true;
  }
  rep1(i, N + 1) {
    if (!leaf[i]) {
      make(1, i);
      reverse(b + 1, b + sz + 1);
      solve(sz);
    }
  }
  rep1(i, N + 1) printf("%lld\\n", ans[i]);
}