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

// Optimise
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

// #define MULTI_TEST
#ifdef LOCAL
#include "/home/shahraaz/bin/debug.h"
#else
#define db(...)
#define pc(...)
#endif

#define f first
#define s second
#define pb push_back
#define all(v) v.begin(), v.end()
auto TimeStart = chrono::steady_clock::now();
auto seed = TimeStart.time_since_epoch().count();
std::mt19937 rng(seed);
using ll = long long;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T> using Random = std::uniform_int_distribution<T>;

const int NAX = 2e3 + 5, MOD = 1000000007;

vector<int> adj[NAX];
int dist[NAX][NAX];
int ans[NAX + 10];
class Solution {
private:
public:
  Solution() {}
  ~Solution() {}
  void solveCase() {
    int n, x, y;
    cin >> n >> x >> y;
    for (size_t i = 0; i < n; i++) {
      for (size_t j = 0; j < n; j++) {
        dist[i][j] = NAX;
      }
    }
    for (size_t i = 0; i < n; i++)
      dist[i][i] = 0;
    for (size_t i = 0; (i + 1) < n; i++) {
      adj[i].pb(i + 1);
      adj[i + 1].pb(i);
    }
    adj[x - 1].pb(y - 1);
    adj[y - 1].pb(x - 1);
    int V = n;
    for (int k = 0; k < V; k++) {
      queue<int> Q;
      Q.push(k);
      while (!Q.empty()) {
        auto top = Q.front();
        Q.pop();
        for (auto &chil : adj[top]) {
          if (dist[k][chil] == NAX) {
            dist[k][chil] = dist[k][top] + 1;
            Q.push(chil);
          }
        }
      }
    }
    for (size_t i = 0; i < n; i++)
      for (size_t j = i + 1; j < n; j++)
        ans[dist[i][j]]++;
    for (size_t i = 1; i < n; i++)
      cout << ans[i] << '\\n';
  }
};

int32_t main() {
#ifndef LOCAL
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif
  int t = 1;
#ifdef MULTI_TEST
  cin >> t;
#endif
  Solution mySolver;
  for (int i = 1; i <= t; ++i) {
    mySolver.solveCase();
#ifdef TIME
    cerr << "Case #" << i << ": Time "
         << chrono::duration<double>(chrono::steady_clock::now() - TimeStart)
                .count()
         << " s.\\n";
    TimeStart = chrono::steady_clock::now();
#endif
  }
  return 0;
}

Error: TLE

Fixed submission:

// Optimise
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

// #define MULTI_TEST
#ifdef LOCAL
#include "/home/shahraaz/bin/debug.h"
#else
#define db(...)
#define pc(...)
#endif

#define f first
#define s second
#define pb push_back
#define all(v) v.begin(), v.end()
auto TimeStart = chrono::steady_clock::now();
auto seed = TimeStart.time_since_epoch().count();
std::mt19937 rng(seed);
using ll = long long;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T> using Random = std::uniform_int_distribution<T>;

const int NAX = 2e3 + 5, MOD = 1000000007;

vector<int> adj[NAX];
int dist[NAX][NAX];
int ans[NAX + 10];
class Solution {
private:
public:
  Solution() {}
  ~Solution() {}
  void solveCase() {
    int n, x, y;
    cin >> n >> x >> y;
    for (size_t i = 0; i < n; i++) {
      for (size_t j = 0; j < n; j++) {
        dist[i][j] = NAX;
      }
    }
    for (size_t i = 0; i < n; i++)
      dist[i][i] = 0;
    for (size_t i = 0; (i + 1) < n; i++) {
      dist[i][i + 1] = 1;
      dist[i + 1][i] = 1;
      // adj[i].pb(i + 1);
      // adj[i + 1].pb(i);
    }
    dist[x - 1][y - 1] = 1;
    dist[y - 1][x - 1] = 1;
    int V = n;
    for (int k = 0; k < V; k++) {
      // Pick all vertices as source one by one
      for (int i = 0; i < V; i++) {
        // Pick all vertices as destination for the
        // above picked source
        for (int j = 0; j < V; j++) {
          // If vertex k is on the shortest path from
          // i to j, then update the value of dist[i][j]
          if (dist[i][k] + dist[k][j] < dist[i][j])
            dist[i][j] = dist[i][k] + dist[k][j];
        }
      }
    }
    for (size_t i = 0; i < n; i++)
      for (size_t j = i + 1; j < n; j++)
        ans[dist[i][j]]++;
    for (size_t i = 1; i < n; i++)
      cout << ans[i] << '\\n';
  }
};

int32_t main() {
#ifndef LOCAL
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif
  int t = 1;
#ifdef MULTI_TEST
  cin >> t;
#endif
  Solution mySolver;
  for (int i = 1; i <= t; ++i) {
    mySolver.solveCase();
#ifdef TIME
    cerr << "Case #" << i << ": Time "
         << chrono::duration<double>(chrono::steady_clock::now() - TimeStart)
                .count()
         << " s.\\n";
    TimeStart = chrono::steady_clock::now();
#endif
  }
  return 0;
}