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

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

typedef pair<int, int> ii;

const int N = 2e3 + 5;
const int oo = 1e9;

int n;
int res[N] = {0}, dist[N];
vector<int> AdjList[N];

void bfs(int s) {
  // cout<<"s:"<<s<<endl;
  for (int i = 0; i < n; ++i)
    dist[i] = oo;
  priority_queue<ii, vector<ii>, greater<ii>> pq;
  dist[s] = 0;
  pq.push({0, s});
  while (!pq.empty()) {
    ii nodo = pq.top();
    pq.pop();
    int d = nodo.first, u = nodo.second;
    // if(d > dist[u]) continue;
    res[d]++;
    for (int v : AdjList[u]) {

      if (dist[v] > dist[u] + 1) {
        dist[v] = dist[u] + 1;
        // cout<<u<<"-"<<v<<" "<<dist[v]<<endl;
        pq.push({dist[v], v});
      }
    }
  }
}

int main() {
  int x, y;
  cin >> n >> x >> y;

  for (int i = 0; i < n - 1; ++i) {
    AdjList[i].push_back(i + 1);
    AdjList[i + 1].push_back(i);
  }
  AdjList[x - 1].push_back(y - 1);
  AdjList[y - 1].push_back(x - 1);

  for (int i = 0; i < n; ++i) {
    bfs(i);
  }

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

  return 0;
}


Error: WA

Fixed submission:

// nani?
// omo ta tori da
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#define pi acos(-1);
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define f(i, a, b) for (int i = a; i < b; i++)
#define sor(a) sort(a.begin(), a.end())
#define rsor(a) sort(a.rbegin(), a.rend())
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)
#define inf 1LL << 62
typedef long long ll;
typedef double ld;
using namespace std;
using namespace __gnu_pbds;
// const ll inf=1e10;
const ll MOD = 1e9 + 7;
const ll mod = 998244353;
const ll N = 1e7 + 5;
// ac cmtr;
const int MAX_N = 2e5 + 5;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}

template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
//---------------------------

ll dp[1005][1005];
ll cnt[MAX_N];
ll n;
vector<pair<ll, ll>> adj[MAX_N];

void dij(ll a) {

  set<pair<ll, ll>> s;
  f(i, 0, n + 1) { dp[i][a] = inf; }
  s.insert(mp(0, a));
  dp[a][a] = 0;
  // ans[a] = 1;
  while (!s.empty()) {
    ll ini = s.begin()->sc;
    s.erase(s.begin());
    for (auto xd : adj[ini]) {
      if (dp[xd.fs][a] > dp[ini][a] + xd.sc) {
        s.erase(mp(dp[xd.fs][a], xd.fs));
        // ans[xd.fs] = ans[ini];
        dp[xd.fs][a] = dp[ini][a] + xd.sc;
        s.insert(mp(dp[xd.fs][a], xd.fs));
      }
    }
  }
  return;
}

int main() {
  fastio;
  ll x, y;
  cin >> n >> x >> y;
  f(i, 1, n) {
    adj[i].pb({i + 1, 1});
    adj[i + 1].pb({i, 1});
  }
  adj[x].pb({y, 1});
  adj[y].pb({x, 1});
  f(i, 1, n + 1) { dij(i); }
  f(i, 1, n + 1) {
    for (int j = 1; j <= n; j++) {
      cnt[dp[i][j]]++;
    }
  }
  f(i, 1, n) { cout << cnt[i] / 2 << " " << endl; }
}