Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ch = char;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<ch> vc;
typedef vector<vc> vvc;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
#define ROF(i, a, b) for (ll i = a; i >= b; i--)
#define per(i, a) ROF(i, a, 0)
#define pb push_back
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ld PI = acos(-1);
const ll INF = 1e18;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct edge {
  ll to, cost;
};
struct edge2 {
  ll from, to, cost;
};

const ll MXV = 2e5;
vl G[MXV];
vl seen(MXV, 0);
vl ans(MXV, 0);
vl a(MXV);

// 木の上でLISを頑張る
void dfs(ll s, vl &v) {
  seen[s] = 1;
  ans[s] = v.size();
  ll sz = G[s].size();
  rep(i, sz) {
    ll nv = G[s][i];
    if (seen[nv] == 0) {
      if (a[nv] > v[v.size() - 1]) {
        v.pb(a[nv]);
        dfs(nv, v);
        v.pop_back();
      } else {
        ll over = v.size() - 1, nover = -1;
        while (abs(over - nover) > 1) {
          ll mid = (over + nover) / 2;
          if (v[mid] >= a[nv]) {
            over = mid;
          } else {
            nover = mid;
          }
        }
        ll k = v[over];
        v[over] = a[nv];
        dfs(nv, v);
        v[over] = k;
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n;
  cin >> n;
  a.resize(n);
  rep(i, n) { cin >> a[i]; }
  rep(i, n - 1) {
    ll u, v;
    cin >> u >> v;
    u--, v--;
    G[u].pb(v);
    G[v].pb(u);
  }
  vl vec = {a[0]};
  dfs(0, vec);
  rep(i, n) { cout << ans[i] << '\\n'; }
}

Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ch = char;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<ch> vc;
typedef vector<vc> vvc;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
#define ROF(i, a, b) for (ll i = a; i >= b; i--)
#define per(i, a) ROF(i, a, 0)
#define pb push_back
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ld PI = acos(-1);
const ll INF = 1e18;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct edge {
  ll to, cost;
};
struct edge2 {
  ll from, to, cost;
};

const ll MXV = 2e5;
vl G[MXV];
vl seen(MXV, 0);
vl ans(MXV, 0);
vl a(MXV);

// 木の上でLISを頑張る
void dfs(ll s, vl v) {
  seen[s] = 1;
  ans[s] = v.size();
  ll sz = G[s].size();
  rep(i, sz) {
    ll nv = G[s][i];
    if (seen[nv] == 0) {
      vl vv = v;
      if (a[nv] > v[v.size() - 1]) {
        vv.pb(a[nv]);
      } else {
        ll over = vv.size() - 1, nover = -1;
        while (abs(over - nover) > 1) {
          ll mid = (over + nover) / 2;
          if (vv[mid] > a[nv]) {
            over = mid;
          } else {
            nover = mid;
          }
        }
        vv[over] = a[nv];
      }
      dfs(nv, vv);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n;
  cin >> n;
  a.resize(n);
  rep(i, n) { cin >> a[i]; }
  rep(i, n - 1) {
    ll u, v;
    cin >> u >> v;
    u--, v--;
    G[u].pb(v);
    G[v].pb(u);
  }
  dfs(0, {a[0]});
  rep(i, n) { cout << ans[i] << '\\n'; }
}