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

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define pr pair<int, int>
#define mod 1000000007
#define endl "\\n"
#define N 200005
vector<int> a, ans, g[N];
int n;

void dfs(int u, int parent, vector<int> &v) {
  int m = v.size(), x = -1, prev;
  if (m == 0 || v[m - 1] < a[u])
    v.pb(a[u]);
  else {
    x = lower_bound(v.begin(), v.end(), a[u]) - v.begin();
    prev = v[x];
    v[x] = a[u];
  }
  ans[u] = v.size();
  for (auto vv : g[u]) {
    if (vv == parent)
      continue;
    dfs(vv, u, v);
  }
  if (x == -1)
    v.pop_back();
  else
    v[x] = prev;
}

void solve() {
  cin >> n;
  ans = a = vector<int>(n);
  int i, x, y;
  for (i = 0; i < n; i++)
    cin >> a[i];
  for (i = 1; i < n; i++) {
    cin >> x >> y;
    x--, y--;
    g[x].pb(y);
    g[y].pb(x);
  }
  vector<int> v;
  dfs(0, 0, v);
  for (i = 0; i < n; i++)
    cout << ans[i] << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1; // cin>>t;
  while (t--)
    solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define pr pair<int, int>
#define mod 1000000007
#define endl "\\n"
#define N 200005
vector<int> a, ans, g[N];
int n;

void dfs(int u, int parent, vector<int> v) {
  int m = v.size();
  if (m == 0 || v[m - 1] < a[u])
    v.pb(a[u]);
  else {
    int x = lower_bound(v.begin(), v.end(), a[u]) - v.begin();
    v[x] = a[u];
  }
  ans[u] = v.size();
  for (auto vv : g[u]) {
    if (vv == parent)
      continue;
    dfs(vv, u, v);
  }
}

void solve() {
  cin >> n;
  ans = a = vector<int>(n);
  int i, x, y;
  for (i = 0; i < n; i++)
    cin >> a[i];
  for (i = 1; i < n; i++) {
    cin >> x >> y;
    x--, y--;
    g[x].pb(y);
    g[y].pb(x);
  }
  vector<int> v;
  dfs(0, 0, v);
  for (i = 0; i < n; i++)
    cout << ans[i] << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1; // cin>>t;
  while (t--)
    solve();
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define fi first
#define se second
#define pb push_back
#define forn(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define forr(i, n) for (int i = n; i >= 0; i--)
#define all(x) x.begin(), x.end()
const int MAXN = 2e5 + 5;

void fio() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

vector<vector<int>> adj(MAXN);

LL max(LL a, LL b) { return a > b ? a : b; }

int n;
vector<LL> v;
int an[MAXN];
vector<int> lc;

void dfs(int u, int p) {
  auto x = upper_bound(all(lc), v[u]) - lc.begin();
  int ch = -1;
  int val;
  if (x >= 1 && lc[x - 1] != v[u] || x == 0) {
    ch = x;
    val = lc[x];
    lc[x] = v[u];
  }
  an[u] = lower_bound(all(lc), INT_MAX) - lc.begin();
  for (auto i : adj[u]) {
    if (i != p) {
      dfs(i, u);
    }
  }
  if (ch != -1)
    lc[x] = val;
}

int main() {
  fio();
  cin >> n;
  set<LL> s;
  forn(i, n) {
    int x;
    cin >> x;
    v.pb(x);
  }
  lc.resize(n + 1);
  forn(i, n + 1) lc[i] = INT_MAX;
  forn(i, n - 1) {
    int u, x;
    cin >> u >> x;
    u--, x--;
    adj[x].pb(u);
    adj[u].pb(x);
  }
  dfs(0, -1);
  forn(i, n) cout << an[i] << endl;

  return 0;
}


Error: -6
free(): invalid pointer


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define fi first
#define se second
#define pb push_back
#define forn(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define forr(i, n) for (int i = n; i >= 0; i--)
#define all(x) x.begin(), x.end()
const int MAXN = 2e5 + 5;

void fio() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

vector<vector<int>> adj(MAXN);

LL max(LL a, LL b) { return a > b ? a : b; }

int n;
vector<LL> v;
int an[MAXN];
vector<int> lc;

void dfs(int u, int p) {
  //	cout<<u<<" "<<p<<endl;
  int ch = -1;
  int val = -1;
  //	cout<<lc.size()<<endl;
  if (lc.size() == 0) {
    //	cout<<"yes"<<endl;
    //	cout<<v[u]<<endl;
    lc.pb(v[u]);

    ch = -1;
  } else {

    auto pt = upper_bound(all(lc), v[u]) - lc.begin();
    if (pt >= 1 && lc[pt - 1] == v[u]) {
      ch = -2;
    } else {
      if (upper_bound(all(lc), v[u]) == lc.end()) {
        ch = -1;
        lc.pb(v[u]);
      } else {
        val = lc[pt];
        lc[pt] = v[u];
        ch = pt;
      }
    }
  }

  an[u] = lc.size();
  for (auto i : adj[u]) {
    if (i != p) {
      dfs(i, u);
    }
  }
  if (ch == -2)
    return;
  if (ch == -1)
    lc.pop_back();
  lc[ch] = val;
}

int main() {
  fio();
  cin >> n;
  set<LL> s;
  forn(i, n) {
    int x;
    cin >> x;
    v.pb(x);
  }
  forn(i, n - 1) {
    int u, x;
    cin >> u >> x;
    u--, x--;
    adj[x].pb(u);
    adj[u].pb(x);
  }
  dfs(0, -1);
  forn(i, n) cout << an[i] << endl;

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

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;

struct v_t {
  int a, c, l;
  vi e;
};

void solve_i(vector<v_t> &v, vi &w, int p) {
  v_t &c = v[p];

  for (int me : c.e) {
    v_t &n = v[me];

    if (n.l)
      continue;
    auto it = lower_bound(w.begin(), w.end(), n.a);
    if (it == w.end()) {
      w.push_back(n.a);
      n.l = w.size() - 1;
      solve_i(v, w, me);
      w.pop_back();
    } else {
      int x = it - w.begin();
      int s = w[x];
      w[x] = n.a;
      n.l = w.size() - 1;
      solve_i(v, w, me);
      w[x] = s;
    }
  }
}

void solve(vector<v_t> &v) {
  vi w;

  w.reserve(v.size());
  w.push_back(0);
  w.push_back(v[1].a);
  v[1].l = w.size() - 1;
  solve_i(v, w, 1);
}

int main(void) {
  int n;
  while (scanf("%d", &n) == 1) {
    vector<v_t> v(n + 1);
    for (int i = 1; i <= n; i++)
      scanf("%d", &v[i].a);

    for (int i = 0; i < n - 1; i++) {
      int f, t;

      scanf("%d%d", &f, &t);
      v[f].e.push_back(t);
      v[t].e.push_back(f);
    }

    solve(v);
    for (int i = 1; i <= n; i++) {
      printf("%d\\n", v[i].l);
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;

struct v_t {
  int a, c, l;
  vi e;
};

void solve_i(vector<v_t> &v, vi &w, int p) {
  v_t &c = v[p];

  for (int me : c.e) {
    v_t &n = v[me];

    if (n.l)
      continue;
    auto it = upper_bound(w.begin(), w.end(), n.a);
    if (it == w.end()) {
      w.push_back(n.a);
      n.l = w.size() - 1;
      solve_i(v, w, me);
      w.pop_back();
    } else {
      int s = *it;
      *it = n.a;
      n.l = w.size() - 1;
      solve_i(v, w, me);
      *it = s;
    }
  }
}

void solve(vector<v_t> &v) {
  vi w;
  vi q;

  v[1].l = 1;
  w.push_back(0);
  w.push_back(v[1].a);
  solve_i(v, w, 1);
}

int main(void) {
  int n;
  while (scanf("%d", &n) == 1) {
    vector<v_t> v(n + 1);
    for (int i = 1; i <= n; i++)
      scanf("%d", &v[i].a);

    for (int i = 0; i < n - 1; i++) {
      int f, t;

      scanf("%d%d", &f, &t);
      v[f].e.push_back(t);
      v[t].e.push_back(f);
    }

    solve(v);
    for (int i = 1; i <= n; i++) {
      printf("%d\\n", v[i].l);
    }
  }
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

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

int N;
vector<vector<int>> graph(222222);
vector<int> a;

vector<bool> visited(222222, false);
vector<int> dp(222222);
vector<vector<int>> hist(222222);
vector<int> ans(222222);

void DFS(int x) {
  auto it = lower_bound(dp.begin(), dp.begin() + N, a[x]);
  int p = it - dp.begin();
  hist[p].push_back(a[x]);
  dp[p] = a[x];
  ans[x] =
      (int)(lower_bound(dp.begin(), dp.begin() + N, 999999999) - dp.begin());
  for (auto t : graph[x]) {
    if (!visited[t]) {
      visited[t] = true;
      DFS(t);
    }
  }
  hist[p].erase(hist[p].begin() + hist[p].size() - 1);
  dp[p] = hist[p][hist[p].size() - 1];
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    a.push_back(tmp);
  }
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    graph[u].push_back(v);
    graph[v].push_back(u);
  }

  for (int i = 0; i < 222222; i++)
    dp[i] = ans[i] = 999999999;
  for (int i = 0; i < 222222; i++)
    hist[i].push_back(999999999);

  visited[0] = true;
  DFS(0);

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

  return 0;
}

Error: 0

Fixed submission:

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

int N;
vector<vector<int>> graph(222222);
vector<int> a;

vector<bool> visited(222222, false);
vector<int> dp(222222);
vector<vector<int>> hist(222222);
map<int, int> pos;
vector<int> ans(222222);

void DFS(int x) {
  ans[x] = (lower_bound(dp.begin(), dp.begin() + N, 999999999) - dp.begin());
  for (auto t : graph[x]) {
    if (!visited[t]) {
      visited[t] = true;
      auto it = lower_bound(dp.begin(), dp.begin() + N, a[t]);
      int p = it - dp.begin();
      hist[p].push_back(a[t]);
      dp[p] = a[t];
      pos.insert(make_pair(a[t], p));
      DFS(t);
    }
  }
  int p = pos[a[x]];
  hist[p].erase(hist[p].begin() + hist[p].size() - 1);
  dp[p] = hist[p][hist[p].size() - 1];
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    a.push_back(tmp);
  }
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    graph[u].push_back(v);
    graph[v].push_back(u);
  }

  for (int i = 0; i < 222222; i++)
    dp[i] = ans[i] = 999999999;
  for (int i = 0; i < 222222; i++)
    hist[i].push_back(999999999);

  visited[0] = true;
  dp[0] = a[0];
  hist[0].push_back(a[0]);
  pos.insert(make_pair(a[0], 0));
  DFS(0);

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

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Map = map<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using T = tuple<ll, ll, vl>;
ll INF = 1LL << 60;
ll MOD = 1000000007;
vector<ll> longest(200001, 1);
vector<vector<ll>> gr(200001, vector<ll>(0));
vector<ll> d(200001, INF);
vector<ll> a(200001, 0);

void dfs(ll x, vl &vec) {
  for (auto k : gr[x]) {
    if (d[k] == d[x] + 1) {
      auto x = lower_bound(vec.begin(), vec.end(), a[k]);
      auto y = *x;
      *x = a[k];
      longest[k] = lower_bound(vec.begin(), vec.end(), INF) - vec.begin();
      dfs(k, vec);
      *x = y;
    };
  }
}

int main() {
  ll N;
  cin >> N;
  vector<ll> u(N - 1, 0);
  vector<ll> v(N - 1, 0);
  for (ll i = 0; i < N; i++) {
    cin >> a[i];
  }
  for (ll i = 0; i < N - 1; i++) {
    cin >> u[i] >> v[i];
    u[i]--;
    v[i]--;
    ;
  }
  for (ll i = 0; i < N - 1; i++) {
    gr[u[i]].emplace_back(v[i]);
    gr[v[i]].emplace_back(u[i]);
    ;
  }

  d[0] = 0;
  queue<ll> qu;
  // 訪問フラグ
  vector<ll> check(N, 0);
  check[0] = 1;

  // bfs
  qu.push(0);
  while (!qu.empty()) {
    ll x = qu.front();
    qu.pop();
    for (ll i = 0; i < gr[x].size(); i++) {
      ll k = gr[x][i];
      if (check[k])
        continue;
      d[k] = d[x] + 1;
      qu.push(k);
      check[k] = 1;
    }
  }

  vector<ll> vec(N, INF);
  vec[0] = a[0];

  dfs(0, vec);

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

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Map = map<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using T = tuple<ll, ll, vl>;
ll INF = 1LL << 60;
ll MOD = 1000000007;

int main() {
  ll N;
  cin >> N;
  vector<ll> a(N, 0);
  vector<ll> u(N - 1, 0);
  vector<ll> v(N - 1, 0);
  for (ll i = 0; i < N; i++) {
    cin >> a[i];
  }
  for (ll i = 0; i < N - 1; i++) {
    cin >> u[i] >> v[i];
    u[i]--;
    v[i]--;
    ;
  }
  vector<vector<ll>> gr(N, vector<ll>(0));
  for (ll i = 0; i < N - 1; i++) {
    gr[u[i]].emplace_back(v[i]);
    gr[v[i]].emplace_back(u[i]);
    ;
  }

  vector<ll> d(N, INF);
  d[0] = 0;
  vector<ll> longest(N, 1);
  queue<ll> qu;
  // 訪問フラグ
  vector<ll> check(N, 0);
  check[0] = 1;
  vector<vector<ll>> vec(N, vector<ll>(N, INF));
  vec[0][0] = a[0];
  qu.push(0);
  while (!qu.empty()) {
    ll x = qu.front();
    qu.pop();
    for (ll i = 0; i < gr[x].size(); i++) {
      ll k = gr[x][i];
      if (check[k])
        continue;
      vl w = vec[x];
      *lower_bound(w.begin(), w.end(), a[k]) = a[k];
      d[k] = d[x] + 1;
      vec[k] = w;
      qu.push(k);
      longest[k] = lower_bound(w.begin(), w.end(), INF) - w.begin();
      check[k] = 1;
    }
  }
  for (ll i = 0; i < N; i++) {
    cout << longest[i] << endl;
    ;
  }

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

// 6/15 解き直し
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

const int MAX_N = 200010;
// int dp[MAX_N];
const int INF = 1000000009;

vector<int> a;

void dfs(int v, Graph &G, vector<int> &dp, vector<int> &lis) {
  int change_idx;
  int before_value;
  int n = a.size();
  // cout << "dfs v = " << v << endl;
  // *lower_bound(dp.begin(), dp.end(), a[v]) = a[v];
  // lis[v] = lower_bound(dp.begin(), dp.end(), INF) - dp.begin();

  change_idx = lower_bound(dp.begin(), dp.end(), a[v]) - dp.begin();
  // cout << "change_idx is " << change_idx << endl;
  if (change_idx < n) {
    // 値を更新する時
    before_value = dp[change_idx];
    dp[change_idx] = a[v];
  }
  lis[v] = lower_bound(dp.begin(), dp.end(), INF) - dp.begin();

  // vector<int> dp_clone;  // (n);
  // if (G[v].size() > 2) {
  //     dp_clone.resize(n);
  // }

  for (auto nv : G[v]) {
    // 既にlisが設定されていたら、それは親ノード。
    if (lis[nv] != -1)
      continue;

    // 子であればもういっかいdfsを回す。dpはポインタ渡しではないので値は更新されない...はず。頼むぞdp...！
    dfs(nv, G, dp, lis);
  }

  if (change_idx < n) {
    dp[change_idx] = before_value;
  }
}

int main() {
  int n;
  cin >> n;
  // vector<int> a(n);
  a.resize(n);
  rep(i, n) cin >> a[i];

  Graph G(n);
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }

  // fill(dp, dp + n, INF);
  vector<int> dp(n, INF);
  vector<int> lis(n, -1);
  stack<P> log; // {変更するdpのINDEX, 変更前の値}

  dfs(0, G, dp, lis);

  for (int l : lis)
    cout << l << endl;
}

Error: 0

Fixed submission:

// 6/15 解き直し
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

const int MAX_N = 200010;
// int dp[MAX_N];
const ll INF = 1000000009;

vector<ll> a;

void dfs(int v, Graph &G, vector<ll> dp, vector<int> &lis) {
  // cout << "dfs v = " << v << endl;
  *lower_bound(dp.begin(), dp.end(), a[v]) = a[v];
  lis[v] = lower_bound(dp.begin(), dp.end(), INF) - dp.begin();

  for (auto nv : G[v]) {
    // 既にlisが設定されていたら、それは親ノード。
    if (lis[nv] != -1)
      continue;

    // 子であればもういっかいdfsを回す。dpはポインタ渡しではないので値は更新されない...はず。頼むぞdp...！
    dfs(nv, G, dp, lis);
  }
}

int main() {
  int n;
  cin >> n;
  // vector<int> a(n);
  a.resize(n);
  rep(i, n) cin >> a[i];

  Graph G(n);
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }

  // fill(dp, dp + n, INF);
  vector<ll> dp(n, INF);
  vector<int> lis(n, -1);

  dfs(0, G, dp, lis);

  for (int l : lis)
    cout << l << endl;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int n;
int a[200000];
int pa[200000];
vector<int> ch[200000];
int ans[200000];
void dfs(int now, int dp[200000], int mem) {
  int md;
  int id = lower_bound(dp, dp + n, a[now]) - dp;
  if (dp[id] == 1e9 + 7) {
    md = dp[id];
    dp[id] = a[now];
    ans[now] = mem + 1;
  } else {
    md = dp[id];
    dp[id] = a[now];
    ans[now] = mem;
  }
  for (int i = 0; i < ch[now].size(); i++) {
    if (ch[now][i] == pa[now]) {
      continue;
    } else {
      pa[ch[now][i]] = now;
      dfs(ch[now][i], dp, ans[now]);
    }
  }
  dp[id] = md;
}
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--;
    ch[u].push_back(v);
    ch[v].push_back(u);
  }
  pa[0] = -1;
  int dp[200000];
  for (int i = 0; i < n; i++) {
    dp[i] = 1e9 + 7;
  }
  dfs(0, dp, 0);
  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int n;
int a[200000];
int pa[200000];
vector<int> ch[200000];
int ans[200000];
void dfs(int now, int dp[200000], int mem) {
  *lower_bound(dp, dp + n, a[now]) = a[now];
  ans[now] = (lower_bound(dp, dp + n, 1e9 + 7) - dp);
  for (int i = 0; i < ch[now].size(); i++) {
    if (ch[now][i] == pa[now]) {
      continue;
    } else {
      pa[ch[now][i]] = now;
      int dpp[200000];
      copy(dp, dp + n, dpp);
      dfs(ch[now][i], dpp, ans[now]);
    }
  }
}
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--;
    ch[u].push_back(v);
    ch[v].push_back(u);
  }
  pa[0] = -1;
  int dp[200000];
  fill(dp, dp + n, 1e9 + 7);
  dfs(0, dp, 0);
  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

int a[2 * 100000];
int lis[2 * 100000];

void dfs(const vector<vector<int>> &edges, int node, vector<int> *state) {
  vector<int>::iterator it = lower_bound(state->begin(), state->end(), a[node]);
  bool extend = it == state->end();
  int original;
  if (extend) {
    state->push_back(a[node]);
  } else {
    original = *it;
    *it = a[node];
  }
  lis[node] = state->size();
  for (int i = 0; i < edges[node].size(); ++i) {
    int n = edges[node][i];
    if (lis[n] == -1) {
      dfs(edges, n, state);
    }
  }
  if (extend) {
    state->pop_back();
  } else {
    *it = original;
  }
}

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

  vector<vector<int>> edges;
  edges.resize(N);
  for (int i = 0; i < N - 1; ++i) {
    int u, v;
    cin >> u >> v;
    edges[u - 1].push_back(v - 1);
    edges[v - 1].push_back(u - 1);
  }

  for (int i = 0; i < N; ++i) {
    lis[i] = -1;
  }
  vector<int> state;
  state.reserve(N);
  dfs(edges, 0, &state);
  for (int i = 0; i < N; ++i) {
    cout << lis[i] << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int a[2 * 100000];
int lis[2 * 100000];

void dfs(const vector<vector<int>> &edges, int node, vector<int> *state) {
  vector<int>::iterator it = lower_bound(state->begin(), state->end(), a[node]);
  bool extend = it == state->end();
  int original;
  if (extend) {
    state->push_back(a[node]);
  } else {
    original = *it;
    *it = a[node];
  }
  lis[node] = state->size();
  for (int i = 0; i < edges[node].size(); ++i) {
    int n = edges[node][i];
    if (lis[n] == -1) {
      dfs(edges, n, state);
    }
  }
  if (extend) {
    state->pop_back();
  } else {
    *it = original;
  }
}

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

  vector<vector<int>> edges;
  edges.resize(N);
  for (int i = 0; i < N - 1; ++i) {
    int u, v;
    cin >> u >> v;
    edges[u - 1].push_back(v - 1);
    edges[v - 1].push_back(u - 1);
  }

  for (int i = 0; i < N; ++i) {
    lis[i] = -1;
  }
  vector<int> state;
  dfs(edges, 0, &state);
  for (int i = 0; i < N; ++i) {
    cout << lis[i] << endl;
  }
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

// -----------------------------------
// Author     : MatsuTaku
// Country    : Japan
// Created    : 05/02/20 20:07:37
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr int INF = 1e9 + 1;

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

  int n;
  cin >> n;
  vector<int> A(n);
  for (auto &a : A)
    cin >> a;
  vector<int> graph[n];
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    graph[u].push_back(v);
    graph[v].push_back(u);
  }
  vector<int> dp(n, INF);
  vector<int> ans(n);
  auto dfs = [&graph, &dp, &A, &ans](auto &f, int s, int p) -> void {
    auto bound = lower_bound(dp.begin(), dp.end(), A[s]);
    bool changed = false;
    int prev = *bound;
    if (*bound > A[s]) {
      changed = true;
      *bound = A[s];
    }
    ans[s] = lower_bound(dp.begin(), dp.end(), INF) - dp.begin();
    for (auto t : graph[s]) {
      if (t == p)
        continue;
      f(f, t, s);
    }
    if (changed)
      *bound = prev;
  };
  dfs(dfs, 0, -1);
  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

// -----------------------------------
// Author     : MatsuTaku
// Country    : Japan
// Created    : 05/02/20 20:07:37
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr int INF = 1e9 + 1;

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

  int n;
  cin >> n;
  vector<int> A(n);
  for (auto &a : A)
    cin >> a;
  vector<int> graph[n];
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    graph[u].push_back(v);
    graph[v].push_back(u);
  }
  vector<vector<int>> dp(n, vector<int>(n, INF));
  auto dfs = [&graph, &dp, &A](auto &f, int s, int p) -> void {
    if (s != 0)
      dp[s] = dp[p];
    auto bound = lower_bound(dp[s].begin(), dp[s].end(), A[s]);
    *bound = min(*bound, A[s]);
    for (auto t : graph[s]) {
      if (t == p)
        continue;
      f(f, t, s);
    }
  };
  dfs(dfs, 0, -1);
  for (int i = 0; i < n; i++) {
    cout << lower_bound(dp[i].begin(), dp[i].end(), INF) - dp[i].begin()
         << endl;
  }

  return 0;
}