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

#include <bits/stdc++.h>
using namespace std;

typedef unsigned long ul;
typedef unsigned long long ull;
typedef long long ll;
typedef vector<ll> vint;
typedef vector<vector<ll>> vvint;
typedef vector<vector<vector<ll>>> vvvint;
typedef vector<string> vstring;
typedef vector<vector<string>> vvstring;
typedef vector<char> vchar;
typedef vector<vector<char>> vvchar;
typedef vector<long double> vdouble;
typedef vector<vector<long double>> vvdouble;
typedef vector<vector<vector<long double>>> vvvdouble;
typedef pair<ll, ll> pint;
typedef vector<pint> vpint;
typedef vector<bool> vbool;

#define rep(i, n) for (ll i = 0; i < n; i++)
#define repf(i, f, n) for (ll i = f; i < n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define ALL(obj) (obj).begin(), (obj).end()
// #define LLONG_MAX 9223372036854775806
#define MOD 1000000007
const double PI = 3.14159265358979323846;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> T Ceil(T a, T b) { return (a + b - 1) / b; }

int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};
int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};

void printv(vint &v) {
  for (auto e : v)
    cout << e << " ";
  cout << endl;
}
ll n;
vint a;
vvint edges;

vint ans;

void f(ll now, ll par, vint &L) {

  auto it = lower_bound(ALL(L), a[now]);
  ll tmp = *it;
  *it = a[now];
  auto it2 = lower_bound(ALL(L), LLONG_MAX - 1);
  ans[now] = it2 - L.begin();

  for (auto to : edges[now]) {
    if (to == par)
      continue;
    f(to, now, L);
  }
  *it = tmp;
}

int main() {
  cout << fixed << setprecision(10);

  cin >> n;
  a.resize(n);
  ans.resize(n, -1);
  rep(i, n) cin >> a[i];

  edges.resize(n);
  rep(i, n - 1) {
    ll u, v;
    cin >> u >> v;
    u--;
    v--;
    edges[u].push_back(v);
    edges[v].push_back(u);
  }

  vint L(n, LLONG_MAX);
  f(0, -1, L);

  for (auto e : ans)
    cout << e << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef unsigned long ul;
typedef unsigned long long ull;
typedef long long ll;
typedef vector<ll> vint;
typedef vector<vector<ll>> vvint;
typedef vector<vector<vector<ll>>> vvvint;
typedef vector<string> vstring;
typedef vector<vector<string>> vvstring;
typedef vector<char> vchar;
typedef vector<vector<char>> vvchar;
typedef vector<long double> vdouble;
typedef vector<vector<long double>> vvdouble;
typedef vector<vector<vector<long double>>> vvvdouble;
typedef pair<ll, ll> pint;
typedef vector<pint> vpint;
typedef vector<bool> vbool;

#define rep(i, n) for (ll i = 0; i < n; i++)
#define repf(i, f, n) for (ll i = f; i < n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define ALL(obj) (obj).begin(), (obj).end()
// #define LLONG_MAX 9223372036854775806
#define MOD 1000000007
const double PI = 3.14159265358979323846;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> T Ceil(T a, T b) { return (a + b - 1) / b; }

int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};
int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};

void printv(vint &v) {
  for (auto e : v)
    cout << e << " ";
  cout << endl;
}
ll n;
vint a;
vvint edges;

vint ans;

void f(ll now, ll par, vint &L) {

  auto it = lower_bound(ALL(L), a[now]);
  if (it == L.end()) {
    L.push_back(a[now]);
  } else {
    *it = a[now];
  }
  ans[now] = L.size();
  vint tmp = L;

  for (auto to : edges[now]) {
    if (to == par)
      continue;
    f(to, now, L);
    L = tmp;
  }
}

int main() {
  cout << fixed << setprecision(10);

  cin >> n;
  a.resize(n);
  ans.resize(n, -1);
  rep(i, n) cin >> a[i];

  edges.resize(n);
  rep(i, n - 1) {
    ll u, v;
    cin >> u >> v;
    u--;
    v--;
    edges[u].push_back(v);
    edges[v].push_back(u);
  }

  vint L;
  f(0, -1, L);

  for (auto e : ans)
    cout << e << endl;

  return 0;
}