alexjercan/bugnet huggingface dataset:
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;

VI a;
VI len;
VVI e;
VI lfs;
VI lfs_bef;
void dfs(int from, int to) {
  int pos = -1;
  int bef = 0;
  if (lfs.back() < a.at(to)) {
    lfs.push_back(a.at(to));
  } else {
    pos = distance(lfs.begin(), lower_bound(lfs.begin(), lfs.end(), a.at(to)));
    bef = lfs.at(pos);
    lfs.at(pos) = a.at(to);
  }
  len.at(to) = lfs.size();
  for (auto next : e.at(to)) {
    if (next == from) {
      continue;
    }
    dfs(to, next);
  }
  if (pos == -1) {
    lfs.pop_back();
  } else {
    lfs.at(pos) = bef;
  }
}
int main() {
  int n;
  cin >> n;
  a.resize(n);
  len.resize(n);
  e.resize(n);
  rep(i, n) { cin >> a.at(i); }
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    e.at(u).push_back(v);
    e.at(v).push_back(u);
  }
  lfs = {a.at(0)};
  dfs(-1, 0);
  rep(i, n) { cout << len.at(i) << endl; }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;

VI a;
VI len;
VVI e;
VI lfs;
VI lfs_bef;
void dfs(int from, int to) {
  VI lfs_bef(lfs.size());
  copy(lfs.begin(), lfs.end(), lfs_bef.begin());
  if (lfs.back() < a.at(to)) {
    lfs.push_back(a.at(to));
  } else {
    int pos =
        distance(lfs.begin(), lower_bound(lfs.begin(), lfs.end(), a.at(to)));
    lfs.at(pos) = a.at(to);
  }
  len.at(to) = lfs.size();
  for (auto next : e.at(to)) {
    if (next == from) {
      continue;
    }
    dfs(to, next);
  }
  swap(lfs_bef, lfs);
}
int main() {
  int n;
  cin >> n;
  a.resize(n);
  len.resize(n);
  e.resize(n);
  rep(i, n) { cin >> a.at(i); }
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    e.at(u).push_back(v);
    e.at(v).push_back(u);
  }
  lfs = {a.at(0)};
  dfs(-1, 0);
  rep(i, n) { cout << len.at(i) << endl; }
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vll = vector<long long>;
using vp = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvll = vector<vector<long long>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = 1001001001;
vvi to;
vi ans;
vi a;

void dfs(int v, vi &dp, int p = -1) {
  for (int u : to[v]) {
    if (u == p)
      continue;
    int idx = lower_bound(dp.begin(), dp.end(), a[u]) - dp.begin();
    int pre = dp[idx];
    dp[idx] = a[u];
    ans[u] = lower_bound(dp.begin(), dp.end(), INF) - dp.begin();
    dfs(u, dp, v);
    dp[idx] = pre;
  }
}

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

  int n;
  cin >> n;
  a.resize(n);
  rep(i, n) cin >> a[i];
  to.resize(n);
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    to[u].pb(v);
    to[v].pb(u);
  }
  ans.resize(n);
  ans[0] = 1;

  vi dp(n, INF);
  dp[0] = a[0];
  dfs(0, dp);
  rep(i, n) cout << ans[i] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vll = vector<long long>;
using vp = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvll = vector<vector<long long>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = 1001001001;
vvi to;
vi ans;
vi a;

void dfs(int v, vi dp, int p = -1) {
  for (int u : to[v]) {
    if (u == p)
      continue;
    int idx = lower_bound(dp.begin(), dp.end(), a[u]) - dp.begin();
    int pre = dp[idx];
    dp[idx] = a[u];
    ans[u] = lower_bound(dp.begin(), dp.end(), INF) - dp.begin();
    dfs(u, dp, v);
    dp[idx] = pre;
  }
}

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

  int n;
  cin >> n;
  a.resize(n);
  rep(i, n) cin >> a[i];
  to.resize(n);
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    to[u].pb(v);
    to[v].pb(u);
  }
  ans.resize(n);
  ans[0] = 1;

  vi dp(n, INF);
  dp[0] = a[0];
  dfs(0, dp);
  rep(i, n) cout << ans[i] << endl;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error


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

typedef long long ll;

const int N = 200005;
int n;
int A[N];
vector<int> edges[N];
vector<int> V(N, INT_MAX);
int res[N];

