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

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

#define INF ((int)1e9)
#define INFLL ((ll)1e18)
#define MOD (1000000007LL)

vector<vector<int>> G;
vector<int> A;
vector<int> Len;
vector<int> Ans;

void DFS(int n, map<int, set<pair<int, int>>> &LenToVal) {
  if (Len[n] != 0) {
    return;
  }

  Len[n] = 1;
  for (auto it = LenToVal.rbegin(); it != LenToVal.rend(); ++it) {
    if (it->second.begin()->first < A[n]) {
      Len[n] = it->first + 1;
      break;
    }
  }

  LenToVal[Len[n]].emplace(A[n], n);
  Ans[n] = LenToVal.rbegin()->first;
  for (int m : G[n]) {
    DFS(m, LenToVal);
  }
  LenToVal[Len[n]].erase(make_pair(A[n], n));
  if (LenToVal[Len[n]].empty()) {
    LenToVal.erase(Len[n]);
  }
}

int main(int argc, const char *argv[]) {
  ios_base::sync_with_stdio(false);
  int N;
  cin >> N;
  A.assign(N, 0);
  for (int n = 0; n < N; ++n) {
    cin >> A[n];
  }
  G.assign(N, vector<int>());
  for (int n = 0; n < N - 1; ++n) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }

  Len.assign(N, 0);
  Ans.assign(N, 0);
  map<int, set<pair<int, int>>> LenToVal;
  DFS(0, LenToVal);

  for (int n = 0; n < N; ++n) {
    cout << Ans[n] << '\\n';
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

#define INF ((int)1e9)
#define INFLL ((ll)1e18)
#define MOD (1000000007LL)

vector<vector<int>> G;
vector<int> A;
vector<int> Len;
vector<int> Ans;

void DFS(int n, vector<int> &Traj) {
  if (Len[n] != 0) {
    return;
  }

  Len[n] = 1;
  for (int k : Traj) {
    if (A[k] < A[n]) {
      Len[n] = max(Len[k] + 1, Len[n]);
    }
  }

  Traj.push_back(n);
  for (int k : Traj) {
    Ans[n] = max(Ans[n], Len[k]);
  }

  for (int m : G[n]) {
    DFS(m, Traj);
  }
  Traj.pop_back();
}

int main(int argc, const char *argv[]) {
  ios_base::sync_with_stdio(false);
  int N;
  cin >> N;
  A.assign(N, 0);
  for (int n = 0; n < N; ++n) {
    cin >> A[n];
  }
  G.assign(N, vector<int>());
  for (int n = 0; n < N - 1; ++n) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }

  Len.assign(N, 0);
  Ans.assign(N, 0);
  vector<int> Traj;
  Traj.reserve(N);
  DFS(0, Traj);

  for (int n = 0; n < N; ++n) {
    cout << Ans[n] << '\\n';
  }

  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define For(i, n, k) for (int i = (n); i < (k); i++)
#define ALL(a) (a).begin(), (a).end()
const int INF = 2e9;
const int N_MAX = 200010;
int a[N_MAX];
vector<vector<int>> tree(N_MAX);
int ans[N_MAX];
stack<pair<int, int>> st;
void calc(int now, int root, vector<int> &vec) {
  auto itr = lower_bound(ALL(vec), a[now]);
  st.push(make_pair(itr - vec.begin(), *itr));
  *itr = a[now];
  if (itr - vec.begin() != ans[root]) {
    ans[now] = ans[root];
  } else {
    ans[now] = ans[root] + 1;
  }
  for (int i : tree[now]) {
    if (i == root)
      continue;
    calc(i, now, vec);
  }
  auto p = st.top();
  st.pop();
  vec[p.first] = p.second;
}
void Main() {
  int n;
  cin >> n;
  For(i, 0, n) cin >> a[i];
  For(i, 0, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    tree[u].push_back(v);
    tree[v].push_back(u);
  }
  vector<int> vec(n, INF);
  vec[0] = a[0];
  ans[0] = 1;
  for (int i : tree[0])
    calc(i, 0, vec);
  For(i, 0, n) cout << ans[i] << endl;
}
int main() {
  Main();
  /*
  東方風神録は神が出てくるので当然神ゲー
  */
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
#define For(i, n, k) for (int i = (n); i < (k); i++)
#define ALL(a) (a).begin(), (a).end()
const int N_MAX = 200010;
int a[N_MAX];
vector<vector<int>> tree(N_MAX);
int ans[N_MAX];
void calc(int now, int root, vector<int> vec) {
  auto itr = lower_bound(ALL(vec), a[now]);
  if (itr != vec.end()) {
    *itr = a[now];
    ans[now] = ans[root];
  } else {
    vec.push_back(a[now]);
    ans[now] = ans[root] + 1;
  }
  for (int i : tree[now]) {
    if (i == root)
      continue;
    calc(i, now, vec);
  }
}
void Main() {
  int n;
  cin >> n;
  For(i, 0, n) cin >> a[i];
  For(i, 0, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    tree[u].push_back(v);
    tree[v].push_back(u);
  }
  vector<int> vec = {a[0]};
  ans[0] = 1;
  for (int i : tree[0])
    calc(i, 0, vec);
  For(i, 0, n) cout << ans[i] << endl;
}
int main() {
  Main();
  /*
  東方風神録は神が出てくるので当然神ゲー
  */
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

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

const int INF = 1e9 + 7;
int N;

vector<int> to[200010];
int A[200010];
int ans[200010];
int dp[200010];
bool visited[200010];

void dfs(int now) {
  visited[now] = true;
  auto tmpIdx = lower_bound(dp, dp + N, A[now]);
  int tmpV = *tmpIdx;
  *tmpIdx = A[now];
  ans[now] = lower_bound(dp, dp + N, INF) - dp;

  for (int v : to[now]) {
    if (!visited[v]) {
      dfs(v);
    }
  }

  *tmpIdx = tmpV;
}

int main() {
  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--;
    to[u].push_back(v);
    to[v].push_back(u);
  }

  for (int i = 0; i < 200010; i++)
    dp[i] = INF;

  dfs(0);
  for (int i = 0; i < N; i++)
    cout << ans[i] << endl;
}

Error: 0

Fixed submission:

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

const int INF = 1e9;
int N;

vector<int> to[100010];
int A[100010];
int ans[100010];
int dp[100010];
bool visited[100010];

void dfs(int now) {
  visited[now] = true;
  auto tmpIdx = lower_bound(dp, dp + N, A[now]);
  int tmpV = *tmpIdx;
  *tmpIdx = A[now];
  ans[now] = lower_bound(dp, dp + N, INF) - dp;

  for (int v : to[now]) {
    if (!visited[v]) {
      dfs(v);
    }
  }

  *tmpIdx = tmpV;
}

int main() {
  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--;
    to[u].push_back(v);
    to[v].push_back(u);
  }

  for (int i = 0; i < 100010; i++)
    dp[i] = INF;

  dfs(0);
  for (int i = 0; i < N; i++)
    cout << ans[i] << endl;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

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

long long N;
long long A[200005];
vector<long long> tree[200005];
long long dp[200005];
long long ans[200005];

// dp[i]:= 長さが i+1
// であるような増加部分列における最終要素の最小値(存在しない場合は INF)
void dfs(long long pa, long long cur) {
  // cout << "pa " << pa << " cur " << cur << endl;
  long long num = A[cur];

  auto it = lower_bound(dp, dp + N, num);
  long long old = *it;
  *it = num;
  ans[cur] = lower_bound(dp, dp + N, LONG_MAX) - dp;
  for (long long i = 0; i < tree[cur].size(); i++) {
    if (tree[cur][i] != pa) {
      dfs(cur, tree[cur][i]);
    }
  }
  *it = old;
}

int main() {
  cin >> N;
  for (long long i = 0; i < N; i++) {
    cin >> A[i];
  }

  for (long long i = 0; i < N - 1; i++) {
    long long u, v;
    cin >> u >> v;
    u--;
    v--;
    tree[u].push_back(v);
    tree[v].push_back(u);
  }

  fill(dp, dp + N, LONG_MAX);
  dfs(-1, 0);

  for (long long i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
}


Error: 0

Fixed submission:

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

long long N;
long long A[200005];
vector<long long> tree[200005];
long long dp[100005];
long long ans[100005];

// dp[i]:= 長さが i+1
// であるような増加部分列における最終要素の最小値(存在しない場合は INF)
void dfs(long long pa, long long cur) {
  // cout << "pa " << pa << " cur " << cur << endl;
  long long num = A[cur];

  auto it = lower_bound(dp, dp + N, num);
  long long old = *it;
  *it = num;
  ans[cur] = lower_bound(dp, dp + N, LONG_MAX) - dp;
  for (long long i = 0; i < tree[cur].size(); i++) {
    if (tree[cur][i] != pa) {
      dfs(cur, tree[cur][i]);
    }
  }
  *it = old;
}

int main() {
  cin >> N;
  for (long long i = 0; i < N; i++) {
    cin >> A[i];
  }

  for (long long i = 0; i < N - 1; i++) {
    long long u, v;
    cin >> u >> v;
    u--;
    v--;
    tree[u].push_back(v);
    tree[v].push_back(u);
  }

  fill(dp, dp + N, LONG_MAX);
  dfs(-1, 0);

  for (long long i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

int N;
ll ans[202020];
ll dp[202020];
ll a[202020];
vector<int> to[202020];

void dfs(int u, int p) {
  int i = (int)(lower_bound(dp, dp + N, a[u]) - dp);
  // cout << i << endl;
  int old = dp[i];
  dp[i] = a[u];
  ans[u] = lower_bound(dp, dp + N, inf) - dp;
  for (int v : to[u]) {
    if (v == p)
      continue;
    dfs(v, u);
  }
  dp[i] = old;
}

int main() {
  cin >> N;
  rep(i, N) { cin >> a[i]; }
  rep(i, N - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    to[u].push_back(v);
    to[v].push_back(u);
  }
  rep(i, 202020) { dp[i] = inf; }
  dfs(0, -1);
  rep(i, N) { cout << ans[i] << endl; }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

int N;
ll ans[202020];
ll dp[202020];
ll a[202020];
vector<int> to[202020];

void dfs(int u, int p) {
  int i = (int)(lower_bound(dp, dp + N, a[u]) - dp);
  // cout << i << endl;
  int old = dp[i];
  dp[i] = a[u];
  ans[u] = lower_bound(dp, dp + N, inf) - dp;
  for (int v : to[u]) {
    if (v == p)
      continue;
    dfs(v, u);
  }
  dp[i] = old;
}

int main() {
  cin >> N;
  rep(i, N) { cin >> a[i]; }
  rep(i, N) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    to[u].push_back(v);
    to[v].push_back(u);
  }
  rep(i, N) { dp[i] = inf; }
  dfs(0, -1);
  rep(i, N) { cout << ans[i] << endl; }
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const int inf = 1e9;
const ll INF = 1e18;
const ld EPS = 1e-10;

const int MAX_V = 2e5;
vector<int> es[MAX_V];
ll dp[MAX_V];
int ans[MAX_V];
ll A[MAX_V];

void dfs(int now, int pre) {
  int j = lower_bound(dp, dp + MAX_V, A[now]) - dp;
  ll memo = dp[j];
  dp[j] = A[now];
  ans[now] = lower_bound(dp, dp + MAX_V, INF) - dp;
  for (auto &e : es[now]) {
    if (e == pre)
      continue;
    dfs(e, now);
  }
  dp[j] = memo;
}

int main() {
  int N;
  cin >> N;
  rep(i, N) cin >> A[i];
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    es[a].pb(b), es[b].pb(a);
  }
  fill(dp, dp + MAX_V, INF);
  dfs(0, -1);
  rep(i, N) { cout << ans[i] << endl; }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const int inf = 1e9;
const ll INF = 1e18;
const ld EPS = 1e-10;

const int MAX_V = 1e5;
vector<int> es[MAX_V];
ll dp[MAX_V];
int ans[MAX_V];
ll A[MAX_V];

void dfs(int now, int pre) {
  int j = lower_bound(dp, dp + MAX_V, A[now]) - dp;
  ll memo = dp[j];
  dp[j] = A[now];
  ans[now] = lower_bound(dp, dp + MAX_V, INF) - dp;
  for (auto &e : es[now]) {
    if (e == pre)
      continue;
    dfs(e, now);
  }
  dp[j] = memo;
}

int main() {
  int N;
  cin >> N;
  rep(i, N) cin >> A[i];
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    es[a].pb(b), es[b].pb(a);
  }
  fill(dp, dp + MAX_V, INF);
  dfs(0, -1);
  rep(i, N) { cout << ans[i] << endl; }
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

vector<int> g[200005], dp;

int fa[200005], ans[200005], v[200005];

void dfs(int x, int fath) {
  int flag = 0;
  int locate, vx;
  if (dp.size() == 0) {
    flag = 1;
    dp.push_back(v[x]);
  } else if (v[x] > dp[dp.size() - 1]) {
    flag = 1;
    dp.push_back(v[x]);
  } else {
    flag = 2;
    locate = lower_bound(dp.begin(), dp.end(), v[x]) - dp.begin();
    vx = dp[locate];
    *lower_bound(dp.begin(), dp.end(), v[x]) = v[x];
  }
  ans[x] = dp.size();
  for (int i = 0; i < g[x].size(); i++) {
    int t1 = g[x][i];
    if (t1 == fath)
      continue;
    dfs(t1, x);
  }
  if (flag == 1)
    dp.pop_back();
  else {
    dp[locate] = vx;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
  }
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

vector<int> g[200005];

int fa[200005], ans[200005], v[200005];

void dfs(vector<int> dp, int x, int fath) {
  if (dp.size() == 0)
    dp.push_back(v[x]);
  else if (v[x] > dp[dp.size() - 1])
    dp.push_back(v[x]);
  else
    *lower_bound(dp.begin(), dp.end(), v[x]) = v[x];
  ans[x] = dp.size();
  for (int i = 0; i < g[x].size(); i++) {
    int t1 = g[x][i];
    if (t1 == fath)
      continue;
    dfs(dp, t1, x);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
  }
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vector<int> dp;
  dp.clear();
  dfs(dp, 1, 0);
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << '\\n';
  }
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define INF 1000000007

vector<int> a;
vector<vector<int>> to;
vector<int> ans;

vector<bool> done;

void dfs(int id, int length, vector<int> &dp) {
  done[id] = true;
  bool plus = false;
  int bound_id;
  int befdp;
  if (id == 0) {
    dp[0] = a[0];
    length = 1;
  } else {
    if (dp[length - 1] < a[id]) {
      dp[length++] = a[id];
      plus = true;
    } else {
      auto itr = lower_bound(dp.begin(), dp.begin() + length, a[id]);
      bound_id = distance(dp.begin(), itr);
      // cerr << bound_id << endl;
      befdp = dp[bound_id];
      dp[bound_id] = a[id];
    }
  }
  /*
  cout << plus << " " << length << endl;
  cout << "id : " << id << " : ";
  for(int i=0;i<=dp.size();i++)
  {
          if(i==dp.size() or dp[i]==INF)
          {
                  cout << endl;
                  break;
          }
          cout << dp[i] << " ";
  }
  */
  ans[id] = length;
  for (int i = 0; i < to[id].size(); i++) {
    int nx = to[id][i];
    if (done[nx])
      continue;
    dfs(nx, length, dp);
  }
  if (plus) {
    dp[length] = INF;
    length--;
  } else {
    dp[bound_id] = befdp;
  }
}

int main() {
  int n;
  cin >> n;
  a = vector<int>(n);
  to = vector<vector<int>>(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--;
    to[u].push_back(v);
    to[v].push_back(u);
  }

  ans = vector<int>(n, 0);
  done = vector<bool>(n, false);

  vector<int> dp(n, INF);

  dfs(0, 0, dp);

  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define INF 1000000007

vector<int> a;
vector<vector<int>> to;
vector<int> ans;

vector<bool> done;

void dfs(int id, int length, vector<int> dp) {
  done[id] = true;
  if (id == 0) {
    dp[0] = a[0];
    length = 1;
  } else {
    if (dp[length - 1] < a[id])
      dp[length++] = a[id];
    else
      *lower_bound(dp.begin(), dp.begin() + length, a[id]) = a[id];
  }
  ans[id] = length;
  for (int i = 0; i < to[id].size(); i++) {
    int nx = to[id][i];
    if (done[nx])
      continue;
    dfs(nx, length, dp);
  }
}

int main() {
  int n;
  cin >> n;
  a = vector<int>(n);
  to = vector<vector<int>>(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--;
    to[u].push_back(v);
    to[v].push_back(u);
  }

  ans = vector<int>(n, 0);
  done = vector<bool>(n, false);

  vector<int> dp(n, INF);

  dfs(0, 0, dp);

  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(obj) (obj).begin(), (obj).end()
typedef pair<int, int> P;
const int mod = 1e9 + 7;
vector<int> dp, ans, a, g[200005];
void dfs(int v, int p = -1) {
  int i = lower_bound(all(dp), a[v]) - dp.begin();
  int old = dp[i];
  dp[i] = a[v];
  ans[v] = i;
  if (p != -1)
    ans[v] = max(ans[v], ans[p]);
  for (auto u : g[v]) {
    if (u == p)
      continue;
    dfs(u, v);
  }
  dp[i] = old;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  a = ans = vector<int>(n);
  rep(i, n) cin >> a[i];
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dp = vector<int>(n + 1, mod);
  dp[0] = -1;
  dfs(0);
  rep(i, n) cout << ans[i] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(obj) (obj).begin(), (obj).end()
typedef pair<int, int> P;
const int mod = 1e9 + 7;
vector<int> dp, ans, a, g[200005];
void dfs(int v, int p = -1) {
  int i = lower_bound(all(dp), a[v]) - dp.begin();
  int old = dp[i];
  dp[i] = a[v];
  ans[v] = i;
  if (p != -1)
    ans[v] = max(ans[v], ans[p]);
  for (auto u : g[v]) {
    if (u == p)
      continue;
    dfs(u, v);
  }
  dp[i] = old;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  a = ans = vector<int>(n);
  rep(i, n) cin >> a[i];
  rep(i, n) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dp = vector<int>(n + 1, mod);
  dp[0] = -1;
  dfs(0);
  rep(i, n) cout << ans[i] << endl;
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second

const int mx = 200010;
const ll mod = 1e9 + 7;

vector<int> v[mx];
vector<int> ans(mx, 1);
vector<int> a(mx);
vector<int> dp(mx, inf);

void dfs(int n, int p) {
  int idx = lower_bound(dp.begin(), dp.end(), a[n]) - dp.begin();
  int tmp = dp[idx];
  dp[idx] = a[n];
  ans[n] = lower_bound(dp.begin(), dp.end(), inf) - dp.begin();
  for (auto to : v[n]) {
    if (to == p)
      continue;
    dfs(to, n);
  }
  dp[idx] = tmp;
}

int main() {
  int n;
  cin >> n;
  rep(i, n) { cin >> a[i]; }
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].emplace_back(b);
    v[b].emplace_back(a);
  }
  dfs(0, -1);

  rep(i, n) { cout << ans[i] << endl; }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second

const int mx = 200010;
const ll mod = 1e9 + 7;

vector<vector<int>> tmp;
vector<int> v[mx];

void f(int n, int p, vector<int> &b) {
  if (v[n].size() == 1 && n != 0) {
    tmp.emplace_back(b);
    return;
  }
  for (auto to : v[n]) {
    if (to == p)
      continue;
    b.emplace_back(to);
    f(to, n, b);
    b.pop_back();
  }
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> l(n, 1);
  rep(i, n) { cin >> a[i]; }
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].emplace_back(b);
    v[b].emplace_back(a);
  }
  vector<int> qw;
  qw.emplace_back(0);
  f(0, -1, qw);

  auto go = [&](int n, vector<int> &name) {
    vector<int> L(n + 1, 0);
    L[0] = a[name[0]];
    int length = 1;
    for (int i = 1; i < n; i++) {
      int t = name[i];
      if (L[length - 1] < a[t]) {
        L[length++] = a[t];
      } else {
        *lower_bound(L.begin(), L.begin() + length, a[t]) = a[t];
      }
      l[t] = length;
    }
    return length;
  };

  for (auto t : tmp) {
    go(t.size(), t);
  }

  for (auto ans : l) {
    cout << ans << endl;
  }
  return 0;
}