Problem p02698 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i = k; i < (ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define DEBUG_VLL(vec)                                                         \
  REP(sz, vec.size())                                                          \
      std::cerr << vec[sz] << (sz == vec.size() - 1 ? '\n' : ' ');

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

int n;
vll a;
V<V<int>> edges;
V<int> ans;
vll dp;

void dfs(int i, int p) {
  int idx = lower_bound(ALL(dp), a[i]) - dp.begin();
  ll tmp = dp[idx];
  dp[idx] = a[i];
  ans[i] = lower_bound(ALL(dp), HIGHINF) - dp.begin();

  for (int e : edges[i]) {
    if (e == p)
      continue;
    dfs(e, i);
  }
  dp[idx] = tmp;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  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;
    u--, v--;
    edges[u].emplace_back(v);
    edges[v].emplace_back(u);
  }

  ans.resize(n);
  ans[0] = 1;
  for (int e : edges[0]) {
    dp.clear();
    dp.resize(n, HIGHINF);
    dp[0] = a[0];
    dfs(e, 0);
  }

  for (int i = 0; i < n; i++)
    cout << ans[i] << '\n';
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i = k; i < (ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define DEBUG_VLL(vec)                                                         \
  REP(sz, vec.size())                                                          \
      std::cerr << vec[sz] << (sz == vec.size() - 1 ? '\n' : ' ');

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

int n;
vll a;
V<V<int>> edges;
V<int> ans;
vll dp;

void dfs(int i, int p) {
  int idx = lower_bound(ALL(dp), a[i]) - dp.begin();
  ll tmp = dp[idx];
  dp[idx] = a[i];
  ans[i] = lower_bound(ALL(dp), HIGHINF) - dp.begin();

  for (int e : edges[i]) {
    if (e == p)
      continue;
    dfs(e, i);
  }
  dp[idx] = tmp;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  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;
    u--, v--;
    edges[u].emplace_back(v);
    edges[v].emplace_back(u);
  }

  ans.resize(n);
  dp.resize(n, HIGHINF);
  dfs(0, -1);

  for (int i = 0; i < n; i++)
    cout << ans[i] << '\n';
  return 0;
}