void dfs(int par, int u) {
  for (auto v : edges[u]) {
    if (v == par)
      continue;
    int pos = lower_bound(V.begin(), V.end(), A[v]) - V.begin();

    res[v] = max(res[u], pos + 1);

    int temp = V[pos];
    V[pos] = A[v];
    dfs(u, v);
    V[pos] = temp;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> A[i];
  }
  int u, v;
  for (int i = 0; i < n - 1; ++i) {
    cin >> u >> v;
    edges[u].push_back(v);
    edges[v].push_back(u);
  }
  res[1] = 1;
  V[0] = A[1];
  dfs(0, 1);

  for (int i = 1; i <= n; ++i) {
    cout << res[i] << endl;
  }

  return 0;
}


Error: 0

Fixed submission:


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

typedef long long ll;

const int N = 200005;
int n;
int A[N];
vector<int> edges[N];
vector<int> V;
int res[N];

void dfs(int par, int u) {
  for (auto v : edges[u]) {
    if (v == par)
      continue;
    auto it = lower_bound(V.begin(), V.end(), A[v]);
    int dist = it - V.begin() + 1;
    res[v] = max(res[u], dist);

    if (it == V.end()) {
      V.push_back(A[v]);
      dfs(u, v);
      V.pop_back();
    } else {
      int temp = *it;
      *it = A[v];
      dfs(u, v);
      *it = temp;
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> A[i];
  }
  int u, v;
  for (int i = 0; i < n - 1; ++i) {
    cin >> u >> v;
    edges[u].push_back(v);
    edges[v].push_back(u);
  }
  res[1] = 1;
  V.push_back(A[1]);
  dfs(0, 1);

  for (int i = 1; i <= n; ++i) {
    cout << res[i] << endl;
  }

  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error


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

typedef long long ll;

const int N = 200005;
int n;
int A[N];
vector<int> edges[N];
int V[N];
int res[N];

void dfs(int par, int u, int s) {
  for (auto v : edges[u]) {
    if (v == par)
      continue;
    int *p = lower_bound(V + 1, V + 1 + s, A[v]);
    int dist = p - V;
    int ns = s; // next size
    if (p == V + 1 + s)
      ++ns;

    int orig = *p;
    *p = A[v];
    res[v] = max(res[u], dist);
    dfs(u, v, ns);
    *p = orig;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> A[i];
  }
  int u, v;
  for (int i = 0; i < n - 1; ++i) {
    cin >> u >> v;
    edges[u].push_back(v);
    edges[v].push_back(u);
  }
  res[1] = 1;
  V[1] = A[1];
  dfs(0, 1, 1);

  for (int i = 1; i <= n; ++i) {
    cout << res[i] << endl;
  }

  return 0;
}


Error: 0

Fixed submission:


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

typedef long long ll;

const int N = 200005;
int n;
int A[N];
vector<int> edges[N];
vector<int> vec;
unsigned int res[N];

void dfs(int par, int u) {
  for (auto v : edges[u]) {
    if (v == par)
      continue;
    auto it = lower_bound(vec.begin(), vec.end(), A[v]);

    if (it == vec.end()) {
      vec.push_back(A[v]);
      res[v] = max(res[u], unsigned(vec.size()));
      dfs(u, v);
      vec.pop_back();
    } else {
      int orig = *it;
      *it = A[v];
      res[v] = max(res[u], unsigned(it - vec.begin()) + 1);
      dfs(u, v);
      *it = orig;
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> A[i];
  }
  int u, v;
  for (int i = 0; i < n - 1; ++i) {
    cin >> u >> v;
    edges[u].push_back(v);
    edges[v].push_back(u);
  }
  res[1] = 1;
  vec.push_back(A[1]);
  dfs(0, 1);

  for (int i = 1; i <= n; ++i) {
    cout << res[i] << endl;
  }

  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define pp push_back
#define pf push_front
#define mp make_pair
#define fs first
#define sc second
#define sf scanf

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;

const int N = (int)2e5 + 7;
const int INF = (int)1e9 + 7;

int a[N];
int ans[N];
vector<int> g[N];
multiset<int> ms;

void dfs(int s, int p) {
  ms.insert(a[s]);
  multiset<int>::iterator it = ms.lower_bound(a[s]);
  ++it;
  int rem = -1;
  if (it != ms.end()) {
    rem = *it;
    ms.erase(it);
  }
  ans[s] = ms.size();

  for (int i = 0; i < g[s].size(); ++i) {
    if (g[s][i] != p)
      dfs(g[s][i], s);
  }

  ms.erase(a[s]);
  if (rem != -1) {
    ms.insert(rem);
  }
}

int main(int argc, char *argv[]) {
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);

  int n;
  sf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    sf("%d", &a[i]);
  }

  for (int i = 0; i < n - 1; ++i) {
    int x, y;
    sf("%d %d", &x, &y);
    g[x].pp(y);
    g[y].pp(x);
  }

  dfs(1, 0);

  for (int i = 1; i <= n; ++i) {
    printf("%d\\n", ans[i]);
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define pp push_back
#define pf push_front
#define mp make_pair
#define fs first
#define sc second
#define sf scanf

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;

const int N = (int)2e5;
const int INF = (int)1e9 + 7;

int mx;
int n;
int a[N + 7];
int bit[N + 7];
bool vis[N];
vector<int> g[N + 7];

void update(int i, int val) {
  while (i <= mx) {
    bit[i] = max(bit[i], val);
    i += (i & -i);
  }
}

void reset(int i, int val) {
  while (i <= mx) {
    bit[i] = val;
    i += (i & -i);
  }
}

int get(int i) {
  int ans = 0;
  while (i > 0) {
    ans = max(ans, bit[i]);
    i -= (i & -i);
  }
  return ans;
}

int ans[N];
void dfs(int s) {
  vis[s] = true;

  int val = get(a[s] - 1) + 1;
  update(a[s], val);
  ans[s] = val;
  int res = val;

  for (int i = 0; i < g[s].size(); ++i) {
    if (!vis[g[s][i]]) {
      dfs(g[s][i]);
    }
  }

  reset(s, res - 1);
}

int main(int argc, char *argv[]) {
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);

  sf("%d", &n);
  ii p[n + 7];
  for (int i = 0; i < n; ++i) {
    int x;
    sf("%d", &x);
    p[i] = mp(x, i + 1);
  }

  sort(p, p + n);
  int x = 1;
  a[p[0].sc] = x;
  mx = 0;
  for (int i = 1; i < n; ++i) {
    if (p[i].fs != p[i - 1].fs) {
      ++x;
    }
    a[p[i].sc] = x;
    mx = max(mx, x);
  }

  for (int i = 1; i < n; ++i) {
    int x, y;
    sf("%d %d", &x, &y);
    g[x].pp(y);
    g[y].pp(x);
  }

  dfs(1);

  for (int i = 1; i <= n; ++i) {
    printf("%d\\n", ans[i]);
  }

  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

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

const int M = 2e5 + 100;
vector<int> e[M];
int a[M], ans[M];
set<int> st;

void dfs(int u, int pre) {
  auto it = st.lower_bound(a[u]);
  int era = -1;
  if (it != st.end()) {
    era = *it;
    st.erase(it);
  }
  st.insert(a[u]);
  ans[u] = st.size();
  for (auto v : e[u]) {
    if (v != pre) {
      dfs(v, u);
    }
  }
  st.erase(a[u]);
  if (era != -1)
    st.insert(era);
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    e[u].push_back(v);
    e[v].push_back(u);
  }
  dfs(0, -1);
  for (int i = 0; i < n; i++)
    cout << ans[i] << "\\n";
}

Error: 0

Fixed submission:

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

const int M = 2e5 + 100;
vector<int> e[M];
int a[M], ans[M];
set<int> st;

void dfs(int u, int pre) {
  int era = -1;
  auto it = st.lower_bound(a[u]);
  if (it != st.end()) {
    era = *it;
    st.erase(it);
  }
  st.insert(a[u]);
  ans[u] = st.size();
  for (auto v : e[u]) {
    if (v != pre) {
      dfs(v, u);
    }
  }
  if (era != -1)
    st.insert(era);
  st.erase(st.find(a[u]));
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    e[u].push_back(v);
    e[v].push_back(u);
  }
  dfs(0, -1);
  for (int i = 0; i < n; i++)
    cout << ans[i] << "\\n";
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <iostream>
#include <list>
#include <vector>
using namespace std;
typedef long long li;
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); i++)
#define df 0
template <class T> void print(const T &t) { cout << t << "\\n"; }
template <class T, class... Ts> void print(const T &t, const Ts &...ts) {
  cout << t;
  if (sizeof...(ts))
    cout << " ";
  print(ts...);
}

void dfs(vector<vector<int>> &adj, vector<int> &ans, const vector<li> &a,
         int parent = -1, int v = 0) {
  static vector<li> lis(a.size(), INT_MAX);

  auto itr = lower_bound(lis.begin(), lis.end(), a[v]);
  int flag = (itr != lis.end());
  li memo;
  if (flag) {
    memo = *itr;
    *itr = a[v];
  } else
    lis.push_back(a[v]);

  ans[v] = lower_bound(lis.begin(), lis.end(), INT_MAX) - lis.begin();
  for (int x : adj[v]) {
    if (x == parent)
      continue;
    dfs(adj, ans, a, v, x);
  }

  if (flag)
    *itr = memo;
  else
    lis.pop_back();
}

int main() {
  int n;
  cin >> n;
  vector<li> a(n);
  rep(i, n) cin >> a[i];
  vector<vector<int>> adj(n);
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  vector<int> ans(n, -1);
  dfs(adj, ans, a);

  rep(i, n) print(ans[i]);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
#include <vector>
using namespace std;
typedef long long li;
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); i++)
#define df 0
template <class T> void print(const T &t) { cout << t << "\\n"; }
template <class T, class... Ts> void print(const T &t, const Ts &...ts) {
  cout << t;
  if (sizeof...(ts))
    cout << " ";
  print(ts...);
}

