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

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define Rep(i, a, b) for (ll i = a; i < b; ++i)
#define rep(i, b) Rep(i, 0, b)
#define allof(a) (a).begin(), (a).end()

#define Yes(q) ((q) ? "Yes" : "No")
#define YES(q) ((q) ? "YES" : "NO")
#define Possible(q) ((q) ? "Possible" : "Impossible")
#define POSSIBLE(q) ((q) ? "POSSIBLE" : "IMPOSSIBLE")

using ll = long long;

constexpr int inf = 1e9 + 7;
constexpr ll infll = 1ll << 60ll;
constexpr ll mod = 1e9 + 7;
// 0~3までは右左下上 4~7までは斜め
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, -1, 1};

namespace {
template <typename T> void chmax(T &a, T b) { a = std::max(a, b); }
template <typename T> void chmin(T &a, T b) { a = std::min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

void Cout(long long x, const char *end = "\\n") { std::cout << x << end; }
template <typename T> void Cout(const T &x, const char *end = "\\n") {
  std::cout << x << end;
}
template <typename T>
void Cout(const std::vector<T> &x, const char *sep = " ",
          const char *end = "\\n") {
  for (std::size_t i = 0, sz = x.size(); i < sz; i++) {
    std::cout << x[i] << (i == sz - 1 ? end : sep);
  }
}

// 標準入出力
struct inp {
  std::size_t sz;
  inp(std::size_t _sz = 1) : sz(_sz) {}
  template <typename T> operator T() const {
    T a;
    std::cin >> a;
    return a;
  }
  template <typename T> operator std::vector<T>() const {
    std::vector<T> a(sz);
    for (std::size_t i = 0; i < sz; i++)
      std::cin >> a[i];
    return a;
  }
};

// 2次元用の標準入出力
template <typename T> struct inpn {
  std::size_t szi, szj;
  inpn(std::size_t _szi, std::size_t _szj) : szi(_szi), szj(_szj) {}
  operator std::vector<std::vector<T>>() const {
    std::vector<std::vector<T>> a(szi, std::vector<T>(szj));
    for (std::size_t i = 0; i < szi; ++i)
      for (std::size_t j = 0; j < szj; ++j)
        cin >> a[i][j];
    return a;
  }
};

inp inp1; // input one

struct Edge {
  ll to;   // 移動先のノード
  ll cost; // 辺のコスト
  ll hoge; // 任意変更 いらないかも

  Edge(ll _to, ll _cost, ll _hoge = 0) : to(_to), cost(_cost), hoge(_hoge){};
  bool operator<(const Edge &rhs) const {
    if (cost != rhs.cost)
      return cost > rhs.cost;
    else
      return to < rhs.to;
  }
};

using Graph = vector<vector<Edge>>;

void dfs(int s, vector<int> &dp, vector<int> &ans, vector<bool> &used,
         const Graph &G, const vector<int> &a) {
  used[s] = true;

  int i = lower_bound(allof(dp), a[s]) - dp.begin();
  int prev = dp[i];
  dp[i] = a[s];
  //*lower_bound(allof(dp), a[s]) = a[s];
  int res = lower_bound(allof(dp), inf) - dp.begin();
  chmax(ans[s], res);

  for (auto edge : G[s]) {
    if (used[edge.to])
      continue;

    dfs(edge.to, dp, ans, used, G, a);
  }

  // 戻るときに変更を戻す
  dp[i] = prev;
}
} // namespace

int main() {

  int n = inp1;
  vector<int> a = inp(n);
  Graph G(n);
  rep(i, n - 1) {
    int v, u;
    cin >> u >> v;
    --u, --v;
    G[u].push_back(Edge(v, 1));
    G[v].push_back(Edge(u, 1));
  }

  vector<bool> used(n, false);
  vector<int> dp(n, inf);
  vector<int> ans(n, 0);

  dfs(0, dp, ans, used, G, a);

  for (auto x : ans)
    Cout(x);

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define Rep(i, a, b) for (ll i = a; i < b; ++i)
#define rep(i, b) Rep(i, 0, b)
#define allof(a) (a).begin(), (a).end()

#define Yes(q) ((q) ? "Yes" : "No")
#define YES(q) ((q) ? "YES" : "NO")
#define Possible(q) ((q) ? "Possible" : "Impossible")
#define POSSIBLE(q) ((q) ? "POSSIBLE" : "IMPOSSIBLE")

using ll = long long;

constexpr int inf = 1e9 + 7;
constexpr ll infll = 1ll << 60ll;
constexpr ll mod = 1e9 + 7;
// 0~3までは右左下上 4~7までは斜め
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, -1, 1};

namespace {
template <typename T> void chmax(T &a, T b) { a = std::max(a, b); }
template <typename T> void chmin(T &a, T b) { a = std::min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

void Cout(long long x, const char *end = "\\n") { std::cout << x << end; }
template <typename T> void Cout(const T &x, const char *end = "\\n") {
  std::cout << x << end;
}
template <typename T>
void Cout(const std::vector<T> &x, const char *sep = " ",
          const char *end = "\\n") {
  for (std::size_t i = 0, sz = x.size(); i < sz; i++) {
    std::cout << x[i] << (i == sz - 1 ? end : sep);
  }
}

// 標準入出力
struct inp {
  std::size_t sz;
  inp(std::size_t _sz = 1) : sz(_sz) {}
  template <typename T> operator T() const {
    T a;
    std::cin >> a;
    return a;
  }
  template <typename T> operator std::vector<T>() const {
    std::vector<T> a(sz);
    for (std::size_t i = 0; i < sz; i++)
      std::cin >> a[i];
    return a;
  }
};

// 2次元用の標準入出力
template <typename T> struct inpn {
  std::size_t szi, szj;
  inpn(std::size_t _szi, std::size_t _szj) : szi(_szi), szj(_szj) {}
  operator std::vector<std::vector<T>>() const {
    std::vector<std::vector<T>> a(szi, std::vector<T>(szj));
    for (std::size_t i = 0; i < szi; ++i)
      for (std::size_t j = 0; j < szj; ++j)
        cin >> a[i][j];
    return a;
  }
};

inp inp1; // input one

struct Edge {
  ll to;   // 移動先のノード
  ll cost; // 辺のコスト
  ll hoge; // 任意変更 いらないかも

  Edge(ll _to, ll _cost, ll _hoge = 0) : to(_to), cost(_cost), hoge(_hoge){};
  bool operator<(const Edge &rhs) const {
    if (cost != rhs.cost)
      return cost > rhs.cost;
    else
      return to < rhs.to;
  }
};

using Graph = vector<vector<Edge>>;

void dfs(int s, vector<int> dp, vector<int> &ans, vector<bool> &used,
         const Graph &G, const vector<int> &a) {
  used[s] = true;

  *lower_bound(allof(dp), a[s]) = a[s];
  int res = lower_bound(allof(dp), inf) - dp.begin();
  chmax(ans[s], res);

  for (auto edge : G[s]) {
    if (used[edge.to])
      continue;

    dfs(edge.to, dp, ans, used, G, a);
  }
}
} // namespace

int main() {

  int n = inp1;
  vector<int> a = inp(n);
  Graph G(n);
  rep(i, n - 1) {
    int v, u;
    cin >> u >> v;
    --u, --v;
    G[u].push_back(Edge(v, 1));
    G[v].push_back(Edge(u, 1));
  }

  vector<bool> used(n, false);
  vector<int> dp(n, inf);
  vector<int> ans(n, 0);

  dfs(0, dp, ans, used, G, a);

  for (auto x : ans)
    Cout(x);

  // サイズnの配列をコピーしまくってるからアウトだよね？

  return 0;
}