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

#include "bits/stdc++.h"
#include <set>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(i) (i).begin(), (i).end()
#define rall(i) (i).begin(), (i).end(), greater<int>()
#define int long long
#define elif else if
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fst first
#define sec second
#define mod 1000000007
template <typename T>

void print(T x) {
  std::cout << x << " ";
}
template <typename T>

void println(T x) {
  std::cout << x << "\\n";
}

using Graph = vector<vector<int>>;

signed main(void) {
  int n, x, y;
  cin >> n >> x >> y;
  x--;
  y--;
  vector<int> ans(n, 0);

  Graph G(n);
  rep(j, n) {
    if (j != n - 1)
      G[j].pb(j + 1);
    if (j != 0)
      G[j].pb(j - 1);
    if (j == x)
      G[j].pb(y);
    if (j == y)
      G[j].pb(x);
  }

  rep(i, n) { // 始点を0からn-2までずらす
    // cout << "i= " << i << endl;
    vector<int> dist(n, -1);
    queue<int> que;
    dist[i] = 0;
    que.push(i);
    while (!que.empty()) {
      int v = que.front();
      // cout << " v= " << v << endl;
      que.pop();
      for (int nv : G[v]) {
        if (dist[nv] != -1)
          continue;
        // if (nv < v)continue;
        // cout << " nv=" << nv << "v=" << v << endl;
        dist[nv] = dist[v] + 1;
        que.push(nv);
      }
    }
    // println(i);
    // for (auto a : dist)cout << a;
    // println("");
    rep(i, n) { ans[dist[i]]++; }
  }
  rep(i, n) ans[i] /= 2;
  // for (auto a : ans)cout<<a<<endl;
  rep1(i, n) println(ans[i]);
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <set>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(i) (i).begin(), (i).end()
#define rall(i) (i).begin(), (i).end(), greater<int>()
#define int long long
#define elif else if
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fst first
#define sec second
#define mod 1000000007
template <typename T>

void print(T x) {
  std::cout << x << " ";
}
template <typename T>

void println(T x) {
  std::cout << x << "\\n";
}

using Graph = vector<vector<int>>;

signed main(void) {
  int n, x, y;
  cin >> n >> x >> y;
  x--;
  y--;
  vector<int> ans(n, 0);

  Graph G(n);
  rep(i, n) { // 始点を0からn-1までずらす
    rep(j, n) {
      if (j != n - 1)
        G[j].pb(j + 1);
      if (j != 0)
        G[j].pb(j - 1);
      if (j == x)
        G[j].pb(y);
      if (j == y)
        G[j].pb(x);
    }
    vector<int> dist(n, -1);
    queue<int> que;
    dist[i] = 0;
    que.push(i);
    while (!que.empty()) {
      int v = que.front();
      que.pop();

      for (int nv : G[v]) {
        if (dist[nv] != -1)
          continue;
        dist[nv] = dist[v] + 1;
        que.push(nv);
      }
    }
    // println(i);
    // for (auto a : dist)cout << a;
    // println("");
    rep(i, n) { ans[dist[i]]++; }
  }
  rep(i, n) ans[i] /= 2;
  // for (auto a : ans)cout<<a<<endl;
  rep1(i, n) println(ans[i]);
}