void dfs(vector<vector<int>> &adj, vector<int> &ans, const vector<li> &a,
         int parent = -1, int v = 0) {
  static vector<li> lis;

  auto itr = lower_bound(lis.begin(), lis.end(), a[v]);
  int flag = (itr != lis.end());
  li memo;
  if (flag) {
    memo = *itr;
    *itr = a[v];
  } else
    lis.push_back(a[v]);

  ans[v] = lis.size();
  for (int x : adj[v]) {
    if (x == parent)
      continue;
    dfs(adj, ans, a, v, x);
  }

  if (flag)
    *itr = memo;
  else
    lis.pop_back();
}

int main() {
  int n;
  cin >> n;
  vector<li> a(n);
  rep(i, n) cin >> a[i];
  vector<vector<int>> adj(n);
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  vector<int> ans(n, -1);
  dfs(adj, ans, a);

  rep(i, n) print(ans[i]);
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<vector<ll>> Graph;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1e16;

int n;
Graph G;
ll ans[200010];
vector<ll> a;
ll dp[200010];

void dfs(int v, int p) {
  ll now = a[v];
  int itr = lower_bound(dp, dp + 200010, now) - dp;
  ll old = dp[itr];
  dp[itr] = now;
  ans[v] = lower_bound(dp, dp + 200010, INF) - dp - 1;
  for (ll x : G[v]) {
    if (x == p)
      continue;
    dfs(x, v);
  }
  dp[itr] = old;
}

int main() {
  cin >> n;
  a.resize(n);
  rep(i, n) cin >> a[i];
  G.resize(n);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  rep(i, 200010) { dp[i] = INF; }
  dp[0] = -INF;
  dfs(0, -1);
  rep(i, n) { cout << ans[i] << endl; }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<vector<ll>> Graph;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1e16;

int n;
Graph G;
set<ll> s;
ll ans[200010];
vector<ll> a;
bool seen[200010];

void dfs(int v, int p, set<ll> s) {
  seen[v] = true;
  ll now = a[v];
  auto itr = s.lower_bound(now);
  if (itr == s.end()) {
    s.emplace(now);
  } else {
    s.erase(itr);
    s.emplace(now);
  }
  ans[v] = s.size();
  for (ll x : G[v]) {
    if (seen[x])
      continue;
    dfs(x, v, s);
  }
}

int main() {
  cin >> n;
  a.resize(n);
  rep(i, n) cin >> a[i];
  G.resize(n);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  dfs(0, -1, s);
  rep(i, n) { cout << ans[i] << endl; }
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
#define ll long long int
int n;
vector<int> a;
vector<int> adj[200007];

vector<int> vv, ans;
multiset<int> s;
void dfs(int u, int p) {
  s.insert(a[u]);
  int del = -1;
  auto it = s.lower_bound(a[u]);
  if (++it != s.end())
    del = *it, s.erase(it);
  ans[u] = s.size();
  for (auto v : adj[u]) {
    if (v != p) {
      dfs(v, u);
    }
  }
  if (del != -1)
    s.insert(del);
  s.erase(s.find(a[u]));
}

int main() {
  cin >> n;
  a.resize(n + 1);
  ans.resize(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define ll long long int
int n;
vector<int> a;
vector<int> adj[200007];

vector<int> vv, ans;
int lis(vector<int> &aa) {
  int n = aa.size();
  multiset<int> m;
  multiset<int>::iterator it;
  for (int i = 0; i < n; i++) {
    m.insert(aa[i]);
    it = m.lower_bound(aa[i]);
    it++;
    if (it != m.end()) {
      m.erase(it);
    }
  }
  return m.size();
}
void dfs(int u, int p) {
  vv.push_back(a[u]);
  ans[u] = lis(vv);
  for (auto v : adj[u]) {
    if (v != p) {
      dfs(v, u);
    }
  }
  vv.pop_back();
}

int main() {
  cin >> n;
  a.resize(n + 1);
  ans.resize(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << endl;
  }
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, k) for (int i = (int)(j); i <= (int)(k); i++)
#define debug(x) cerr << #x << ":" << x << endl
#define fix cout << fixed << setprecision(20);
const int maxn = (int)1e6 + 5;
const int inf = (int)1e9 + 5;
int ans, n, dp[maxn], a[maxn], Ans[maxn];
vector<int> E[maxn];
void dfs(int x, int fa) {
  // debug(x);
  for (auto v : E[x])
    if (v != fa) {
      int p = lower_bound(dp, dp + 1 + ans, a[v]) - dp;
      int tmp = dp[p], tmpans = ans;
      dp[p] = min(dp[p], a[v]);
      ans = max(p, ans);
      Ans[v] = ans;
      dfs(v, x);
      dp[p] = tmp;
      ans = tmpans;
    }
  // debug(x);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  rep(i, 1, n) cin >> a[i];
  rep(i, 1, n + 2) dp[i] = inf;
  dp[0] = -1;
  rep(i, 1, n - 1) {
    int x, y;
    cin >> x >> y;
    E[x].push_back(y);
    E[y].push_back(x);
  }
  E[0].push_back(1);
  dfs(0, 0);
  rep(i, 1, n) cout << Ans[i] << endl;
}
/*
dp[i]: LIS第i位的最小值
applying rollback technique to the dynamic programming of solving the longest
increasing subsequence problem.

Here the rollback refers to, when processing updates of values of variables and
questions one after another, withdrawing the changes as it wasbefore the latest
changes.

wr 下标a[v] , ans 没更新

 */

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, k) for (int i = (int)(j); i <= (int)(k); i++)
#define debug(x) cerr << #x << ":" << x << endl
#define fix cout << fixed << setprecision(20);
const int maxn = (int)1e6 + 5;
const int inf = (int)1e9 + 5;
int ans, n, dp[maxn], a[maxn], Ans[maxn];
vector<int> E[maxn];
void dfs(int x, int fa) {
  debug(x);
  for (auto v : E[x])
    if (v != fa) {
      int p = lower_bound(dp, dp + 1 + ans, a[v]) - dp;
      int tmp = dp[p], tmpans = ans;
      dp[p] = min(dp[p], a[v]);
      ans = max(p, ans);
      Ans[v] = ans;
      dfs(v, x);
      dp[p] = tmp;
      ans = tmpans;
    }
  debug(x);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  rep(i, 1, n) cin >> a[i];
  rep(i, 1, n + 2) dp[i] = inf;
  dp[0] = -1;
  rep(i, 1, n - 1) {
    int x, y;
    cin >> x >> y;
    E[x].push_back(y);
    E[y].push_back(x);
  }
  E[0].push_back(1);
  dfs(0, 0);
  rep(i, 1, n) cout << Ans[i] << endl;
}
/*
dp[i]: LIS第i位的最小值
applying rollback technique to the dynamic programming of solving the longest
increasing subsequence problem.

Here the rollback refers to, when processing updates of values of variables and
questions one after another, withdrawing the changes as it wasbefore the latest
changes.


 */