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

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

#define fast                                                                   \\
  ios_base::sync_with_stdio;                                                   \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define ll long long
#define ull unsigned long long
#define pb push_back
#define pii pair<int, int>
#define pll pair<long, long>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pii>
#define Mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define ff first
#define ss second
#define sz(x) (int)x.size()
// #define endl        '\\n'
#define mod 1000000007
// #define mod        998244353
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rem(i, a, b) for (int i = a; i > b; i--)
#define mp(a, b) make_pair(a, b)
#define INF numeric_limits<ll>::max();
#define NINF numeric_limits<ll>::min();
#define vvi(a, b, name) vector<vector<int>> name(a, vector<int>(b, -1))
const long double pi = 3.14159265359;

vector<vector<int>> v;
vector<int> value, ans;
vi ar;

void dfs(int i, int j) {
  int temp, ind = -1;
  auto it = lower_bound(ar.begin(), ar.end(), value[i]);
  if (it == ar.end()) {
    ar.push_back(value[i]);

  } else {
    ind = it - ar.begin();
    temp = *it;
    *it = value[i];
  }

  ans[i] = ar.size();
  for (auto u : v[i]) {
    if (u == j) {
      continue;
    }
    dfs(u, i);
  }
  if (ind == -1) {
    ar.pop_back();
  } else {
    ar[ind] = temp;
  }
}

int main() {
  fast;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#endif
  int t = 1, n, a, b;
  // cin>>t;
  while (t--) {
    cin >> n;
    v.resize(n + 1);
    value.resize(n + 1);
    rep(i, 0, n) { cin >> value[i + 1]; }
    rep(i, 0, n - 1) {
      cin >> a >> b;
      v[a].pb(b);
      v[b].pb(a);
    }
    ans.resize(n + 1);
    ans[1] = 1;
    dfs(1, 0);
    rep(i, 1, n + 1) { cout << ans[i] << endl; }
  }
  return 0;
}


Error: TLE

Fixed submission:

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

#define fast                                                                   \\
  ios_base::sync_with_stdio;                                                   \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define ll long long
#define ull unsigned long long
#define pb push_back
#define pii pair<int, int>
#define pll pair<long, long>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pii>
#define Mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define ff first
#define ss second
#define sz(x) (int)x.size()
// #define endl        '\\n'
#define mod 1000000007
// #define mod        998244353
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rem(i, a, b) for (int i = a; i > b; i--)
#define mp(a, b) make_pair(a, b)
#define INF numeric_limits<ll>::max();
#define NINF numeric_limits<ll>::min();
#define vvi(a, b, name) vector<vector<int>> name(a, vector<int>(b, -1))
const long double pi = 3.14159265359;

vector<vector<int>> v;
vector<int> d, value, path, ans;

/*void dfs(int i,int j){
    for(auto u:v[i]){
      if(u==j){
        continue;
      }
      //path.pb(value[u]);


        auto it = lower_bound(ar.begin(), ar.end(), value[u]);
        if (it == ar.end()) ar.push_back(value[u]);
        else *it = value[u];

      ans[u]=ar.size();
      dfs(u,i);
      //path.erase(--path.end());
      auto ite=find(all(ar),value[u]);
      if(ite!=ar.end()){
        ar.erase(ite);
      }
    }
}
*/

void dfs(vi ar, int i, int j) {
  auto it = lower_bound(ar.begin(), ar.end(), value[i]);
  if (it == ar.end())
    ar.push_back(value[i]);
  else
    *it = value[i];

  ans[i] = ar.size();
  for (auto u : v[i]) {
    if (u == j) {
      continue;
    }
    // path.pb(value[u]);

    dfs(ar, u, i);
  }
}

int main() {
  fast;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#endif
  int t = 1, n, a, b;
  // cin>>t;
  while (t--) {
    cin >> n;
    v.resize(n + 1);
    d.resize(n + 1);
    value.resize(n + 1);
    rep(i, 0, n) { cin >> value[i + 1]; }
    rep(i, 0, n - 1) {
      cin >> a >> b;
      v[a].pb(b);
      v[b].pb(a);
    }
    // queue<pair<int,int>> q;
    ans.resize(n + 1);
    ans[1] = 1;
    // q.push({1,0});
    vi ar;
    /*while(!q.empty()){
      int i=q.front().ff,j=q.front().ss;
      q.pop();

    }*/
    dfs(ar, 1, 0);
    rep(i, 1, n + 1) { cout << ans[i] << endl; }
  }
  return 0;
}