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

// Author ::  ABHINAV3010 ::

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define endl "\\n"
#define all(x) (x).begin(), (x).end()
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr)
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
typedef pair<ll, ll> p;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;
ll mod = 1000000007;
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
ll power(ll x, ll y, ll p = mod) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) {
      res = (res * x) % p;
    }
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
void dfs(ll n, vector<vector<ll>> &adj, vector<ll> &ans, vector<bool> &visited,
         multiset<ll> &s, ll src, vector<ll> &v) {
  visited[src] = true;
  ll prev = -1;
  if (s.empty()) {
    s.insert(v[src]);
  } else {
    auto it = s.rbegin();
    if (v[src] > (*it)) {
      s.insert(v[src]);
    } else {
      s.insert(v[src]);
      auto x = s.find(v[src]);
      x++;
      prev = *x;
      s.erase(x);
    }
  }
  ans[src] = s.size();
  for (auto y : adj[src]) {
    if (visited[y] == false) {
      dfs(n, adj, ans, visited, s, y, v);
    }
  }
  s.erase(v[src]);
  if (prev != -1) {
    s.insert(prev);
  }
}
int main() {
  fast;
  ll n;
  cin >> n;
  vector<ll> v;
  v.pb(0);
  for (ll i = 0; i < n; i++) {
    ll a;
    cin >> a;
    v.pb(a);
  }
  vector<vector<ll>> adj(n + 1);
  for (ll i = 0; i < n - 1; i++) {
    ll a, b;
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  vector<ll> ans(n + 1, -1);
  multiset<ll> s;
  vector<bool> visited(n + 1, false);
  dfs(n, adj, ans, visited, s, 1, v);
  for (ll i = 1; i <= n; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

// Author ::  ABHINAV3010 ::

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define endl "\\n"
#define all(x) (x).begin(), (x).end()
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr)
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
typedef pair<ll, ll> p;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;
ll mod = 1000000007;
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
ll power(ll x, ll y, ll p = mod) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) {
      res = (res * x) % p;
    }
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
void dfs(ll n, vector<vector<ll>> &adj, vector<ll> &ans, vector<bool> &visited,
         set<ll> &s, ll src, vector<ll> &v) {
  visited[src] = true;
  ll prev = -1;
  if (s.empty()) {
    s.insert(v[src]);
  } else {
    auto it = s.rbegin();
    if (v[src] > (*it)) {
      s.insert(v[src]);
    } else {
      s.insert(v[src]);
      auto x = s.find(v[src]);
      x++;
      prev = *x;
      s.erase(x);
    }
  }
  ans[src] = s.size();
  for (auto y : adj[src]) {
    if (visited[y] == false) {
      dfs(n, adj, ans, visited, s, y, v);
    }
  }
  s.erase(v[src]);
  if (prev != -1) {
    s.insert(prev);
  }
}
int main() {
  fast;
  ll n;
  cin >> n;
  vector<ll> v;
  v.pb(0);
  for (ll i = 0; i < n; i++) {
    ll a;
    cin >> a;
    v.pb(a);
  }
  vector<vector<ll>> adj(n + 1);
  for (ll i = 0; i < n - 1; i++) {
    ll a, b;
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  vector<ll> ans(n + 1, -1);
  set<ll> s;
  vector<bool> visited(n + 1, false);
  dfs(n, adj, ans, visited, s, 1, v);
  for (ll i = 1; i <= n; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}