Problem p02698 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, m, n) for (int(i) = (int)(m); i < (int)(n); i++)
#define rep2(i, m, n) for (int(i) = (int)(n)-1; i >= (int)(m); i--)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
#define FOR(i, c)                                                              \
  for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
using ll = long long;
using ull = unsigned long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = (ll)1e9 + 7;
// constexpr long long MOD = 998244353LL;
typedef vector<ll> Array;
typedef vector<Array> Matrix;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

struct Edge {
  ll to, cap, rev;
  Edge(ll _to, ll _cap, ll _rev) {
    to = _to;
    cap = _cap;
    rev = _rev;
  }
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;

void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) {
  G[from].push_back(Edge(to, cap, (ll)G[to].size()));
  if (revFlag)
    G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

void dfs(Graph &g, ll v, ll p, ll t, Array &dp, Array &ans, Array &a) {
  auto it = lower_bound(dp.begin(), dp.begin() + t, a[v]);
  bool flag = false;
  P change = {-1, -1};
  if (it - dp.begin() == t) {
    change = {t, dp[t]};
    dp[t] = a[v];
    t++;

  } else {

    if (*it > a[v]) {
      change = {it - dp.begin(), *it};
      *it = a[v];
    }
  }

  ans[v] = t;
  Array temp = dp;

  for (auto e : g[v]) {
    if (e.to == p)
      continue;
    dfs(g, e.to, v, t, dp, ans, a);
  }

  if (change.first != -1) {
    dp[change.first] = change.second;
  }
}

void solve() {
  ll n;
  cin >> n;
  Array a(n);
  REP(i, n) cin >> a[i];
  Graph g(n);
  REP(i, n - 1) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    add_edge(g, a, b, 1, true, 1);
  }

  Array ans(n, 0);
  Array dp(n + 1, INF);
  dp[0] = a[0];
  dfs(g, 0, -1, 1, dp, ans, a);
  for (auto i : ans)
    cout << i << en;
}

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

  solve();
  // ll t;cin>>t;REP(i,t) solve();

  return 0;
}


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

#define rep(i, m, n) for (int(i) = (int)(m); i < (int)(n); i++)
#define rep2(i, m, n) for (int(i) = (int)(n)-1; i >= (int)(m); i--)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
#define FOR(i, c)                                                              \
  for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
using ll = long long;
using ull = unsigned long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = (ll)1e9 + 7;
// constexpr long long MOD = 998244353LL;
typedef vector<ll> Array;
typedef vector<Array> Matrix;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

struct Edge {
  ll to, cap, rev;
  Edge(ll _to, ll _cap, ll _rev) {
    to = _to;
    cap = _cap;
    rev = _rev;
  }
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;

void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) {
  G[from].push_back(Edge(to, cap, (ll)G[to].size()));
  if (revFlag)
    G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

void dfs(Graph &g, ll v, ll p, ll t, Array &dp, Array &ans, Array &a) {
  auto it = lower_bound(dp.begin(), dp.begin() + t, a[v]);
  bool flag = false;
  P change = {-1, -1};
  if (it - dp.begin() == t) {
    change = {t, dp[t]};
    dp[t] = a[v];
    t++;

  } else {

    if (*it > a[v]) {
      change = {it - dp.begin(), *it};
      *it = a[v];
    }
  }

  ans[v] = t;

  for (auto e : g[v]) {
    if (e.to == p)
      continue;
    dfs(g, e.to, v, t, dp, ans, a);
  }

  if (change.first != -1) {
    dp[change.first] = change.second;
  }
}

void solve() {
  ll n;
  cin >> n;
  Array a(n);
  REP(i, n) cin >> a[i];
  Graph g(n);
  REP(i, n - 1) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    add_edge(g, a, b, 1, true, 1);
  }

  Array ans(n, 0);
  Array dp(n + 1, INF);
  dp[0] = a[0];
  dfs(g, 0, -1, 1, dp, ans, a);
  for (auto i : ans)
    cout << i << en;
}

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

  solve();
  // ll t;cin>>t;REP(i,t) solve();

  return 0;
}