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

#include <bits/stdc++.h>

using namespace std;

#define S scanf
#define P printf
#define G getline
#define SZ size()
#define C clear()
#define B begin()
#define F front()
#define T top()
#define E end()
#define EM empty()
#define V vector
#define Q queue
#define DQ deque
#define PQ priority_queue
#define ST stack
#define FI first
#define SE second
#define PI acos(-1)
#define PS push
#define PP pop()
#define PSF push_front
#define PSB push_back
#define PPF pop_front()
#define PPB pop_back()
#define MP make_pair
#define LL long long int
#define ULL unsigned long long int
#define PII pair<int, int>
#define PSI pair<string, int>
#define PIS pair<int, string>
#define PLI pair<long long int, int>
#define PLL pair<long long int, long long int>
#define MII map<int, int>
#define MSI map<string, int>
#define MIS map<int, string>
#define MLI map<long long int, int>
#define FAST()                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)

// int dx[]={-1,1,0,0};
// int dy[]={0,0,-1,1};
// int dx[]= {-1,0,1,-1,1,-1,0,1};
// int dy[]= {1,1,1,0,0,-1,-1,-1};

int cnt[2005];
int dist[2005];
V<int> adj[2005];
PQ<PII, V<PII>, greater<PII>> pq;

void runBFS() {
  int x, y;
  while (!pq.EM) {
    x = pq.T.SE;
    y = pq.T.FI;
    pq.PP;
    for (int bal : adj[x]) {
      if (dist[bal] > y + 1) {
        dist[bal] = y + 1;
        pq.PS({y + 1, bal});
      }
    }
  }
}

int main() {
  FAST();

  //    freopen("input.txt","r",stdin);
  //    freopen("output.txt","w",stdout);

  int i, j, k, n, x, y;
  cin >> n >> x >> y;
  for (i = 2; i <= n; i++) {
    adj[i - 1].emplace_back(i);
    adj[i].emplace_back(i - 1);
  }
  adj[x].emplace_back(y);
  adj[y].emplace_back(x);
  runBFS();
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++)
      dist[j] = INT_MAX;
    dist[i] = 0;
    pq.PS({0, i});
    runBFS();
    for (j = 1; j <= n; j++)
      cnt[dist[j]]++;
  }
  for (i = 1; i < n; i++)
    cout << cnt[i] / 2 << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define S scanf
#define P printf
#define G getline
#define SZ size()
#define C clear()
#define B begin()
#define F front()
#define T top()
#define E end()
#define EM empty()
#define V vector
#define Q queue
#define DQ deque
#define PQ priority_queue
#define ST stack
#define FI first
#define SE second
#define PI acos(-1)
#define PS push
#define PP pop()
#define PSF push_front
#define PSB push_back
#define PPF pop_front()
#define PPB pop_back()
#define MP make_pair
#define LL long long int
#define ULL unsigned long long int
#define PII pair<int, int>
#define PSI pair<string, int>
#define PIS pair<int, string>
#define PLI pair<long long int, int>
#define PLL pair<long long int, long long int>
#define MII map<int, int>
#define MSI map<string, int>
#define MIS map<int, string>
#define MLI map<long long int, int>
#define FAST()                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)

// int dx[]={-1,1,0,0};
// int dy[]={0,0,-1,1};
// int dx[]= {-1,0,1,-1,1,-1,0,1};
// int dy[]= {1,1,1,0,0,-1,-1,-1};

int cnt[2005];
LL dist[2005][2005];

int main() {
  FAST();

  //    freopen("input.txt","r",stdin);
  //    freopen("output.txt","w",stdout);

  int i, j, k, n, x, y;
  cin >> n >> x >> y;
  for (i = 1; i <= n; i++) {
    dist[i][i + 1] = dist[i + 1][i] = 1;
    for (j = i + 2; j <= n; j++)
      dist[i][j] = dist[j][i] = INT_MAX;
  }
  dist[x][y] = dist[y][x] = 1;
  for (k = 1; k <= n; k++) {
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= n; j++) {
        if (dist[i][k] + dist[k][j] < dist[i][j])
          dist[i][j] = dist[i][k] + dist[k][j];
      }
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      if (dist[i][j] < n)
        cnt[dist[i][j]]++;
    }
  }
  for (i = 1; i < n; i++)
    cout << cnt[i] / 2 << '\\n';
  return 0;
}