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 all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a)
    a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b)
    a = b;
}
void print() { cout << "\\n"; }
template <class T, class... Args> void print(const T &x, const Args &...args) {
  cout << x << " ";
  print(args...);
}
void debug_out() { cout << "\\n"; }
template <class T, class... Args>
void debug_out(const T &x, const Args &...args) {
  cout << x << " ";
  debug_out(args...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

struct info {
  int k, value;
};

int n;
vector<int> A, dp, ans;
vector<vector<int>> G;
const int INF = 1001001001;

void dfs(int i, int par) {
  int k = lower_bound(all(dp), A[i]) - dp.begin();
  info c;
  if (k < n) {
    c = {k, dp[k]};
    dp[k] = A[i];
  } else
    c = {-1, -1};
  ans[i] = lower_bound(all(dp), INF) - dp.begin();
  for (auto to : G[i]) {
    if (to != par)
      dfs(to, i);
  }
  if (c.k != -1) {
    dp[k] = c.value;
  }
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  cin >> n;
  A = vector<int>(n);
  rep(i, n) cin >> A[i];
  G = vector<vector<int>>(n);
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }
  ans = vector<int>(n);
  ans[0] = 1;
  dp = vector<int>(n + 5, INF);
  dp[0] = A[0];
  for (auto to : G[0]) {
    dfs(to, 0);
  }
  rep(i, n) { cout << ans[i] << "\\n"; }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a)
    a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b)
    a = b;
}
void print() { cout << "\\n"; }
template <class T, class... Args> void print(const T &x, const Args &...args) {
  cout << x << " ";
  print(args...);
}
void debug_out() { cout << "\\n"; }
template <class T, class... Args>
void debug_out(const T &x, const Args &...args) {
  cout << x << " ";
  debug_out(args...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

int n;
vector<int> A, ans;
vector<vector<int>> G;
const int INF = 1001001001;

void dfs(int i, int par, vector<int> dp) {
  int k = lower_bound(all(dp), A[i]) - dp.begin();
  if (k < n)
    dp[k] = A[i];
  ans[i] = lower_bound(all(dp), INF) - dp.begin();
  for (auto to : G[i]) {
    if (to != par)
      dfs(to, i, dp);
  }
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  cin >> n;
  A = vector<int>(n);
  rep(i, n) cin >> A[i];
  G = vector<vector<int>>(n);
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }
  vector<int> dp = vector<int>(n + 5, INF);
  dp[0] = A[0];
  ans = vector<int>(n);
  ans[0] = 1;
  for (auto to : G[0]) {
    dfs(to, 0, dp);
  }

  rep(i, n) { cout << ans[i] << "\\n"; }
  return 0;
}