Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
typedef long long int ll;

#define PI (acos(-1))
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)

#define dump(x) cerr << #x << " = " << (x) << endl;
#define dumparr(x)                                                             \\
  cerr << #x;                                                                  \\
  for (auto elem : x) {                                                        \\
    cerr << " " << elem;                                                       \\
  }                                                                            \\
  cerr << endl;
#define dumppairs(x)                                                           \\
  cerr << #x;                                                                  \\
  for (auto elem : x) {                                                        \\
    cerr << "(" << elem.first << " " << elem.second << ") ";                   \\
  }                                                                            \\
  cerr << endl;
#define dumpline cerr << "**********************" << endl;
#define dumpwline(x)                                                           \\
  cerr << "******** " << #x << "=" << (x) << " ********" << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))

int main(void) {
  ll n;
  cin >> n;
  ll x;
  cin >> x;
  ll y;
  cin >> y;
  vector<vector<ll>> G(n, vector<ll>());
  REP(i, n - 1) {
    G[i].push_back(i + 1);
    G[i + 1].push_back(i);
  }
  G[x - 1].push_back(y - 1);
  G[y - 1].push_back(x - 1);
  vector<ll> cnt(n, 0);
  REP(i, n) {
    vector<ll> distance(n, -1);
    queue<ll> q;
    q.push(i);
    distance[i] = 0;
    while (!q.empty()) {
      ll now = q.front();
      q.pop();
      for (auto next : G[now]) {
        if (distance[next] != -1) {
          continue;
        }
        distance[next] = distance[now] + 1;
        cnt[distance[next]]++;
        q.push(next);
      }
    }
  }
  REP(i, n - 1) { cout << cnt[i + 1] / 2 << endl; }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
typedef long long int ll;

#define PI (acos(-1))
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)

#define dump(x) cerr << #x << " = " << (x) << endl;
#define dumparr(x)                                                             \\
  cerr << #x;                                                                  \\
  for (auto elem : x) {                                                        \\
    cerr << " " << elem;                                                       \\
  }                                                                            \\
  cerr << endl;
#define dumppairs(x)                                                           \\
  cerr << #x;                                                                  \\
  for (auto elem : x) {                                                        \\
    cerr << "(" << elem.first << " " << elem.second << ") ";                   \\
  }                                                                            \\
  cerr << endl;
#define dumpline cerr << "**********************" << endl;
#define dumpwline(x)                                                           \\
  cerr << "******** " << #x << "=" << (x) << " ********" << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))

int main(void) {
  ll n;
  cin >> n;
  ll x;
  cin >> x;
  ll y;
  cin >> y;
  vector<vector<ll>> G(n, vector<ll>(n, 1e9));
  REP(i, n) { G[i][i] = 0; }
  REP(i, n - 1) { G[i][i + 1] = 1; }
  REP(i, n - 1) { G[i + 1][i] = 1; }
  G[x - 1][y - 1] = 1;
  G[y - 1][x - 1] = 1;
  REP(i, n) {
    REP(j, n) {
      REP(k, n) {
        if (G[i][j] + G[j][k] < G[i][k]) {
          G[i][k] = G[i][j] + G[j][k];
          G[k][i] = G[i][j] + G[j][k];
        }
      }
    }
  }
  vector<ll> cnt(n, 0);
  REP(i, n) {
    REP(j, n) { cnt[G[i][j]]++; }
  }
  REP(i, n - 1) { cout << cnt[i + 1] / 2 << endl; }
  return 0;
}