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

#include <bits/stdc++.h>
using namespace std;
///////////////////////////////////////////
const long long int INF = 2 * 1e13;
const long long int Mod = 1000000007;
using ll = long long int;
using ci = const int;
using vi = vector<int>;
using Vi = vector<long long int>;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
using matrix = vector<vector<ll>>;
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define all(x) (x).begin(), (x).end()
#define rep(i, N) for (ll i = 0; i < (ll)N; i++)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
template <class T> bool chmax(T &former, const T &b) {
  if (former < b) {
    former = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &former, const T &b) {
  if (b < former) {
    former = b;
    return true;
  }
  return false;
}
template <class T> T sqar(T x) { return x * x; } // sqrt(x)は平方根;
#define Sort(v)                                                                \\
  std::sort(v.begin(), v.end(),                                                \\
            std::greater<decltype(v[0])>()) // 降順でVをソート
template <class T> inline void princ(T x) { cout << x << " "; };
template <class T> inline void print(T x) { cout << x << "\\n"; };
template <class T> inline void Yes(T condition) {
  if (condition)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
template <class T> inline void YES(T condition) {
  if (condition)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
///////////////////////////////////////////////////////////////////////////////////

ll n, x, y;
struct edge {   /*重み付き,有向*/
  int to, cost; /*toは接続先*/
  edge(ll to_, ll cost_ = 1) {
    to = to_;
    cost = cost_;
  }; /*全てのcost=1 <=> 無向グラフ*/
};
bool operator<(edge e1, edge e2) { return e1.cost < e2.cost; }
bool operator>(edge e1, edge e2) { return e1.cost > e2.cost; }
using graph = vector<vector<edge>>;
graph g;
bool ck[5010];
ll dist[5010];
vector<PLL> ans;

void bfs(ll x, ll num) {
  queue<PLL> q;
  rep(i, 5010) ck[i] = false;
  rep(i, 5010) dist[i] = 0;
  q.push(mp(x, num));
  ck[x] = true;
  dist[x] = 0;
  while (!q.empty()) {
    ll now = q.front().first;
    ll cs = q.front().second;
    q.pop();
    dist[now] = cs;
    if (false) {
      ans.pb(mp(x, now));
      continue;
    }
    if (true) {
      rep(i, g[now].size()) {
        if (!ck[g[now][i].to])
          q.push(mp(g[now][i].to, cs + 1));
        ck[g[now][i].to] = true;
      }
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(30);
  cin >> n >> x >> y;
  g.resize(n + 1);
  rep(i, n) {
    if (i == 0)
      continue;
    g[i + 1].pb(edge(i));
    g[i].pb(edge(i + 1));
  }
  g[x].pb(edge(y));
  g[y].pb(edge(x));
  bool ck = false;
  // rep(i,n){
  //     if(i==0)continue;
  //     if(!ck)rep(j,n){
  //         bfs(j+1,i);
  //     }
  //     print(ans.size()/2);
  //     if(ans.size()==0)ck = true;
  //     ans.clear();
  // }
  Vi tp(n + 1);
  rep(i, n) {
    bfs(i + 1, 0);
    rep(i, n) { tp[dist[i + 1]]++; }
  }
  rep(i, n) {
    if (i == 0)
      continue;
    print(tp[i] / 2);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
///////////////////////////////////////////
const long long int INF = 2 * 1e13;
const long long int Mod = 1000000007;
using ll = long long int;
using ci = const int;
using vi = vector<int>;
using Vi = vector<long long int>;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
using matrix = vector<vector<ll>>;
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define all(x) (x).begin(), (x).end()
#define rep(i, N) for (ll i = 0; i < (ll)N; i++)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
template <class T> bool chmax(T &former, const T &b) {
  if (former < b) {
    former = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &former, const T &b) {
  if (b < former) {
    former = b;
    return true;
  }
  return false;
}
template <class T> T sqar(T x) { return x * x; } // sqrt(x)は平方根;
#define Sort(v)                                                                \\
  std::sort(v.begin(), v.end(),                                                \\
            std::greater<decltype(v[0])>()) // 降順でVをソート
template <class T> inline void princ(T x) { cout << x << " "; };
template <class T> inline void print(T x) { cout << x << "\\n"; };
template <class T> inline void Yes(T condition) {
  if (condition)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
template <class T> inline void YES(T condition) {
  if (condition)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
///////////////////////////////////////////////////////////////////////////////////

ll n, x, y;
struct edge {   /*重み付き,有向*/
  int to, cost; /*toは接続先*/
  edge(ll to_, ll cost_ = 1) {
    to = to_;
    cost = cost_;
  }; /*全てのcost=1 <=> 無向グラフ*/
};
bool operator<(edge e1, edge e2) { return e1.cost < e2.cost; }
bool operator>(edge e1, edge e2) { return e1.cost > e2.cost; }
using graph = vector<vector<edge>>;
graph g;
bool ck[5010];
vector<PLL> ans;

void bfs(ll x, ll num) {
  queue<PLL> q;
  rep(i, 5010) ck[i] = false;
  q.push(mp(x, num));
  ck[x] = true;
  while (!q.empty()) {
    ll now = q.front().first;
    ll cs = q.front().second;
    q.pop();
    if (cs == 0) {
      ans.pb(mp(x, now));
      continue;
    }
    if (true) {
      rep(i, g[now].size()) {
        if (!ck[g[now][i].to])
          q.push(mp(g[now][i].to, cs - 1));
        ck[g[now][i].to] = true;
      }
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(30);
  cin >> n >> x >> y;
  g.resize(n + 1);
  rep(i, n) {
    if (i == 0)
      continue;
    g[i + 1].pb(edge(i));
    g[i].pb(edge(i + 1));
  }
  g[x].pb(edge(y));
  g[y].pb(edge(x));
  bool ck = false;
  rep(i, n) {
    if (i == 0)
      continue;
    if (!ck)
      rep(j, n) { bfs(j + 1, i); }
    print(ans.size() / 2);
    if (ans.size() == 0)
      ck = true;
    ans.clear();
  }
  return 0;
}