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


#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << endl; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int N;
  cin >> N;

  vector<ll> A(N);
  REP(i, N) cin >> A[i];

  vector<vi> graph(N);
  REP(i, N - 1) {
    int u, v;
    cin >> u >> v, u--, v--;

    graph[u].pb(v);
    graph[v].pb(u);
  }

  priority_queue<Pin> q;
  q.emplace(0, 0);

  vector<ll> dist(N, inf), prev(N);
  dist[0] = 0;
  while (!q.empty()) {
    auto p = q.top();
    q.pop();

    ll d = p.fi + 1;
    for (auto e : graph[p.se]) {
      if (dist[e] <= d)
        continue;

      dist[e] = d;
      prev[e] = p.se;
      q.emplace(d, e);
    }
  }

  vi ans(N);

  auto dfs = [&](auto &&f, int from, int cur, vll &dp) -> void {
    // update dp table
    auto it = lower_bound(ALL(dp), A[cur]);

    int op, idx, value;
    if (it == dp.end()) {
      dp.pb(A[cur]);
      op = 0;
    } else {
      value = *it;
      idx = it - dp.begin();
      op = 1;
      *it = A[cur];
    }

    ans[cur] = dp.size();

    // go to next nodes
    for (auto e : graph[cur]) {
      if (prev[e] != cur)
        continue;
      f(f, cur, e, dp);
    }

    if (op == 0) {
      dp.pop_back();
    } else {
      dp[idx] = value;
    }
  };

  vll dp{A[0]};
  dfs(dfs, -1, 0, dp);

  REP(i, N) { print(ans[i]); }
}


Error: TLE

Fixed submission:


#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << endl; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int N;
  cin >> N;

  vector<ll> A(N);
  REP(i, N) cin >> A[i];

  vector<vi> graph(N);
  REP(i, N - 1) {
    int u, v;
    cin >> u >> v, u--, v--;

    graph[u].pb(v);
    graph[v].pb(u);
  }

  priority_queue<Pin> q;
  q.emplace(0, 0);

  vector<ll> dist(N, inf), prev(N);
  dist[0] = 0;
  while (!q.empty()) {
    auto p = q.top();
    q.pop();

    ll d = p.fi + 1;
    for (auto e : graph[p.se]) {
      if (dist[e] <= d)
        continue;

      dist[e] = d;
      prev[e] = p.se;
      q.emplace(d, e);
    }
  }

  vi ans(N);

  auto dfs = [&](auto &&f, int from, int cur, vll &dp) -> void {
    // update dp table
    auto it = lower_bound(ALL(dp), A[cur]);
    if (it == dp.end())
      dp.pb(A[cur]);
    else
      *it = A[cur];

    ans[cur] = dp.size();

    int cnt = 0;
    for (auto e : graph[cur])
      if (prev[e] == cur)
        cnt++;

    vll save;
    if (cnt > 1)
      save = dp;
    // go to next nodes
    for (auto e : graph[cur]) {
      if (prev[e] != cur)
        continue;
      if (cnt > 1)
        dp = save;
      f(f, cur, e, dp);
    }
  };

  vll dp{A[0]};
  dfs(dfs, -1, 0, dp);

  REP(i, N) { print(ans[i]); }
}