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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, n) for (int i = 0; i < int(n); i++)
#define FOR(i, m, n) for (int i = int(m); i < int(n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define VI vector<int>
#define VP vector<pair<int, int>>
#define VPP vector<pair<int, pair<int, int>>>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define fore(i, a) for (auto &i : a)
typedef pair<int, int> P;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = 1 << 30;
const ll INFL = 1LL << 60;
const ll mod = 1000000007;

int longest_increasing_subsequence(const vector<int> &v) {
  int n = v.size();
  vector<int> dp(n, INF);
  for (int i = 0; i < n; i++)
    *lower_bound(dp.begin(), dp.end(), v[i]) = v[i];
  return find(dp.begin(), dp.end(), INF) - dp.begin();
}

bool b[200005];
VI a;

VI ans(200005, 0);

VI g[200005];

VI dp;

void dfs(int i, int sz) {
  // v.push_back(a[i]);
  int it = lower_bound(ALL(dp), a[i]) - dp.begin();
  int tmp = dp[it];
  dp[it] = a[i];
  sz = max(sz, it + 1);
  ans[i] = sz;
  b[i] = true;
  for (int j : g[i]) {
    if (b[j])
      continue;
    dfs(j, sz);
  }
  dp[it] = tmp;
}

int main() {

  int n;
  cin >> n;
  a.resize(n);
  dp.resize(n);
  REP(i, n) dp[i] = INF;
  REP(i, n) cin >> a[i];

  REP(i, n - 1) {
    int c, d;
    cin >> c >> d;
    c--;
    d--;
    g[c].push_back(d);
    g[d].push_back(c);
  }
  dfs(0, 0);

  REP(i, n) cout << ans[i] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, n) for (int i = 0; i < int(n); i++)
#define FOR(i, m, n) for (int i = int(m); i < int(n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define VI vector<int>
#define VP vector<pair<int, int>>
#define VPP vector<pair<int, pair<int, int>>>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define fore(i, a) for (auto &i : a)
typedef pair<int, int> P;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = 1 << 30;
const ll INFL = 1LL << 60;
const ll mod = 1000000007;

int longest_increasing_subsequence(const vector<int> &v) {
  int n = v.size();
  vector<int> dp(n, INF);
  for (int i = 0; i < n; i++)
    *lower_bound(dp.begin(), dp.end(), v[i]) = v[i];
  return find(dp.begin(), dp.end(), INF) - dp.begin();
}

bool b[200005];
VI a;

VI ans(200005, 0);

VI g[200005];

queue<pair<int, pair<VI, VI>>> q;

int main() {

  int n;
  cin >> n;
  a.resize(n);
  REP(i, n) cin >> a[i];

  REP(i, n - 1) {
    int c, d;
    cin >> c >> d;
    c--;
    d--;
    g[c].push_back(d);
    g[d].push_back(c);
  }
  q.push({0, {{}, {INF}}});

  while (!q.empty()) {
    auto to = q.front();
    q.pop();
    int i = to.first;
    VI v = to.second.first;
    VI dp = to.second.second;
    v.push_back(a[i]);
    int it = lower_bound(ALL(dp), a[i]) - dp.begin();
    dp[it] = a[i];
    if (it == dp.size() - 1)
      dp.push_back(INF);
    ans[i] = dp.size() - 1;
    b[i] = true;
    for (int j : g[i]) {
      if (b[j])
        continue;
      q.push({j, {v, dp}});
    }
  }

  REP(i, n) cout << ans[i] << endl;
}