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)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll mod = 1000000007;
const double eps = 1e-8;

#ifdef DEBUG
#include "inc/debug.hpp"
#else
#define debug(...) 42
#endif

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

  vector<vector<int>> to(N);
  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);
  }

  vector<int> ans(N, 0);
  vector<int> lis;

  function<void(int, int)> dfs = [&](int u, int par) {
    auto it = lower_bound(lis.begin(), lis.end(), a[u]);

    // dump
    tuple<bool, int, int> dump; // pop, pos, val
    if (it == lis.end()) {
      dump = make_tuple(true, -1, -1);
      lis.push_back(a[u]);
    } else {
      dump = make_tuple(false, it - lis.begin(), *it);
      *it = a[u];
    }

    ans[u] = max(ans[u], (int)lis.size());
    for (int v : to[u]) {
      if (v == par)
        continue;
      dfs(v, u);
    }

    // revert
    bool pop;
    int pos, val;
    tie(pop, pos, val) = dump;
    if (pop)
      lis.pop_back();
    else
      lis[pos] = val;
  };
  dfs(0, -1);

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

  return 0;
}

Error: TLE

Fixed submission:

#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>;

const ll mod = 1000000007;
const double eps = 1e-8;

#ifdef DEBUG
#include "inc/debug.hpp"
#else
#define debug(...) 42
#endif

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

  vector<vector<int>> to(N);
  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);
  }

  vector<int> ans(N, 0);
  vector<int> lis;

  function<void(int, int)> dfs = [&](int u, int par) {
    vector<int> dump = lis;
    auto it = lower_bound(lis.begin(), lis.end(), a[u]);
    if (it == lis.end())
      lis.push_back(a[u]);
    else
      *it = a[u];
    ans[u] = max(ans[u], (int)lis.size());
    for (int v : to[u]) {
      if (v == par)
        continue;
      dfs(v, u);
    }
    lis = dump;
  };
  dfs(0, -1);

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

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

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

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

void dfs(int cur, int pre) {
  int tmp = lower_bound(dp.begin(), dp.end(), a[cur]) - dp.begin();
  int par = dp[tmp];
  dp[tmp] = a[cur];
  ans[cur] = tmp;
  if (pre != -1)
    ans[cur] = max(ans[cur], ans[pre]);
  for (auto &&next : to[cur]) {
    if (next == pre)
      continue;
    dfs(next, cur);
  }
  dp[tmp] = par;
}

int main() {
  int n;
  cin >> n;
  to.resize(n);
  a = ans = vector<int>(n);
  dp = vector<int>(n, 1e9);
  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);
  }
  dp[0] = -1e9;
  dfs(0, -1);
  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;
  return 0;
}

Error: 0

Fixed submission:

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

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

void dfs(int cur, int pre, vector<int> dp) {
  int tmp = lower_bound(dp.begin(), dp.end(), a[cur]) - dp.begin();
  dp[tmp] = a[cur];
  for (auto &&next : to[cur]) {
    if (next == pre)
      continue;
    dfs(next, cur, dp);
  }
  ans[cur] = lower_bound(dp.begin(), dp.end(), 1e9) - dp.begin();
}

int main() {
  int n;
  cin >> n;
  to.resize(n);
  a.resize(n);
  ans.resize(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);
  }
  dfs(0, -1, vector<int>(n, 1e9));
  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
constexpr int MOD = 998244353;
constexpr int INF = numeric_limits<int>::max() / 2;
typedef pair<int, int> P;
using Graph = vector<vector<int>>;

