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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rep2(i, s, n) for (int i = s; i < n; ++i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define tmax(a, b, c) max(a, max(b, c))
#define tmin(a, b, c) min(a, min(b, c))
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define vp vector<P>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v) {
    is >> t;
  }
  return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (T t : v) {
    os << t << " ";
  }
  os << "\\n";
  return os;
}
void Yes(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YES(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int inf = 1001001001;
const ll linf = 1001001001001001001;

int n;
vi a;
vvi tree;
vi ans;

P dfs(vi &dp, int v = 0, int p = -1, int cnt = 0) {
  int f, s;
  int now = a[v];
  if (cnt == 0 || dp[cnt - 1] < now) {
    f = cnt;
    s = inf;
    dp[cnt] = now;
    cnt++;
  } else {
    auto it = lower_bound(all(dp), now);
    f = it - dp.begin();
    s = *it;
    *it = now;
  }
  ans[v] = cnt;
  for (int i : tree[v]) {
    if (i == p)
      continue;
    auto np = dfs(dp, i, v, cnt);
    dp[np.first] = np.second;
  }
  return make_pair(f, s);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> n;
  a.resize(n);
  cin >> a;
  tree.resize(n);
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    tree[u].pb(v);
    tree[v].pb(u);
  }
  ans.resize(n);
  vi v(n, inf);
  dfs(v);
  rep(i, n) cout << ans[i] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rep2(i, s, n) for (int i = s; i < n; ++i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define tmax(a, b, c) max(a, max(b, c))
#define tmin(a, b, c) min(a, min(b, c))
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define vp vector<P>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v) {
    is >> t;
  }
  return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (T t : v) {
    os << t << " ";
  }
  os << "\\n";
  return os;
}
void Yes(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YES(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int inf = 1001001001;
const ll linf = 1001001001001001001;

int n;
vi a;
vvi tree;
vi ans;

void dfs(int v = 0, int p = -1, vi dp = vector<int>(n, inf), int cnt = 0) {
  int now = a[v];
  if (cnt == 0 || dp[cnt - 1] < now) {
    dp[cnt] = now;
    cnt++;
  } else {
    auto it = lower_bound(all(dp), now);
    *it = now;
  }
  ans[v] = cnt;
  for (int i : tree[v]) {
    if (i == p)
      continue;
    dfs(i, v, dp, cnt);
  }
}

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