Problem p02726 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <random>
#define all(x) (x).begin(), (x).end()

using namespace std;
void dout() { cerr << endl; }
typedef long long ll;
template <typename Head, typename... Tail> void dout(Head H, Tail... T) {
  cerr << H << ' ';
  dout(T...);
}

#define debug(x) cout << #x " = " << (x) << endl

using ll = long long;
using pii = pair<ll, ll>;
using ld = long double;
using pll = pair<long long, long long>;

template <typename T> void do_uniq(vector<T> vec) {
  sort(all(vec));
  vec.resize(unique(all(vec)) - vec.begin());
}

// #define solvsh
// #define multi
#ifdef solvsh

void solve() {}

#else

const int MAXN = 1e3 + 9;

vector<int> g[MAXN];

int answr[MAXN];

int d[MAXN];
int usd[MAXN];
int n;

void f(int v) {
  for (int i = 1; i <= n; ++i) {
    d[i] = n + 2;
    usd[i] = false;
  }

  queue<int> q;
  q.push(v);
  d[v] = 0;
  usd[v] = true;

  while (!q.empty()) {
    auto h = q.front();
    q.pop();
    for (auto e : g[h]) {
      d[e] = min(d[e], d[h] + 1);

      if (!usd[e]) {
        q.push(e);
        usd[e] = true;
      }
    }
  }

  for (int i = 1; i <= n; ++i) {
    answr[d[i]]++;
  }
}

void solve() {
  cin >> n;

  int x, y;
  cin >> x >> y;
  g[x].push_back(y);
  g[y].push_back(x);

  for (int i = 1; i < n; ++i) {
    g[i].push_back(i + 1);
    g[i + 1].push_back(i);
  }

  for (int i = 1; i <= n; ++i) {
    f(i);
  }

  for (int i = 1; i < n; ++i) {
    cout << answr[i] / 2 << "\n";
  }
}

#endif

void multisolve() {
  int t;
  cin >> t;

  int i = 1;
  while (t--) {
    //        cout << "Case #" << (i++)  << ": ";
    solve();
    //        cout << "\n";
  }
}

#define int int
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  cout << fixed << setprecision(20);

#ifdef multi
  multisolve();
#else
  solve();
//    gen();
#endif

#ifdef LOCAL
  // cout << '\n' << (double)clock() / CLOCKS_PER_SEC << '\n';
#endif
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <random>
#define all(x) (x).begin(), (x).end()

using namespace std;
void dout() { cerr << endl; }
typedef long long ll;
template <typename Head, typename... Tail> void dout(Head H, Tail... T) {
  cerr << H << ' ';
  dout(T...);
}

#define debug(x) cout << #x " = " << (x) << endl

using ll = long long;
using pii = pair<ll, ll>;
using ld = long double;
using pll = pair<long long, long long>;

template <typename T> void do_uniq(vector<T> vec) {
  sort(all(vec));
  vec.resize(unique(all(vec)) - vec.begin());
}

// #define solvsh
// #define multi
#ifdef solvsh

void solve() {}

#else

const int MAXN = 1e4 + 9;

vector<int> g[MAXN];

int answr[MAXN];

int d[MAXN];
int usd[MAXN];
int n;

void f(int v) {
  for (int i = 1; i <= n; ++i) {
    d[i] = n + 2;
    usd[i] = false;
  }

  queue<int> q;
  q.push(v);
  d[v] = 0;
  usd[v] = true;

  while (!q.empty()) {
    auto h = q.front();
    q.pop();
    for (auto e : g[h]) {
      d[e] = min(d[e], d[h] + 1);

      if (!usd[e]) {
        q.push(e);
        usd[e] = true;
      }
    }
  }

  for (int i = 1; i <= n; ++i) {
    answr[d[i]]++;
  }
}

void solve() {
  cin >> n;

  int x, y;
  cin >> x >> y;
  g[x].push_back(y);
  g[y].push_back(x);

  for (int i = 1; i < n; ++i) {
    g[i].push_back(i + 1);
    g[i + 1].push_back(i);
  }

  for (int i = 1; i <= n; ++i) {
    f(i);
  }

  for (int i = 1; i < n; ++i) {
    cout << answr[i] / 2 << "\n";
  }
}

#endif

void multisolve() {
  int t;
  cin >> t;

  int i = 1;
  while (t--) {
    //        cout << "Case #" << (i++)  << ": ";
    solve();
    //        cout << "\n";
  }
}

#define int int
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  cout << fixed << setprecision(20);

#ifdef multi
  multisolve();
#else
  solve();
//    gen();
#endif

#ifdef LOCAL
  // cout << '\n' << (double)clock() / CLOCKS_PER_SEC << '\n';
#endif
}