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

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

#define pii pair<int, int>
#define pll pair<ll, ll>
#define eb emplace_back
#define ll long long
#define nl '\\n'
#define in()                                                                   \\
  ({                                                                           \\
    int a;                                                                     \\
    scanf("%d", &a);                                                           \\
    a;                                                                         \\
  })

const int N = 3e5 + 9;
const int mod = 1e9 + 7;
// int dx[]={1,0,-1,0,1,-1,-1,1};
// int dy[]={0,1,0,-1,1,1,-1,-1};
vector<int> graph[10003];
int vis[10003], ans[10003];
int dist[10003][10003];
void bfs(int src) {

  int i;
  deque<int> q;
  q.eb(src);
  vis[src] = 1;
  dist[src][src] = 0;
  while (!q.empty()) {
    int ma_src = q.front();
    q.pop_front();
    for (i = 0; i < graph[ma_src].size(); i++) {
      int adj = graph[ma_src][i];
      // cout << ma_src << " " << adj << nl;
      if (!vis[adj]) {
        dist[src][adj] = min(dist[src][adj], 1 + dist[src][ma_src]);
        dist[adj][src] = min(dist[adj][src], 1 + dist[ma_src][src]);
        // cout << dist[src][adj] << " " << dist[adj][src] << nl;
        vis[adj] = 1;
        q.eb(adj);
      }
    }
  }
}
int main() {
  int n, x, y, i, j;
  cin >> n >> x >> y;
  for (i = 1; i < n; i++) {
    graph[i].eb(i + 1);
    graph[i + 1].eb(i);
  }
  graph[x].eb(y);
  graph[y].eb(x);
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= n; j++) {
      dist[i][j] = 9999999;
    }
  }
  for (i = 1; i <= n; i++) {
    bfs(i);
    memset(vis, 0, sizeof(vis));
  }
  // cout << "HEint" << nl;
  for (i = 0; i <= n; i++) {
    for (j = i + 1; j <= n; j++) {
      if (dist[i][j] != 9999999) {
        ans[dist[i][j]]++;
      }
    }
  }
  for (int k = 1; k < n; k++) {
    cout << ans[k] << nl;
  }
}

Error: TLE

Fixed submission:

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

#define pii pair<int, int>
#define pll pair<ll, ll>
#define eb emplace_back
#define ll long long
#define nl '\\n'
#define in()                                                                   \\
  ({                                                                           \\
    int a;                                                                     \\
    scanf("%d", &a);                                                           \\
    a;                                                                         \\
  })

const int N = 3e5 + 9;
const int mod = 1e9 + 7;
// int dx[]={1,0,-1,0,1,-1,-1,1};
// int dy[]={0,1,0,-1,1,1,-1,-1};
vector<ll> graph[10003];
ll dist[10003][10003];
void bfs(ll src, ll n) {

  int i;
  vector<bool> vis(n, false);
  deque<ll> q;
  q.eb(src);
  vis[src] = 1;
  dist[src][src] = 0;
  while (!q.empty()) {
    ll ma_src = q.front();
    q.pop_front();
    for (i = 0; i < graph[ma_src].size(); i++) {
      ll adj = graph[ma_src][i];
      // cout << ma_src << " " << adj << nl;
      if (!vis[adj]) {
        dist[src][adj] = min(dist[src][adj], 1 + dist[src][ma_src]);
        dist[adj][src] = min(dist[adj][src], 1 + dist[ma_src][src]);
        // cout << dist[src][adj] << " " << dist[adj][src] << nl;
        vis[adj] = 1;
        q.eb(adj);
      }
    }
  }
}
int main() {
  ll n, x, y, i, j;
  cin >> n >> x >> y;
  for (i = 1; i < n; i++) {
    graph[i].eb(i + 1);
    graph[i + 1].eb(i);
  }
  graph[x].eb(y);
  graph[y].eb(x);
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= n; j++) {
      dist[i][j] = 99999999999;
    }
  }
  for (i = 1; i <= n; i++) {
    bfs(i, n);
    // cout << "HELL" << nl;
  }
  for (int k = 1; k < n; k++) {
    int cnt = 0;
    for (i = 0; i <= n; i++) {
      for (j = i + 1; j <= n; j++) {
        if (k == dist[i][j]) {
          cnt++;
        }
      }
    }
    cout << cnt << nl;
  }
}