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

#include <bits/stdc++.h>
using namespace std;

#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl
#define rep(i, n) for (int i = (int)(0); i < (int)(n); i++)
#define repp(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define all(v) v.begin(), v.end()
#define dump(v)                                                                \\
  cerr << #v ":[ ";                                                            \\
  for (auto macro_vi : v) {                                                    \\
    cerr << macro_vi << " ";                                                   \\
  }                                                                            \\
  cerr << "]" << endl;
#define ddump(v)                                                               \\
  cerr << #v ":" << endl;                                                      \\
  for (auto macro_row : v) {                                                   \\
    cerr << "[";                                                               \\
    for (auto macro__vi : macro_row) {                                         \\
      cerr << macro__vi << " ";                                                \\
    }                                                                          \\
    cerr << "]" << endl;                                                       \\
  }
using lint = long long;
const int INF = 1e9;
const lint LINF = 1e18;
const lint MOD = 1e9 + 7;
const double EPS = 1e-10;

struct Graph {
  struct Edge {
    int from;
    int to;
    int cost;
    int index;
    int color;
  };
  struct Node {
    int index;
    bool check;
    int color;
    lint dist;
    std::vector<Edge> edges;
    Node() {
      index = -1;
      check = false;
      color = -1;
      dist = INF;
      std::vector<Edge>(0);
    }
  };

  int numNodes;
  bool directed;
  std::vector<Node> nodes;
  std::vector<Edge> edges;

  int numColor;
  Graph(int N) : numNodes(N), nodes(N) {
    for (int i = 0; i < N; i++)
      nodes[i].index = i;
    numColor = -1;
    directed = false;
  }
  Graph(int N, bool directed) : numNodes(N), nodes(N) {
    for (int i = 0; i < N; i++)
      nodes[i].index = i;
    numColor = -1;
    directed = true;
  }

  void addEdge(int from, int to, int cost) {
    nodes[from].edges.push_back(Edge({from, to, cost}));
    if (!directed)
      nodes[to].edges.push_back(Edge({to, from, cost}));
  }

  void addEdge(int from, int to) {
    nodes[from].edges.push_back(Edge({from, to}));
    if (!directed)
      nodes[to].edges.push_back(Edge({to, from}));
  }

  void bfs(int startIndex) {
    for (auto &node : nodes)
      node.dist = INF;
    nodes[startIndex].dist = 0;
    std::queue<int> que({startIndex});
    while (!que.empty()) {
      int currentIndex = que.front();
      que.pop();
      for (auto edge : nodes[currentIndex].edges) {
        if (nodes[edge.to].dist == INF) {
          nodes[edge.to].dist = nodes[currentIndex].dist + 1;
          que.push(edge.to);
        }
      }
    }
  }

  void coloring() {
    int c = 0;
    for (auto &node : nodes) {
      if (node.color != -1)
        continue;
      node.color = c;
      std::queue<int> que({node.index});
      while (!que.empty()) {
        int currentIndex = que.front();
        for (auto edge : nodes[currentIndex].edges) {
          if (nodes[edge.to].color != -1)
            continue;
          nodes[edge.to].color = c;
          que.push(nodes[edge.to].index);
        }
      }
      c++;
    }
    numColor = c;
  }
};

int main() {
  int N, X, Y;
  cin >> N >> X >> Y;
  X--, Y--;
  Graph g(N);
  rep(i, N - 1) { g.addEdge(i, i + 1); }
  g.addEdge(X, Y);
  //
  vector<lint> cnt(N + 1, 0);

  for (int i = 0; i < N; i++) {
    g.bfs(i);
    for (int j = i + 1; j < N; j++) {
      cnt[g.nodes[j].dist]++;
    }
  }

  for (int k = 1; k < N; k++) {
    cout << cnt[k] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl
#define rep(i, n) for (int i = (int)(0); i < (int)(n); i++)
#define repp(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define all(v) v.begin(), v.end()
#define dump(v)                                                                \\
  cerr << #v ":[ ";                                                            \\
  for (auto macro_vi : v) {                                                    \\
    cerr << macro_vi << " ";                                                   \\
  }                                                                            \\
  cerr << "]" << endl;
#define ddump(v)                                                               \\
  cerr << #v ":" << endl;                                                      \\
  for (auto macro_row : v) {                                                   \\
    cerr << "[";                                                               \\
    for (auto macro__vi : macro_row) {                                         \\
      cerr << macro__vi << " ";                                                \\
    }                                                                          \\
    cerr << "]" << endl;                                                       \\
  }
using lint = long long;
const int INF = 1e9;
const lint LINF = 1e18;
const lint MOD = 1e9 + 7;
const double EPS = 1e-10;

int main() {
  int N, X, Y;
  cin >> N >> X >> Y;
  X--, Y--;
  vector<vector<lint>> dist(N, vector<lint>(N, LINF));
  vector<lint> K(N + 1, 0);
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      dist[i][j] = min(dist[i][j], (long long)j - i);
      dist[i][j] = min(dist[i][j], abs(i - X) + 1ll + abs(j - Y));
      dist[i][j] = min(dist[i][j], abs(i - Y) + 1ll + abs(j - X));
      K[dist[i][j]]++;
    }
  }
  ddump(dist);
  for (int k = 1; k <= N - 1; k++) {
    cout << K[k] << endl;
  }
  return 0;
}