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

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)

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 = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;

// -------------------------------------------------------
int N;
int a[201010];
vector<int> T[201010];
int dp[201010];
using P = pair<int, int>;
stack<P> S;
int ans[201010];
bool used[201010];

void dfs(int cur) {
  int n = a[cur];
  auto lb = lower_bound(dp, dp + N, n);

  int idx = lb - dp;

  int prev_value = *lb;
  S.push(make_pair(idx, prev_value));
  *lb = n;

  auto lb_inf = lower_bound(dp, dp + N, inf);
  int inf_idx = lb_inf - dp;
  ans[cur] = inf_idx;

  used[cur] = true;

  auto node = T[cur];
  for (int i = 0; i < node.size(); i++) {
    if (used[node[i]]) {
      continue;
    }

    dfs(node[i]);
    P last_op = S.top();
    S.pop();
    dp[last_op.first] = last_op.second;
  }

  used[cur] = false;
}

int main() {
  rep(i, 0, 201000) { dp[i] = inf; }

  cin >> N;
  rep(i, 0, N) { cin >> a[i]; }
  rep(i, 0, N - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    T[u].push_back(v);
    T[v].push_back(u);
  }

  dfs(0);

  rep(i, 0, N) { cout << ans[i] << endl; }
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)

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 = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;

// -------------------------------------------------------
int N;
int a[201010];
vector<int> T[201010];
int dp[201010];
using P = pair<int, int>;
stack<P> S;
int ans[201010];
bool used[201010];

void dfs(int cur) {
  int n = a[cur];
  auto lb = lower_bound(dp, dp + N, n);

  int idx = lb - dp;

  int prev_value = *lb;
  S.push(make_pair(idx, prev_value));
  *lb = n;

  for (int i = 0; i < N; i++) {
    if (dp[i] != inf) {
      ans[cur] = i + 1;
    } else {
      break;
    }
  }

  used[cur] = true;

  auto node = T[cur];
  for (int i = 0; i < node.size(); i++) {
    if (used[node[i]]) {
      continue;
    }

    dfs(node[i]);
    P last_op = S.top();
    S.pop();
    dp[last_op.first] = last_op.second;
  }

  used[cur] = false;
}

int main() {
  rep(i, 0, 201000) { dp[i] = inf; }

  cin >> N;
  rep(i, 0, N) { cin >> a[i]; }
  rep(i, 0, N - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    T[u].push_back(v);
    T[v].push_back(u);
  }

  dfs(0);

  rep(i, 0, N) { cout << ans[i] << endl; }
}