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

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, m, n) for (int i = m; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
#define RREP(i, m, n) for (int i = n; i >= m; --i)
#define llrep(i, n) for (ll i = 0; i < n; ++i)
#define llREP(i, m, n) for (ll i = m; i < n; ++i)
#define llrrep(i, n) for (ll i = n; i >= 0; --i)
#define llRREP(i, m, n) for (ll i = n; i >= m; --i)

using ll = long long;
using ull = unsigned long long;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
using pil = std::pair<int, long long>;
using pli = std::pair<long long, int>;
using plpii = std::pair<long long, std::pair<int, int>>;
using pdd = std::pair<double, double>;

using namespace std;

const long long INF = 1e18;
const long long _MOD = 1000000007;
const long long MOD = 998244353;
const int ALPHABET = 27;
const double pi = 3.14159265358979;

struct node {
  ll a;
  int length;
  vector<int> children;
};

void solve(node *nodes, ll *lis, int current, int pre, int n) {
  ll t;
  auto p = lower_bound(lis, lis + n, nodes[current].a);
  t = *p;
  *p = nodes[current].a;
  nodes[current].length = lower_bound(lis, lis + n, INF - 1) - lis;

  rep(i, nodes[current].children.size()) {
    if (nodes[current].children[i] == pre)
      continue;

    solve(nodes, lis, nodes[current].children[i], current, n);
  }

  *p = t;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  node *nodes = new node[n];
  rep(i, n) cin >> nodes[i].a;

  int u, v;
  rep(i, n - 1) {
    cin >> u >> v;
    nodes[u - 1].children.push_back(v - 1);
    nodes[v - 1].children.push_back(u - 1);
  }

  ll *lis = new ll[n + 1];
  rep(i, n + 1) lis[i] = INF;
  solve(nodes, lis, 0, -1, n);

  rep(i, n) { cout << nodes[i].length << "\\n"; }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, m, n) for (int i = m; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
#define RREP(i, m, n) for (int i = n; i >= m; --i)
#define llrep(i, n) for (ll i = 0; i < n; ++i)
#define llREP(i, m, n) for (ll i = m; i < n; ++i)
#define llrrep(i, n) for (ll i = n; i >= 0; --i)
#define llRREP(i, m, n) for (ll i = n; i >= m; --i)

using ll = long long;
using ull = unsigned long long;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
using pli = std::pair<long long, int>;
using plpii = std::pair<long long, std::pair<int, int>>;
using pdd = std::pair<double, double>;

using namespace std;

const long long _MOD = 1000000007;
const long long MOD = 998244353;
const int ALPHABET = 27;
const double pi = 3.14159265358979;

struct node {
  ll a;
  int length;
  vector<int> children;
  vector<ll> lis;
};

void solve(node *nodes, int current, int pre) {
  if (pre != -1) {
    rep(i, nodes[pre].lis.size()) {
      if (i == 0) {
        nodes[current].lis.push_back(min(nodes[current].a, nodes[pre].lis[i]));
      } else {
        if (nodes[current].a > nodes[pre].lis[i - 1]) {
          nodes[current].lis.push_back(
              min(nodes[current].a, nodes[pre].lis[i]));
        } else {
          nodes[current].lis.push_back(nodes[pre].lis[i]);
        }
      }
    }

    if (nodes[current].a > (*nodes[pre].lis.rbegin())) {
      nodes[current].lis.push_back(nodes[current].a);
    }
  } else {
    nodes[current].lis.push_back(nodes[current].a);
  }
  nodes[current].length = nodes[current].lis.size();

  rep(i, nodes[current].children.size()) {
    if (nodes[current].children[i] == pre)
      continue;

    solve(nodes, nodes[current].children[i], current);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  node *nodes = new node[n];
  rep(i, n) cin >> nodes[i].a;

  int u, v;
  rep(i, n - 1) {
    cin >> u >> v;
    nodes[u - 1].children.push_back(v - 1);
    nodes[v - 1].children.push_back(u - 1);
  }

  solve(nodes, 0, -1);

  rep(i, n) { cout << nodes[i].length << "\\n"; }

  return 0;
}