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

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repk(i, k, n) for (int i = k; i < n; i++)
#define MOD 1e9 + 7
#define INF 1e9
#define PIE 3.14159265358979323

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> T GCD(T a, T b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}
template <class T> inline T LCM(T a, T b) { return (a * b) / GCD(a, b); }

int main() {
  int n, x, y;
  cin >> n >> x >> y;
  x--;
  y--;
  vector<vector<int>> dist(n, vector<int>(n, -1));
  for (int i = 0; i < n; i++) {
    queue<int> q;
    q.push(i);
    dist[i][i] = 0;
    while (!q.empty()) {
      int a = q.front();
      q.pop();
      if (a != n - 1 && dist[i][a + 1] == -1) {
        q.push(a + 1);
        dist[i][a + 1] = dist[i][a] + 1;
      }
      if (a != 0 && dist[i][a - 1] == -1) {
        q.push(a - 1);
        dist[i][a - 1] = dist[i][a] + 1;
      }
      if (a == x && dist[i][y] == -1) {
        q.push(y);
        dist[i][y] = dist[i][a] + 1;
      }
      if (a == y && dist[i][x] == -1) {
        q.push(x);
        dist[i][x] = dist[i][a] + 1;
      }
    }
  }
  vector<int> result(n, 0);
  rep(i, n) {
    rep(j, n) { result[dist[i][j]]++; }
  }
  repk(i, 1, n) cout << result[i] / 2 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repk(i, k, n) for (int i = k; i < n; i++)
#define MOD 1e9 + 7
#define INF 1e9
#define PIE 3.14159265358979323

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> T GCD(T a, T b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}
template <class T> inline T LCM(T a, T b) { return (a * b) / GCD(a, b); }

int main() {
  int n, x, y;
  cin >> n >> x >> y;
  x--;
  y--;
  for (int k = 1; k < n; k++) {
    int count = 0;
    for (int i = 0; i < n; i++) {
      queue<int> q;
      vector<int> dist(n, -1);
      q.push(i);
      dist[i] = 0;
      while (!q.empty()) {
        int a = q.front();
        q.pop();
        if (a != n - 1 && dist[a + 1] == -1) {
          q.push(a + 1);
          dist[a + 1] = dist[a] + 1;
          if (dist[a + 1] == k)
            count++;
        }
        if (a != 0 && dist[a - 1] == -1) {
          q.push(a - 1);
          dist[a - 1] = dist[a] + 1;
          if (dist[a - 1] == k)
            count++;
        }
        if (a == x && dist[y] == -1) {
          q.push(y);
          dist[y] = dist[a] + 1;
          if (dist[y] == k)
            count++;
        }
        if (a == y && dist[x] == -1) {
          q.push(x);
          dist[x] = dist[a] + 1;
          if (dist[x] == k)
            count++;
        }
      }
    }
    count /= 2;
    cout << count << endl;
  }
  return 0;
}