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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1000000007;
const int INF = 1000000000; // 1e9
const int NIL = -1;
const long long LINF = 1000000000000000000; // 1e18
const long double EPS = 1E-10;

template <class T, class S> inline bool chmax(T &a, const S &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class S> inline bool chmin(T &a, const S &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

const int inf = 1000110000; // 1e9
void dfs(int v, int prv, std::vector<int> &a, std::vector<int> &ans,
         std::vector<std::vector<int>> &G, std::vector<int> &LIS) {
  auto itr(std::lower_bound(LIS.begin(), LIS.end(), a[v]));
  int aa(*itr);
  *itr = a[v];
  ans[v] =
      std::distance(LIS.begin(), std::lower_bound(LIS.begin(), LIS.end(), inf));
  for (int e : G[v]) {
    if (e == prv)
      continue;
    dfs(e, v, a, ans, G, LIS);
  }
  *itr = aa;
}

int main() {
  int N;
  std::cin >> N;
  std::vector<int> a(N), ans(N, 0), LIS(N, inf);
  for (int i(0); i < N; ++i)
    std::cin >> a[i];
  std::vector<std::vector<int>> G(N);
  {
    int u, v;
    for (int i(0), i_len(N - 1); i < i_len; ++i) {
      std::cin >> u >> v;
      --u;
      --v;
      G[u].push_back(v);
      G[v].push_back(u);
    }
  }
  dfs(0, -1, a, ans, G, LIS);
  for (int i(0); i < N; ++i) {
    std::cout << ans[i] << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1000000007;
const int INF = 1000000000; // 1e9
const int NIL = -1;
const long long LINF = 1000000000000000000; // 1e18
const long double EPS = 1E-10;

template <class T, class S> inline bool chmax(T &a, const S &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class S> inline bool chmin(T &a, const S &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

const int inf = 1000110000; // 1e9
void dfs(int v, int prv, std::vector<int> &a, std::vector<int> &ans,
         std::vector<std::vector<int>> &G, std::vector<int> LIS) {
  *(std::lower_bound(LIS.begin(), LIS.end(), a[v])) = a[v];
  ans[v] =
      std::distance(LIS.begin(), std::lower_bound(LIS.begin(), LIS.end(), inf));
  for (int e : G[v]) {
    if (e == prv)
      continue;
    dfs(e, v, a, ans, G, LIS);
  }
}

int main() {
  int N;
  std::cin >> N;
  std::vector<int> a(N), ans(N, 0), LIS(N, inf);
  for (int i(0); i < N; ++i)
    std::cin >> a[i];
  std::vector<std::vector<int>> G(N);
  {
    int u, v;
    for (int i(0), i_len(N - 1); i < i_len; ++i) {
      std::cin >> u >> v;
      --u;
      --v;
      G[u].push_back(v);
      G[v].push_back(u);
    }
  }
  dfs(0, -1, a, ans, G, LIS);
  for (int i(0); i < N; ++i) {
    std::cout << ans[i] << "\\n";
  }
  return 0;
}