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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define FORS(i, s, n) for (int i = (s), i##_len = (n); i <= i##_len; ++i)
#define VFOR(i, s, n) for (int i = (s); i < (n); ++i)
#define VFORS(i, s, n) for (int i = (s); i <= (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FORS(i, 0, n)
#define VREP(i, n) VFOR(i, 0, n)
#define VREPS(i, n) VFORS(i, 0, n)
#define RFOR(i, s, n) for (int i = (s), i##_len = (n); i >= i##_len; --i)
#define RFORS(i, s, n) for (int i = (s), i##_len = (n); i > i##_len; --i)
#define RREP(i, n) RFOR(i, n, 0)
#define RREPS(i, n) RFORS(i, n, 0)
#define IREP(i, v) for (auto i = (v).begin(); i != (v).end(); ++i)
#define IRREP(i, v) for (auto i = (v).rbegin(); i != (v).rend(); ++i)
#define ALL(v) (v).begin(), (v).end()
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(ALL(v), greater<decltype(v[0])>())
#define SZ(x) ((int)(x).size())
#define REV(x) reverse(ALL(x))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end())

using ld = long double;
using ll = long long;
using ui = unsigned int;
using ull = unsigned long long;
using Pi_i = pair<int, int>;
using Pll_ll = pair<ll, ll>;
using VB = vector<bool>;
using VC = vector<char>;
using VD = vector<double>;
using VI = vector<int>;
using VLL = vector<ll>;
using VS = vector<string>;
using VSH = vector<short>;
using VULL = vector<ull>;

const int MOD = 1000000007;
const int INF = 1000000000; // 1e9
const int NIL = -1;
const ll LINF = 1000000000000000000; // 1e18
const double EPS = 1E-10;

template <class T, class S> bool chmax(T &a, const S &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class S> bool chmin(T &a, const S &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

struct edge {
  int to, cost;
  edge(int To, int Cost) : to(To), cost(Cost){};
};

int V; // 頂点数
vector<vector<edge>> G;
vector<vector<int>> d; // sからの距離

void dijkstra(int s) {
  // Pi_i first: 距離　second: 頂点
  priority_queue<Pi_i, vector<Pi_i>, greater<Pi_i>> que;
  d[s].resize(V, INF);
  d[s][s] = 0;
  que.push(MP(0, s));

  while (!que.empty()) {
    Pi_i p = que.top();
    que.pop();
    int v = p.second;
    if (d[s][v] < p.first)
      continue;
    for (edge &e : G[v]) {
      if (d[s][e.to] > d[s][v] + e.cost) {
        d[s][e.to] = d[s][v] + e.cost;
        que.push(MP(d[s][e.to], e.to));
      }
    }
  }
}

int main() {
  cin >> V;
  d.resize(V);
  G.resize(V);
  REP(i, V - 1) {
    G[i].push_back(edge(i + 1, 1));
    G[i + 1].push_back(edge(i, 1));
  }
  int X, Y;
  cin >> X >> Y;
  --X;
  --Y;
  G[X].push_back(edge(Y, 1));
  G[Y].push_back(edge(X, 1));
  REP(i, V)
  dijkstra(i);
  VI cnt(V, 0);
  REP(i, V) {
    FOR(j, i + 1, V)
    ++cnt[d[i][j]];
  }
  FOR(i, 1, V)
  cout << cnt[i] << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define FORS(i, s, n) for (int i = (s), i##_len = (n); i <= i##_len; ++i)
#define VFOR(i, s, n) for (int i = (s); i < (n); ++i)
#define VFORS(i, s, n) for (int i = (s); i <= (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FORS(i, 0, n)
#define VREP(i, n) VFOR(i, 0, n)
#define VREPS(i, n) VFORS(i, 0, n)
#define RFOR(i, s, n) for (int i = (s), i##_len = (n); i >= i##_len; --i)
#define RFORS(i, s, n) for (int i = (s), i##_len = (n); i > i##_len; --i)
#define RREP(i, n) RFOR(i, n, 0)
#define RREPS(i, n) RFORS(i, n, 0)
#define IREP(i, v) for (auto i = (v).begin(); i != (v).end(); ++i)
#define IRREP(i, v) for (auto i = (v).rbegin(); i != (v).rend(); ++i)
#define ALL(v) (v).begin(), (v).end()
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(ALL(v), greater<decltype(v[0])>())
#define SZ(x) ((int)(x).size())
#define REV(x) reverse(ALL(x))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end())

using ld = long double;
using ll = long long;
using ui = unsigned int;
using ull = unsigned long long;
using Pi_i = pair<int, int>;
using Pll_ll = pair<ll, ll>;
using VB = vector<bool>;
using VC = vector<char>;
using VD = vector<double>;
using VI = vector<int>;
using VLL = vector<ll>;
using VS = vector<string>;
using VSH = vector<short>;
using VULL = vector<ull>;

const int MOD = 1000000007;
const int INF = 1000000000; // 1e9
const int NIL = -1;
const ll LINF = 1000000000000000000; // 1e18
const double EPS = 1E-10;

template <class T, class S> bool chmax(T &a, const S &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class S> bool chmin(T &a, const S &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int V; // 頂点数
vector<vector<int>>
    d; // d[i][j] iからjまでのコスト 最初辺がなければINF 同じ点では0

void warshallFloyd() {
  REP(k, V) REP(i, V) REP(j, V) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}

int main() {
  cin >> V;
  d.resize(V);
  REP(i, V) {
    d[i].resize(V, INF);
    d[i][i] = 0;
    if (i) {
      d[i][i - 1] = d[i - 1][i] = 1;
    }
  }
  int X, Y;
  cin >> X >> Y;
  --X;
  --Y;
  d[X][Y] = d[Y][X] = 1;
  warshallFloyd();
  VI cnt(V, 0);
  REP(i, V) {
    FOR(j, i + 1, V)
    ++cnt[d[i][j]];
  }
  FOR(i, 1, V)
  cout << cnt[i] << "\\n";
  return 0;
}