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

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <vector>

int N;
std::vector<int> edge[200005];
std::vector<int> dp(200005, 1001001001);
std::vector<int> ans(200005);
std::vector<int> a(200005);
void dfs(int n, int p = -1) {
  auto itr2 = std::lower_bound(dp.begin(), dp.end(), a[n]);
  int temp = *itr2;
  *itr2 = a[n];
  auto itr = std::lower_bound(dp.begin(), dp.end(), 1001001000);
  ans[n] = itr - dp.begin();
  for (int i = 0; i < edge[n].size(); i++) {
    int dst = edge[n][i];
    if (dst == p)
      continue;
    dfs(dst, n);
  }
  *itr2 = temp;
  return;
}
int main() {

  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &a[i]);
  }
  int u, v;
  for (int i = 0; i < N - 1; i++) {
    scanf("%d", &u);
    scanf("%d", &v);
    u--;
    v--;
    edge[u].push_back(v);
    edge[v].push_back(u);
  }
  dfs(0);
  for (int i = 0; i < N; i++) {
    std::cout << ans[i] << std::endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <vector>

int main() {
  int N;
  scanf("%d", &N);
  std::vector<int> a(N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &a[i]);
  }
  std::vector<int> edge[N + 4];
  int u, v;
  for (int i = 0; i < N - 1; i++) {
    scanf("%d", &u);
    scanf("%d", &v);
    u--;
    v--;
    edge[u].push_back(v);
    edge[v].push_back(u);
  }
  std::queue<int> queue;
  queue.push(0);
  std::vector<bool> visited(N, false);
  visited[0] = true;
  std::vector<int> dp_orig;
  std::queue<std::vector<int>> queue_dp;
  dp_orig.push_back(a[0]);
  queue_dp.push(dp_orig);
  std::vector<int> ans(N);
  ans[0] = 1;

  while (queue.size() > 0) {
    int src = queue.front();
    queue.pop();
    std::vector<int> dp = queue_dp.front();
    queue_dp.pop();
    // printf("==========--\\n");
    // printf("src=%d, dp size = %d\\n", src, dp.size());
    /*for (int j=0; j<dp.size(); j++){
      printf("dp[%d]=%d\\n", j, dp[j]);
      }*/
    for (int i = 0; i < edge[src].size(); i++) {
      int dst = edge[src][i];
      if (visited[dst])
        continue;
      visited[dst] = true;
      auto itr = std::lower_bound(dp.begin(), dp.end(), a[dst]);
      int orig;
      bool found = false;
      if (itr == dp.end()) {
        // printf("a[%d]=%d not found\\n", dst, a[dst]);
        dp.push_back(a[dst]);
        ans[dst] = ans[src] + 1;
        // printf("ans[%d]=%d\\n", dst, ans[dst]);
      } else {
        found = true;
        // printf("a[%d]=%d found\\n", dst, a[dst]);
        orig = *itr;
        *itr = a[dst];
        ans[dst] = ans[src];
        // printf("ans[%d]=%d\\n", dst, ans[dst]);
      }
      queue.push(dst);
      queue_dp.push(dp);
      if (found) {
        *itr = orig;
      } else {
        dp.pop_back();
      }
    }
  }
  for (int i = 0; i < N; i++) {
    printf("%d\\n", ans[i]);
  }
  return 0;
}