Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mod 1000000007
#define inf 100000000000000001
#define PI 3.14159265
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
string char_to_str(char c) {
  string tem(1, c);
  return tem;
}
typedef pair<long long, long long> ii;
#define S second
#define F first
ll max(ll a, ll b) {
  if (a > b) {
    return a;
  }
  return b;
}
ll min(ll a, ll b) {
  if (a < b) {
    return a;
  }
  return b;
}
#define MAXN 200005
// string to integer stoi() Remember: it takes string not character
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();

// To compile--> g++ -std=c++0x -o output one.cpp
// To run--> ./output

// string to integer stoi() Remember: it takes string not character
// string to long long stol()
// string.substr(position,length);
// integer to string to_string();

// To compile--> g++ -std=c++0x -o output one.cpp
// To run--> ./output
ll n;
ll a[200001];
vector<ll> adj[200001];
bool vis[200001];
ll ans[200001];
vector<ll> v1;

void dfs(ll u) {

  ll fg = 1;
  ll po = 0;
  ll ori = 0;
  auto it = lower_bound(v1.begin(), v1.end(), a[u]);
  if (it == v1.end()) {
    fg = 1;
    v1.push_back(a[u]);
  } else {
    fg = 0;
    ll pos = it - v1.begin();
    po = pos;
    ori = v1[pos];
    v1[pos] = a[u];
  }
  ans[u] = v1.size();

  for (auto i = adj[u].begin(); i != adj[u].end(); ++i) {
    if (!vis[*i]) {
      vis[*i] = true;
      dfs(*i);
    }
  }

  if (fg == 1) {
    v1.pop_back();
  } else {
    v1[po] = ori;
  }
}

int main() {
  fastio;
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    vis[i] = false;
    ans[i] = 0;
  }
  for (int i = 0; i < n - 1; ++i) {
    ll a1, b1;
    cin >> a1 >> b1;
    adj[a1].push_back(b1);
    adj[b1].push_back(a1);
  }

  ans[1] = 1;
  vis[1] = true;
  dfs(1);

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

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mod 1000000007
#define inf 100000000000000001
#define PI 3.14159265
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
string char_to_str(char c) {
  string tem(1, c);
  return tem;
}
typedef pair<long long, long long> ii;
#define S second
#define F first
ll max(ll a, ll b) {
  if (a > b) {
    return a;
  }
  return b;
}
ll min(ll a, ll b) {
  if (a < b) {
    return a;
  }
  return b;
}
#define MAXN 200005
// string to integer stoi() Remember: it takes string not character
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();

// To compile--> g++ -std=c++0x -o output one.cpp
// To run--> ./output

// string to integer stoi() Remember: it takes string not character
// string to long long stol()
// string.substr(position,length);
// integer to string to_string();

// To compile--> g++ -std=c++0x -o output one.cpp
// To run--> ./output
ll n;
ll a[200001];
vector<ll> adj[200001];
bool vis[200001];
ll ans[200001];
vector<ll> v1;

void dfs(ll u) {

  ll fg = 1;
  ll po = 0;
  ll ori = 0;
  auto it = lower_bound(v1.begin(), v1.end(), a[u]);
  if (it == v1.end()) {
    fg = 1;
    v1.push_back(a[u]);
  } else {
    fg = 0;
    ll pos = it - v1.begin();
    po = pos;
    ori = v1[u];
    v1[pos] = a[u];
  }
  ans[u] = v1.size();

  for (auto i = adj[u].begin(); i != adj[u].end(); ++i) {
    if (!vis[*i]) {
      vis[*i] = true;
      dfs(*i);
    }
  }

  if (fg == 1) {
    if (!v1.empty()) {
      v1.pop_back();
    }
  } else {
    v1[po] = ori;
  }
}

int main() {
  fastio;
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    vis[i] = false;
    ans[i] = 0;
  }
  for (int i = 0; i < n - 1; ++i) {
    ll a1, b1;
    cin >> a1 >> b1;
    adj[a1].push_back(b1);
    adj[b1].push_back(a1);
  }

  ans[1] = 1;
  vis[1] = true;
  dfs(1);

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

  return 0;
}