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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
vector<ll> adj[2005];
ll expo(ll b, ll n, ll m) {
  ll r, p;
  p = 1;
  b = b % m;
  while (n > 0) {
    r = n % 2;
    if (r == 1)
      p = ((p % m) * (b % m)) % m;
    b = ((b % m) * (b % m)) % m;
    n = n / 2;
  }
  return (p % m);
}
ll divide1(ll a, ll b, ll m) {
  ll r, p, n;
  p = a % m;
  n = m - 2;
  while (n > 0) {
    r = n % 2;
    if (r == 1)
      p = ((p % m) * (b % m)) % m;
    b = ((b % m) * (b % m)) % m;
    n = n / 2;
  }
  return (p % m);
}
ll a[3002];
bool visited[2003][2002];
ll ans[2002];
void bfs(ll u) {
  queue<pair<ll, ll>> q;
  q.push(mp(u, 0));
  while (!q.empty()) {
    ll vertex = q.front().ff;
    visited[vertex][u] = 1;
    ll d = q.front().ss;
    ans[d++]++;
    for (auto v : adj[vertex]) {
      if (!visited[v][u]) {
        q.push(mp(v, d));
      }
    }
    while (!q.empty() && visited[q.front().ff][u] == 1) {
      q.pop();
    }
  }
}
int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);
  ll n, x, y;
  cin >> n;
  for (int i = 1; i < n; ++i) {
    adj[i].pb(i + 1);
    adj[i + 1].pb(i);
  }
  cin >> x >> y;
  adj[x].pb(y);
  adj[y].pb(x);
  for (int i = 1; i <= n; ++i) {
    bfs(i);
  }
  for (int i = 1; i < n; ++i) {
    cout << ans[i] / 2 << "\\n";
  }

  return 0;
}


Error: -6
terminate called after throwing an instance of 'St9bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
vector<ll> adj[1000005];
ll expo(ll b, ll n, ll m) {
  ll r, p;
  p = 1;
  b = b % m;
  while (n > 0) {
    r = n % 2;
    if (r == 1)
      p = ((p % m) * (b % m)) % m;
    b = ((b % m) * (b % m)) % m;
    n = n / 2;
  }
  return (p % m);
}
ll divide1(ll a, ll b, ll m) {
  ll r, p, n;
  p = a % m;
  n = m - 2;
  while (n > 0) {
    r = n % 2;
    if (r == 1)
      p = ((p % m) * (b % m)) % m;
    b = ((b % m) * (b % m)) % m;
    n = n / 2;
  }
  return (p % m);
}
ll a[3002];
ll dist[2002][2002];
bool visited[2003][2002];
ll ans[1002];
void bfs(ll u) {
  queue<pair<ll, ll>> q;
  q.push(mp(u, 0));
  while (!q.empty()) {
    ll vertex = q.front().ff;
    visited[vertex][u] = 1;
    ll d = q.front().ss;
    dist[vertex][u] = d;
    ans[d]++;
    for (auto v : adj[vertex]) {
      if (!visited[v]) {
        q.push(mp(d + 1, v));
      }
    }
    while (!q.empty() && visited[q.front().ff][u] == 1) {
      q.pop();
    }
  }
}
int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);
  ll n, x, y;
  for (int i = 1; i < n; ++i) {
    adj[i].pb(i + 1);
    adj[i + 1].pb(i);
  }
  adj[x].pb(y);
  adj[y].pb(x);
  for (int i = 1; i <= n; ++i) {
    bfs(i);
  }
  for (int i = 1; i < n; ++i) {
    cout << ans[i] << " ";
  }
  cout << endl;
  return 0;
}