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

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define ll long long

using namespace std;

long long MOD = 1000000007;

void dfs(int x, vector<int> &lis, vector<int> &A, vector<vector<int>> &edges,
         vector<int> &ans) {
  if (ans[x] > 0) {
    return;
  }
  auto it = lower_bound(lis.begin(), lis.end(), A[x]);
  int idx, prev;
  if (it == lis.end()) {
    prev = -1;
    lis.push_back(A[x]);
  } else {
    prev = *it;
    idx = distance(lis.begin(), it);
    *it = A[x];
  }
  ans[x] = lis.size();

  int M = edges[x].size();
  for (int i = 0; i < M; i++) {
    dfs(edges[x][i], lis, A, edges, ans);
  }

  if (prev == -1) {
    lis.pop_back();
  } else {
    lis[idx] = prev;
  }
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (auto &a : A) {
    cin >> a;
  }

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

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

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define ll long long

using namespace std;

long long MOD = 1000000007;

void dfs(int x, vector<int> &lis, vector<int> &A, vector<vector<int>> &edges,
         vector<int> &ans) {
  if (ans[x] > 0) {
    return;
  }
  auto it = lower_bound(lis.begin(), lis.end(), A[x]);
  if (it == lis.end()) {
    lis.push_back(A[x]);
  } else {
    *it = A[x];
  }
  ans[x] = lis.size();

  int M = edges[x].size();
  for (int i = 0; i < M; i++) {
    if (i == M - 1) {
      dfs(edges[x][i], lis, A, edges, ans);
    } else {
      vector<int> tmp(lis.size());
      copy(lis.begin(), lis.end(), tmp.begin());
      dfs(edges[x][i], tmp, A, edges, ans);
    }
  }
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (auto &a : A) {
    cin >> a;
  }

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

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

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

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

typedef long long ll;
const ll inf = 1e18;
int N;
vector<int> g[200100];
ll a[200100];
ll dp[200100];
ll ans[200100];

void solve(int u, int pa = -1) {
  int ind = lower_bound(dp, dp + N, a[u]) - dp;
  ll prev = dp[ind];
  dp[ind] = a[u];
  /*
  for(int i=0;i<N;i++){
      if(dp[i]==inf) cout << "inf" << " ";
      else cout << dp[i] << " ";
  }
  cout << endl;
  */
  ans[u] = lower_bound(dp, dp + N, inf) - dp;
  for (auto v : g[u]) {
    if (v == pa)
      continue;
    solve(v, u);
  }
  dp[ind] = prev;
}

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--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 0; i < N; i++) {
    dp[i] = inf;
  }
  solve(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 ll inf = 1e18;
int N;
vector<int> g[100100];
ll a[200100];
ll dp[200100];
ll ans[200100];

void solve(int u, int pa = -1) {
  int ind = lower_bound(dp, dp + N, a[u]) - dp;
  ll prev = dp[ind];
  dp[ind] = a[u];
  /*
  for(int i=0;i<N;i++){
      if(dp[i]==inf) cout << "inf" << " ";
      else cout << dp[i] << " ";
  }
  cout << endl;
  */
  ans[u] = lower_bound(dp, dp + N, inf) - dp;
  for (auto v : g[u]) {
    if (v == pa)
      continue;
    solve(v, u);
  }
  dp[ind] = prev;
}

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--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 0; i < N; i++) {
    dp[i] = inf;
  }
  solve(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;

const int INF = 1e9 + 1;

int N;
vector<int> a;
vector<vector<int>> neighbors;
vector<int> res;
vector<int> dp;

void dfs(int current, int parent = -1) {
  vector<int>::iterator it = lower_bound(dp.begin(), dp.end(), a.at(current));
  int prev = *it;
  *it = a.at(current);

  res.at(current) = distance(dp.begin(), it) + 1;
  if (parent != -1) {
    res.at(current) = max(res.at(current), res.at(parent));
  }

  for (auto &&next : neighbors.at(current)) {
    if (next == parent) {
      continue;
    }
    dfs(next, current);
  }

  *it = prev;
}

int main() {
  cin >> N;
  a = vector<int>(N);
  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
  }
  neighbors = vector<vector<int>>(N, vector<int>());
  int ui, vi;
  for (int i = 0; i < N - 1; i++) {
    cin >> ui >> vi;
    ui--;
    vi--;
    neighbors.at(ui).push_back(vi);
    neighbors.at(vi).push_back(ui);
  }

  res = vector<int>(N);
  dp = vector<int>(N, INF);
  dfs(0);

  for (int i = 0; i < N; i++) {
    cout << res.at(i) << endl;
  }
}

Error: 0

Fixed submission:

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

int N;
vector<int> a;
vector<vector<int>> neighbors;
vector<int> res;
vector<int> dp;

void dfs(int current, int parent) {
  bool added = false;
  vector<int>::iterator it;
  int prev;
  if (dp.size() == 0 || dp.back() < a.at(current)) {
    dp.push_back(a.at(current));
    added = true;
  } else {
    it = lower_bound(dp.begin(), dp.end(), a.at(current));
    prev = *it;
    *it = a.at(current);
  }

  res.at(current) = dp.size();

  for (auto &&next : neighbors.at(current)) {
    if (next == parent) {
      continue;
    }
    dfs(next, current);
  }

  if (added) {
    dp.pop_back();
  } else {
    *it = prev;
  }
}

int main() {
  cin >> N;
  a = vector<int>(N);
  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
  }
  neighbors = vector<vector<int>>(N, vector<int>());
  int ui, vi;
  for (int i = 0; i < N - 1; i++) {
    cin >> ui >> vi;
    ui--;
    vi--;
    neighbors.at(ui).push_back(vi);
    neighbors.at(vi).push_back(ui);
  }

  res = vector<int>(N);
  dp = vector<int>();
  dfs(0, -1);

  for (int i = 0; i < N; i++) {
    cout << res.at(i) << endl;
  }
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef int64_t llo;
#define mp make_pair
#define a first
#define b second
#define pb push_back
int ma = 0;
int ss[200001];
int ans[200001];
int it[200001];
vector<int> adj[200001];
void dfs(int no, int par = -1) {
  pair<int, pair<int, int>> rev;
  if (ma == 0) {
    ss[0] = it[no];
    ma += 1;
    rev = {-1, {-1, -1}};
  } else if (it[no] > ss[ma - 1]) {
    ma += 1;
    ss[ma - 1] = it[no];
    rev = {-1, {-1, -1}};
  } else {
    auto ind = lower_bound(ss, ss + ma, it[no]) - ss;
    rev = {ind, {ss[ind], it[no]}};
    ss[ind] = it[no];
  }
  ans[no] = ma;
  for (auto j : adj[no]) {
    if (j == par) {
      continue;
    }
    dfs(j, no);
  }
  if (rev.a != -1) {
    ss[rev.a] = rev.b.a;
  } else {
    ma -= 1;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> it[i];
  }
  int aa, bb;
  for (int i = 0; i < n - 1; i++) {
    cin >> aa >> bb;
    adj[aa - 1].pb(bb - 1);
    adj[bb - 1].pb(aa - 1);
  }
  dfs(0, -1);
  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef int64_t llo;
#define mp make_pair
#define a first
#define b second
#define pb push_back
int ma = 0;
int ss[200001];
int ans[200001];
int it[200001];
vector<int> adj[200001];
void dfs(int no, int par = -1) {
  pair<int, pair<int, int>> rev;
  if (ma == 0) {
    ss[0] = it[no];
    ma += 1;
    rev = {-1, {-1, -1}};
  } else if (it[no] > ss[ma - 1]) {
    ma += 1;
    ss[ma - 1] = it[no];
    rev = {-1, {-1, -1}};
  } else if (it[no] == ss[ma - 1]) {
  } else {
    int low = 0;
    int high = ma - 1;
    while (low > high - 1) {
      int mid = (low + high) / 2;
      if (ss[mid] > it[no]) {
        high = mid;
      } else {
        low = mid + 1;
      }
    }
    int ind = high;
    if (ss[low] > it[no]) {
      ind = min(ind, low);
    }
    rev = {ind, {ss[ind], it[no]}};
    ss[ind] = it[no];
  }
  ans[no] = ma;
  for (auto j : adj[no]) {
    if (j == par) {
      continue;
    }
    dfs(j, no);
  }
  if (rev.a != -1) {
    ss[rev.a] = rev.b.a;
  } else {
    ma -= 1;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> it[i];
  }
  int aa, bb;
  for (int i = 0; i < n - 1; i++) {
    cin >> aa >> bb;
    adj[aa - 1].pb(bb - 1);
    adj[bb - 1].pb(aa - 1);
  }
  dfs(0, -1);
  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>
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define MOD 1000000007
using namespace std;
vector<int> ans, a;
vector<vector<int>> edge;
vector<int> dp;
void dfs(int now, int prev) {
  auto ind = lower_bound(all(dp), a[now]);
  int val = *ind;
  *ind = a[now];
  ans[now] = lower_bound(all(dp), __INT_MAX__) - dp.begin();
  for (auto next : edge[now]) {
    if (next != prev) {
      dfs(next, now);
    }
  }
  *ind = val;
}
int main() {
  int n;
  cin >> n;
  ans.resize(n, 0);
  a.resize(n, 0);
  edge.resize(n, vector<int>(0));
  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--;
    edge[u].push_back(v);
    edge[v].push_back(u);
  }
  vector<int> hoge(a.size(), __INT_MAX__);
  dp = hoge;
  dfs(0, -1);
  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define MOD 1000000007
using namespace std;
vector<int> ans, a;
vector<vector<int>> edge;
void dfs(int now, int prev, vector<int> dp) {
  dp.push_back(__INT_MAX__);
  *lower_bound(all(dp), a[now]) = a[now];
  ans[now] = lower_bound(all(dp), __INT_MAX__) - dp.begin();
  for (auto next : edge[now]) {
    if (next != prev) {
      dfs(next, now, dp);
    }
  }
}
int main() {
  int n;
  cin >> n;
  ans.resize(n, 0);
  a.resize(n, 0);
  edge.resize(n, vector<int>(0));
  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--;
    edge[u].push_back(v);
    edge[v].push_back(u);
  }
  vector<int> dp(0);
  dfs(0, -1, dp);
  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;

void solve(int s, const Graph &gph, const vector<int> &data, vector<int> *ans,
           vector<int> *dp) {
  auto itr = lower_bound(dp->begin(), dp->end(), data.at(s));
  int idx = distance(dp->begin(), itr);
  int prev = dp->at(idx);
  dp->at(idx) = data.at(s);
  auto itr_ans = lower_bound(dp->begin(), dp->end(), INT_MAX);
  ans->at(s) = distance(dp->begin(), itr_ans);

  for (auto v : gph.at(s)) {
    if (ans->at(v) == 0) {
      solve(v, gph, data, ans, dp);
    }
  }
  dp->at(idx) = prev;
}

int main() {
  int N;
  cin >> N;
  const int M = N - 1;

  vector<int> data(N);
  Graph gph(N);
  for (int i = 0; i < N; i++) {
    cin >> data.at(i);
  }
  int buf1, buf2;
  for (int j = 0; j < M; j++) {
    cin >> buf1 >> buf2;
    buf1--;
    buf2--; // 1-indexed -> 0-indexed

    gph.at(buf1).emplace_back(buf2);
    gph.at(buf2).emplace_back(buf1);
  }

  vector<int> ans(N);
  vector<int> dp(N, INT_MAX);
  solve(0, gph, data, &ans, &dp);

  for (auto elem : ans) {
    cout << elem << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;

void solve(int s, const Graph &gph, const vector<int> &data, vector<int> *ans,
           vector<int> dp) {
  auto itr = lower_bound(dp.begin(), dp.end(), data.at(s));
  int idx = distance(dp.begin(), itr);
  dp.at(idx) = data.at(s);
  auto itr_ans = lower_bound(dp.begin(), dp.end(), INT_MAX);
  ans->at(s) = distance(dp.begin(), itr_ans);

  for (auto v : gph.at(s)) {
    if (ans->at(v) == 0) {
      solve(v, gph, data, ans, dp);
    }
  }
}

int main() {
  int N;
  cin >> N;
  const int M = N - 1;

  vector<int> data(N);
  Graph gph(N);
  for (int i = 0; i < N; i++) {
    cin >> data.at(i);
  }
  int buf1, buf2;
  for (int j = 0; j < M; j++) {
    cin >> buf1 >> buf2;
    buf1--;
    buf2--; // 1-indexed -> 0-indexed

    gph.at(buf1).emplace_back(buf2);
    gph.at(buf2).emplace_back(buf1);
  }

  vector<int> ans(N);
  vector<int> dp(N, INT_MAX);
  solve(0, gph, data, &ans, dp);

  for (auto elem : ans) {
    cout << elem << endl;
  }
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

int a[200001];
int d[200001];
int n;
vector<int> v[200001];
vector<int> tmp;

void go(int x, int p) {
  for (int next : v[x]) {
    if (next == p)
      continue;
    if (tmp.back() < a[next]) {
      tmp.push_back(a[next]);
      d[next] = tmp.size();
      go(next, x);
      tmp.pop_back();
    } else {
      auto p = lower_bound(tmp.begin(), tmp.end(), a[next]);
      int l = 0;
      int r = tmp.size() - 1;
      while (l <= r) {
        int mid = (l + r) / 2;
        if (tmp[mid] >= a[next]) {
          r = mid - 1;
        } else
          l = mid + 1;
      }
      int tt = tmp[l];
      tmp[l] = a[next];
      d[next] = tmp.size();
      go(next, x);
      tmp[l] = tt;
    }
  }
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  tmp.push_back(a[1]);
  d[1] = 1;
  go(1, 0);
  for (int i = 1; i <= n; i++) {
    cout << d[i] << '\\n';
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

int a[200001];
int d[200001];
int n;

vector<int> v[200001];

void go(vector<int> &tmp, int x, int p) {
  for (int next : v[x]) {
    if (next == p)
      continue;
    vector<int> tmp2 = tmp;
    if (tmp2.back() < a[next]) {
      tmp2.push_back(a[next]);
    } else {
      auto p = lower_bound(tmp2.begin(), tmp2.end(), a[next]);
      *p = a[next];
    }
    d[next] = tmp2.size();
    go(tmp2, next, x);
  }
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  vector<int> tmp;
  tmp.push_back(a[1]);
  d[1] = 1;
  go(tmp, 1, 0);

  for (int i = 1; i <= n; i++) {
    cout << d[i] << '\\n';
  }
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

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

#define GO                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long int

const int maxr = 2e5 + 10;

int n, arr[maxr], ans[maxr];
vector<int> adj[maxr];
vector<int> lis;

void dfs(int start, int parent = -1) {
  bool first = false;
  auto itr = lower_bound(lis.begin(), lis.end(), arr[start]);
  int value = -1;
  int idx = -1;
  if (itr == lis.end()) {
    lis.push_back(arr[start]);
    first = true;
  } else {
    idx = itr - lis.begin();
    value = lis[idx];
    lis[idx] = arr[start];
  }
  ans[start] = lis.size();
  for (auto x : adj[start]) {
    if (x == parent)
      continue;
    dfs(x, start);
  }
  if (first && lis.size()) {
    lis.pop_back();
  } else {
    lis[idx] = value;
  }
}

int32_t main() {
  GO;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    adj[a - 1].push_back(b - 1);
    adj[b - 1].push_back(a - 1);
  }
  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;

#define GO                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long int

const int maxr = 2e5 + 10;

int n, arr[maxr], ans[maxr];
vector<int> adj[maxr];
vector<int> lis;

void dfs(int start, int parent = -1) {
  bool first = false;
  auto itr = lower_bound(lis.begin(), lis.end(), arr[start]);
  int value = -1;
  if (itr == lis.end()) {
    lis.push_back(arr[start]);
    first = true;
  } else {
    value = *(itr);
    *(itr) = arr[start];
  }
  ans[start] = lis.size();
  for (auto x : adj[start]) {
    if (x == parent)
      continue;
    dfs(x, start);
  }
  if (first && lis.size()) {
    lis.pop_back();
  } else {
    assert(!(value == -1));
    *(itr) = value;
  }
}

int32_t main() {
  GO;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    adj[a - 1].push_back(b - 1);
    adj[b - 1].push_back(a - 1);
  }
  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;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPi(i, a, b) for (int i = int(a); i < int(b); i++)
#define MEMS(a, b) memset(a, b, sizeof(a))
#define mp make_pair
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll MOD = 1e9 + 7;
const ll INF = 1e12;

ll N;
ll A[200100];
vector<ll> G[200100];
ll ans[200100];

void dfs(ll v, vector<ll> &table, ll parant) {
  int idx = lower_bound(table.begin(), table.end(), A[v]) - table.begin();
  ll orig = table[idx];
  if (table[idx] > A[v])
    table[idx] = A[v];

  ll len = lower_bound(table.begin(), table.end(), INF) - table.begin();
  ans[v] = len;

  int first = -1;
  for (auto &&n : G[v]) {
    if (n == parant)
      continue;
    else {
      dfs(n, table, v);
    }
  }

  table[idx] = orig;
}

int main() {
  cin >> N;
  REP(i, N) {
    ll a;
    cin >> a;
    A[i] = a;
  }

  REP(i, N - 1) {
    ll u, v;
    cin >> u >> v;
    u--, v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }

  vector<ll> table(N, INF);
  dfs(0, table, -1);

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPi(i, a, b) for (int i = int(a); i < int(b); i++)
#define MEMS(a, b) memset(a, b, sizeof(a))
#define mp make_pair
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll MOD = 1e9 + 7;
const ll INF = 1e12;

ll N;
ll A[200100];
vector<ll> G[200100];
ll ans[200100];

void dfs(ll v, vector<ll> &table, ll parant) {
  int idx = lower_bound(table.begin(), table.end(), A[v]) - table.begin();
  if (table[idx] > A[v])
    table[idx] = A[v];

  ll len = lower_bound(table.begin(), table.end(), INF) - table.begin();
  ans[v] = len;

  int first = -1;
  for (auto &&n : G[v]) {
    if (n == parant)
      continue;
    else {
      if (first == -1) {
        first = n;
      } else {
        vector<ll> tmp(table);
        dfs(n, tmp, v);
      }
    }
  }

  if (first != -1)
    dfs(first, table, v);
}

int main() {
  cin >> N;
  REP(i, N) {
    ll a;
    cin >> a;
    A[i] = a;
  }

  REP(i, N - 1) {
    ll u, v;
    cin >> u >> v;
    u--, v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }

  vector<ll> table(N, INF);
  dfs(0, table, -1);

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

#pragma GCC optimize("Ofast")
#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
int n, num[200005], ans[200005];
vector<int> g[200005], dp(200005, 2147483647);
void DFS(int v, int p) {
  auto x = lower_bound(dp.begin(), dp.begin() + n, num[v]);
  int y = *x;
  *x = num[v];
  ans[v] = lower_bound(dp.begin(), dp.begin() + n, 2147483647) - dp.begin();
  for (auto z : g[v])
    if (z != p)
      DFS(z, v);
  *x = y;
}
int main() {
  int i, from, to;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d", &num[i]);
  for (i = 0; i < n - 1; i++) {
    scanf("%d%d", &from, &to);
    g[from].emplace_back(to);
    g[to].emplace_back(from);
  }
  DFS(1, -1);
  for (i = 1; i <= n; i++)
    printf("%d\\n", ans[i]);
}

Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
int n, num[200005], ans[200005];
vector<int> g[200005], dp(200005, 2147483647);
void DFS(int v, int p, vector<int> dp) {
  auto x = lower_bound(dp.begin(), dp.begin() + n, num[v]);
  int y = *x;
  *x = num[v];
  ans[v] = lower_bound(dp.begin(), dp.begin() + n, 2147483647) - dp.begin();
  for (auto z : g[v])
    if (z != p)
      DFS(z, v, dp);
  *x = y;
}
int main() {
  int i, from, to;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d", &num[i]);
  for (i = 0; i < n - 1; i++) {
    scanf("%d%d", &from, &to);
    g[from].emplace_back(to);
    g[to].emplace_back(from);
  }
  DFS(1, -1, dp);
  for (i = 1; i <= n; i++)
    printf("%d\\n", ans[i]);
}