vector<int> B(200010, INF);
vector<int> A(200010);
vector<int> ans(200010);
vector<bool> seen;
void dfs(const Graph &G, int v) {
  seen[v] = true; // v を訪問済みにする
  auto itr = lower_bound(B.begin(), B.end(), A[v]);
  int old = *itr;
  *itr = A[v];
  ans[v] = lower_bound(B.begin(), B.end(), INF) - B.begin();
  // vから行ける各頂点 next_v について
  for (auto nv : G[v]) {
    if (seen[nv])
      continue; // next_v が探索済みだったらスルー
    dfs(G, nv); // 再帰的に探索
  }
  *itr = old;
}

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

  int N;
  cin >> N;
  rep(i, N) cin >> A[i];
  Graph G(N);
  int a, b;
  rep(i, N - 1) {
    cin >> a >> b;
    --a;
    --b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  seen.assign(N, false); // 全頂点を「未訪問」にして初期化
  dfs(G, 0);
  rep(i, N) cout << ans[i] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
constexpr int MOD = 998244353;
constexpr int INF = numeric_limits<int>::max() / 2;
typedef pair<int, int> P;
using Graph = vector<vector<int>>;

vector<int> B(100010, INF);
vector<int> A(100010);
vector<int> ans(100010);
vector<bool> seen;
void dfs(const Graph &G, int v) {
  seen[v] = true; // v を訪問済みにする
  auto itr = lower_bound(B.begin(), B.end(), A[v]);
  int old = *itr;
  *itr = A[v];
  ans[v] = itr - B.begin() + 1;
  // vから行ける各頂点 next_v について
  for (auto nv : G[v]) {
    if (seen[nv])
      continue; // next_v が探索済みだったらスルー
    dfs(G, nv); // 再帰的に探索
  }
  *itr = old;
}

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

  int N;
  cin >> N;
  rep(i, N) cin >> A[i];
  Graph G(N);
  int a, b;
  rep(i, N - 1) {
    cin >> a >> b;
    --a;
    --b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  seen.assign(N, false); // 全頂点を「未訪問」にして初期化
  dfs(G, 0);
  rep(i, N) cout << ans[i] << endl;
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, int> LP;
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 int INF = 1e9 + 5;

int n;
vector<int> a, ans;
vector<vector<int>> G;

void dfs(int v, int p, vector<int> &vec) {
  for (auto nv : G[v]) {
    if (nv == p)
      continue;
    auto idx = lower_bound(vec.begin(), vec.end(), a[nv]);
    int origin = *idx;
    *idx = a[nv];
    ans[nv] = lower_bound(vec.begin(), vec.end(), INF) - vec.begin();
    dfs(nv, v, vec);
    *idx = origin;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  a.resize(n);
  rep(i, n) cin >> a[i];
  G.resize(n);
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  ans.resize(n, -1);
  ans[0] = 1;
  vector<int> v(n + 1, INF);
  v[0] = a[0];
  dfs(0, -1, v);
  rep(i, n) cout << ans[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, int> LP;
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 int INF = 1e9 + 5;

int n;
vector<int> a, ans;
vector<vector<int>> G;

void dfs(int v, int p, vector<int> &vec) {
  for (auto nv : G[v]) {
    if (nv == p)
      continue;
    vector<int> w = vec;
    auto idx = lower_bound(w.begin(), w.end(), a[nv]);
    *idx = a[nv];
    if (w.back() != INF)
      w.push_back(INF);
    ans[nv] = w.size() - 1;
    dfs(nv, v, w);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  a.resize(n);
  rep(i, n) cin >> a[i];
  G.resize(n);
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  ans.resize(n, -1);
  ans[0] = 1;
  vector<int> v(2, INF);
  v[0] = a[0];
  dfs(0, -1, v);
  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;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 2147483647;
const ll LINF = 9223372036854775807;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

int n;
VI a(214514);
VVI edge(214514, VI(0));
VI ans(214514);
VI dp(214514, LINF);

void dfs(int v, int p) {
  int itr = lower_bound(ALL(dp), a[v]) - dp.begin();
  ll z = dp[itr];
  dp[itr] = a[v];
  ans[v] = lower_bound(ALL(dp), INF) - dp.begin();
  for (auto to : edge[v])
    if (to != p)
      dfs(to, v);
  dp[itr] = z;
}

int main() {
  cin >> n;
  REP(i, n) cin >> a[i];
  int u, v;
  REP(i, n - 1) {
    cin >> u >> v;
    u--;
    v--;
    edge[u].push_back(v);
    edge[v].push_back(u);
  }
  dfs(0, -1);
  REP(i, n)
  cout << ans[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 2147483647;
const ll LINF = 9223372036854775807;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

int n;
VI a(214514);
VVI edge(214514, VI(0));
VI ans(214514);

void dfs(int v, int p, VI dp) {
  *lower_bound(ALL(dp), a[v]) = a[v];
  ans[v] = lower_bound(ALL(dp), INF) - dp.begin();
  for (auto to : edge[v])
    if (to != p)
      dfs(to, v, dp);
}

int main() {
  cin >> n;
  REP(i, n) cin >> a[i];
  int u, v;
  REP(i, n - 1) {
    cin >> u >> v;
    u--;
    v--;
    edge[u].push_back(v);
    edge[v].push_back(u);
  }
  VI dp(n, LINF);
  dfs(0, -1, dp);
  REP(i, n)
  cout << ans[i] << endl;
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define pi acos(-1)
#define ll long long
#define pii pair<ll, ll>
#define debug(a) cout << a << '\\n'
#define maxn 200009 /// Still trying to be normal
#define MOD 1000000007
#define F first
#define S second
#define rep(i, a, b) for (ll i = a; i < (b); ++i)
#define per(i, b, a) for (ll i = b - 1; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define allin(a, x) for (auto a : x)
#define all(x) begin(x), end(x)
#define sz(x) (ll)(x).size()
using namespace std;
const ll INF = 1e9 + 15;
ll vet[maxn];
ll ans[maxn];
ll vis[maxn];
vector<ll> adj[maxn];
vector<ll> lis;
void dfs(ll x) {
  auto it = (ll)(lower_bound(all(lis), vet[x]) - lis.begin());
  bool acrescentou = false;
  ll antigo = -1;
  if (it == lis.size())
    lis.push_back(vet[x]), acrescentou = true;
  else
    antigo = lis[it], lis[it] = vet[x];
  vis[x] = 1;
  ans[x] = lis.size();
  for (auto e : adj[x]) {
    if (vis[e] == -1) {
      dfs(e);
    }
  }
  if (acrescentou)
    lis.pop_back();
  else
    lis[it] = antigo;
}
void addedge(ll a, ll b) {
  adj[a].push_back(b);
  adj[b].push_back(a);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  cin >> n;
  rep(i, 1, n + 1) cin >> vet[i];
  memset(vis, -1, sizeof vis);
  ll n2 = n - 1;
  while (n2--) {
    ll a, b;
    cin >> a >> b;
    addedge(a, b);
  }
  vector<ll> lis2;
  dfs(1);
  rep(i, 1, n + 1) cout << ans[i] << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define pi acos(-1)
#define ll long long
#define pii pair<ll, ll>
#define debug(a) cout << a << '\\n'
#define maxn 200009 /// Still trying to be normal
#define MOD 1000000007
#define F first
#define S second
#define rep(i, a, b) for (ll i = a; i < (b); ++i)
#define per(i, b, a) for (ll i = b - 1; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define allin(a, x) for (auto a : x)
#define all(x) begin(x), end(x)
#define sz(x) (ll)(x).size()
using namespace std;
const ll INF = 1e9 + 15;
ll vet[maxn];
ll ans[maxn];
ll vis[maxn];
vector<ll> adj[maxn];
vector<ll> lis;
void dfs(ll x) {
  auto it = lower_bound(all(lis), vet[x]);
  bool acrescentou = false;
  ll antigo = -1;
  if (it == lis.end())
    lis.push_back(vet[x]), acrescentou = true;
  else
    antigo = (*it), *it = vet[x];
  vis[x] = 1;
  ans[x] = lis.size();
  for (auto e : adj[x]) {
    if (vis[e] == -1) {
      dfs(e);
    }
  }
  if (acrescentou)
    lis.pop_back();
  else
    *it = antigo;
}
void addedge(ll a, ll b) {
  adj[a].push_back(b);
  adj[b].push_back(a);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  cin >> n;
  rep(i, 1, n + 1) cin >> vet[i];
  memset(vis, -1, sizeof vis);
  ll n2 = n - 1;
  while (n2--) {
    ll a, b;
    cin >> a >> b;
    addedge(a, b);
  }
  vector<ll> lis2;
  dfs(1);
  rep(i, 1, n + 1) cout << ans[i] << '\\n';
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

vector<int> edge[200001];
int arr[200001];
int n;
int res[200001];
bool visited[200001];
set<int> lis;

void dfs(int now) {
  int val = -1;
  if (lis.empty()) {
    lis.insert(arr[now]);
  } else {
    auto it = lis.end();
    it--;
    if (*it < arr[now]) {
      lis.insert(arr[now]);
    } else {
      auto it = lis.lower_bound(arr[now]);
      if (it != lis.end()) {
        val = *it;
        lis.erase(val);
        lis.insert(arr[now]);
      }
    }
  }
  res[now] = lis.size();
  for (auto next : edge[now]) {
    if (visited[next] == false) {
      visited[next] = true;
      dfs(next);
    }
  }
  lis.erase(arr[now]);
  if (val != -1) {
    lis.insert(val);
  }
}
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }

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

  memset(visited, false, sizeof(visited));
  visited[1] = true;
  dfs(1);

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

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

vector<int> lis[200001];
vector<int> edge[200001];
int arr[200001];
int n;
int res[200001];
bool visited[200001];

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

  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }

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

  memset(visited, false, sizeof(visited));
  queue<int> que;
  que.push(1);
  lis[1].push_back(arr[1]);
  visited[1] = true;
  while (!que.empty()) {
    int now = que.front();
    que.pop();
    res[now] = lis[now].size();
    for (auto next : edge[now]) {
      if (visited[next] == true) {
        continue;
      }
      visited[next] = true;
      lis[next] = lis[now];
      if (lis[next].size() == 0 || lis[next].back() < arr[next]) {
        lis[next].push_back(arr[next]);
      } else {
        auto it = lower_bound(lis[next].begin(), lis[next].end(), arr[next]);
        *it = arr[next];
      }
      que.push(next);
    }
    lis[now].clear();
  }

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

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

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

const int N = 2e5 + 5;
vector<int> adj[N];
int n;
vector<ll> arr(N);
set<ll> sz[N];
vector<ll> res(N);
set<ll> ans;

void dp(int node, int par) {
  for (auto i : adj[node]) {
    if (i != par) {
      if (arr[i] > *prev(ans.end())) {
        ans.insert(arr[i]);
        res[i] = ans.size();
        dp(i, node);
        ans.erase(arr[i]);
      } else {
        auto get = ans.lower_bound(arr[i]);
        ll val = *get;
        ans.erase(get);
        ans.insert(arr[i]);
        res[i] = ans.size();
        dp(i, node);
        ans.erase(arr[i]);
        ans.insert(val);
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
  }
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  ans.insert(arr[0]);
  res[0] = 1;
  dp(0, -1);
  for (int i = 0; i < n; ++i) {
    cout << res[i] << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

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

const int N = 2e5 + 5;
vector<int> adj[N];
int n;
vector<ll> arr(N);
set<ll> sz[N];

void dp(int node, int par) {
  for (auto i : adj[node]) {
    if (i != par) {
      sz[i] = sz[node];
      if (arr[i] > *prev(sz[i].end())) {
        sz[i].insert(arr[i]);
      } else {
        auto get = sz[i].lower_bound(arr[i]);
        sz[i].erase(get);
        sz[i].insert(arr[i]);
      }
      dp(i, node);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
  }
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  sz[0].insert(arr[0]);
  dp(0, -1);
  for (int i = 0; i < n; ++i) {
    cout << sz[i].size() << '\\n';
  }
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX_N = 200005;
const int INF = (1e9) + 5;
bool visited[MAX_N];
vector<int> G[MAX_N];
int a[MAX_N], dp[MAX_N], ans[MAX_N];
int N;

void init() {
  rep(i, MAX_N) { dp[i] = INF; }
}

void dfs(int x) {
  if (visited[x])
    return;
  visited[x] = true;
  auto it = lower_bound(dp, dp + N, a[x]);
  int idx = it - dp;
  int v = *it;
  *it = a[x];
  ans[x] = lower_bound(dp, dp + N, INF) - dp;
  rep(i, G[x].size()) { dfs(G[x][i]); }
  dp[idx] = v;
  return;
}

int main() {
  init();
  cin >> 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);
  }
  vector<int> dp;
  dp.push_back(a[0]);
  dfs(0);
  rep(i, N) { cout << ans[i] << endl; }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX_N = 200005;
bool visited[MAX_N];
vector<int> G[MAX_N];
int a[MAX_N], dp[MAX_N], ans[MAX_N];

void dfs(vector<int> dp, int x) {
  if (visited[x])
    return;
  visited[x] = true;
  auto it = lower_bound(dp.begin(), dp.end(), a[x]);
  if (it == dp.end())
    dp.push_back(a[x]);
  else
    *it = a[x];
  ans[x] = dp.size();
  rep(i, G[x].size()) {
    if (!visited[G[x][i]])
      dfs(dp, G[x][i]);
  }
}

int main() {
  int N;
  cin >> 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);
  }
  vector<int> dp;
  dp.push_back(a[0]);
  dfs(dp, 0);
  rep(i, N) { cout << ans[i] << endl; }
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

ll A[200005];
struct Vertex {
  vector<ll> node;
};

Vertex V[200005];
ll C[200005];

void dfs(vector<ll> &v, ll index = 0, ll par = -1) {
  ll a = A[index];
  ll mi = lower_bound(v.begin(), v.end(), a) - v.begin();
  ll t = v[mi];
  v[mi] = a;
  C[index] = lower_bound(v.begin(), v.end(), INF) - v.begin();
  REP(i, V[index].node.size()) {
    ll n = V[index].node[i];
    if (n == par)
      continue;
    dfs(v, n, index);
  }
  v[mi] = t;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  REP(i, N) { cin >> A[i]; }
  REP(i, N) { C[i] = 0; }
  REP(i, N - 1) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    V[a].node.push_back(b);
    V[b].node.push_back(a);
  }

  vector<ll> v(N + 5, INF);
  dfs(v);

  REP(i, N) { cout << C[i] << endl; }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

ll A[200005];
struct Vertex {
  vector<ll> node;
};

Vertex V[200005];
ll C[200005];

void dfs(vector<ll> v, ll index = 0, ll par = -1) {
  ll a = A[index];
  ll mi = lower_bound(v.begin(), v.end(), a) - v.begin();
  C[index] = mi;
  ll t = v[mi];
  v[mi] = a;
  REP(i, V[index].node.size()) {
    ll n = V[index].node[i];
    dfs(v, n, index);
  }
  v[mi] = t;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  REP(i, N) { cin >> A[i]; }
  REP(i, N) { C[i] = 0; }
  REP(i, N - 1) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    V[a].node.push_back(b);
    V[b].node.push_back(a);
  }

  vector<ll> v(N + 5, INF);
  dfs(v);

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
using Edge = pair<int, ll>;
using Graph = vector<vector<int>>;
using WeightedGraph = vector<vector<Edge>>;
const ll INF = 1LL << 60;
const int INF32 = 1 << 29;
const ll MOD = 1000000007;

Graph G;
vector<ll> A;
vector<ll> ans;

void dfs(int u, int p, vector<ll> &vec, vector<ll> &dp) {

  int idx = lower_bound(dp.begin(), dp.end(), vec.back()) - dp.begin();
  ll old = dp[idx];
  dp[idx] = vec.back();
  ans[u] = lower_bound(dp.begin(), dp.end(), INF) - dp.begin();

  for (auto e : G[u]) {
    if (e == p)
      continue;
    vec.push_back(A[e]);
    dfs(e, u, vec, dp);
    vec.pop_back();
  }
  dp[idx] = old;
}

int main() {
  ll n;
  cin >> n;
  A.resize(n);
  for (int i = 0; i < n; ++i)
    cin >> A[i];

  ans.resize(n);
  G.resize(n);
  for (int i = 0; i < n - 1; ++i) {
    ll u, v;
    cin >> u >> v;
    --u;
    --v;
    G[u].push_back(v);
    G[v].push_back(u);
  }

  vector<ll> dp(2 * n, INF), buf;
  buf.push_back(A[0]);

  dfs(0, -1, buf, dp);
  for (int i = 0; i < n; ++i) {
    cout << ans[i] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
using Edge = pair<int, ll>;
using Graph = vector<vector<int>>;
using WeightedGraph = vector<vector<Edge>>;
const ll INF64 = 1LL << 60;
const int INF32 = 1 << 29;
const ll MOD = 1000000007;

Graph G;
vector<ll> A;
void dfs(int u, int p, vector<ll> &vec, vector<ll> &dp) {

  int idx = lower_bound(dp.begin(), dp.end(), vec.back()) - dp.begin();
  ll old = dp[idx];
  dp[idx] = vec.back();
  cout << lower_bound(dp.begin(), dp.end(), INF32) - dp.begin() << endl;

  for (auto e : G[u]) {
    if (e == p)
      continue;
    vec.push_back(A[e]);
    dfs(e, u, vec, dp);
    vec.pop_back();
  }
  dp[idx] = old;
}

int main() {
  ll n;
  cin >> n;
  A.resize(n);
  for (int i = 0; i < n; ++i)
    cin >> A[i];

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

  vector<ll> dp(n, INF32), buf;
  buf.push_back(A[0]);

  dfs(0, 0, buf, dp);

  return 0;
}