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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
template <class T>
using reversed_priority_queue = priority_queue<T, vector<T>, greater<T>>;

const int INF = 1e9;

struct Node {
  int len;
  vector<int> arr;

  Node() : len(0), arr(vector<int>(200010, INF)) {}
};

int N;
vector<int> a;
vector<int> G[200010];
vector<int> ans;
bool used[200010];

void dfs(Node &node, int v) {
  if (used[v])
    return;
  used[v] = true;
  auto p = lower_bound(node.arr.begin(), node.arr.end(), a[v]);
  auto tt = *(p);
  auto lt = node.len;
  *(p) = a[v];
  for (int i = node.len; i < node.arr.size(); i++) {
    if (node.arr[i] == INF) {
      node.len = i;
      break;
    }
  }
  ans[v] = node.len;
  for (int i = 0; i < G[v].size(); i++) {
    dfs(node, G[v][i]);
  }
  node.len = lt;
  *(p) = tt;
  node.arr[node.len] = INF;
  node.arr[node.len + 1] = INF;
  node.arr[node.len + 2] = INF;
}

int main() {
  cin >> N;
  a.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    G[v].push_back(u);
    G[u].push_back(v);
  }

  Node fn;
  ans.resize(N);
  dfs(fn, 0);

  for (auto x : ans) {
    cout << x << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
template <class T>
using reversed_priority_queue = priority_queue<T, vector<T>, greater<T>>;

const int INF = 1e9;

struct Node {
  int len;
  vector<int> arr;

  Node() : len(0), arr(vector<int>(200010, INF)) {}
};

int N;
vector<int> a;
vector<int> G[200010];
vector<int> ans;
bool used[200010];

template <class T> int LIS(vector<T> &a) {

  vector<T> res(a.size() + 1, INF);
  for (int i = 0; i < a.size(); i++) {
    *(lower_bound(res.begin(), res.end(), a[i])) = a[i];
  }
  for (int i = 0; i < a.size(); i++) {
    if (res[i] == INF)
      return i;
  }
  return (int)a.size();
}

void dfs(Node node, int v) {
  if (used[v])
    return;
  used[v] = true;
  *(lower_bound(node.arr.begin(), node.arr.end(), a[v])) = a[v];
  for (int i = node.len; i < node.arr.size(); i++) {
    if (node.arr[i] == INF) {
      node.len = i;
      break;
    }
  }
  ans[v] = node.len;
  for (int i = 0; i < G[v].size(); i++) {
    dfs(node, G[v][i]);
  }
}

int main() {
  cin >> N;
  a.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    G[v].push_back(u);
    G[u].push_back(v);
  }

  Node fn;
  ans.resize(N);
  dfs(fn, 0);

  for (auto x : ans) {
    cout << x << endl;
  }

  return 0;
}