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

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long int ll;
typedef long double ld;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
typedef pair<ll, ll> pairs;
vector<pairs> p;
bool pairCompare(const pair<double, ll> &firstElof,
                 const pair<double, ll> &secondElof) {
  return firstElof.first < secondElof.first;
}
#define MAX_N 100100
bool x[MAX_N];
ll num[MAX_N];
ll fibl[MAX_N] = {0};
// 四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll fib(ll a) {
  if (fibl[a] != 0)
    return fibl[a];
  if (a == 0) {
    return 0;
  } else if (a == 1) {
    return 1;
  }
  return fibl[a] = fib(a - 1) + fib(a - 2);
}

ll eratosthenes(ll n) {
  int p = 0;
  for (ll i = 0; i <= n; ++i)
    x[i] = true;
  x[0] = x[1] = false;
  for (int i = 2; i <= n; ++i) {
    if (x[i]) {
      p++;
      for (int j = 2 * i; j <= n; j += i)
        x[j] = false;
    }

    num[i] = p;
  }
  return p;
}

ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

ll keta(ll N) {
  int tmp{};
  while (N > 0) {
    tmp += (N % 10);
    N /= 10;
  }
  N = tmp;
  return N;
}

int main() {
  ll n, x, y;
  cin >> n >> x >> y;
  vector<vector<ll>> graph(n);
  graph[0].push_back(1);
  for (ll i = 1; i < n - 1; i++) {
    graph[i].push_back(i - 1);
    graph[i].push_back(i + 1);
  }
  graph[n - 1].push_back(n - 2);
  graph[x - 1].push_back(y - 1);
  graph[y - 1].push_back(x - 1);
  ll visited[n];
  ll ans[n];
  fill(ans, ans + n, 0);
  for (ll i = 0; i < n; i++) {
    fill(visited, visited + n, n + 1);
    visited[i] = 0;
    queue<ll> que;
    que.push(i);
    while (!que.empty()) {
      ll a = que.front();
      que.pop();
      for (ll j = 0; j < graph[a].size(); j++) {
        if (visited[graph[a][j]] > visited[a] + 1) {
          visited[graph[a][j]] = visited[a] + 1;
          que.push(graph[a][j]);
        }
      }
    }
    for (ll j = 0; j < n; j++) {
      ans[visited[j]] = ans[visited[j]] + 1;
    }
  }
  for (ll i = 1; i < n; i++) {
    cout << ans[i] / 2 << " ";
  }
  cout << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long int ll;
typedef long double ld;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
typedef pair<ll, ll> pairs;
vector<pairs> p;
bool pairCompare(const pair<double, ll> &firstElof,
                 const pair<double, ll> &secondElof) {
  return firstElof.first < secondElof.first;
}
#define MAX_N 100100
bool x[MAX_N];
ll num[MAX_N];
ll fibl[MAX_N] = {0};
// 四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll fib(ll a) {
  if (fibl[a] != 0)
    return fibl[a];
  if (a == 0) {
    return 0;
  } else if (a == 1) {
    return 1;
  }
  return fibl[a] = fib(a - 1) + fib(a - 2);
}

ll eratosthenes(ll n) {
  int p = 0;
  for (ll i = 0; i <= n; ++i)
    x[i] = true;
  x[0] = x[1] = false;
  for (int i = 2; i <= n; ++i) {
    if (x[i]) {
      p++;
      for (int j = 2 * i; j <= n; j += i)
        x[j] = false;
    }

    num[i] = p;
  }
  return p;
}

ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

ll keta(ll N) {
  int tmp{};
  while (N > 0) {
    tmp += (N % 10);
    N /= 10;
  }
  N = tmp;
  return N;
}

int main() {
  ll n, x, y;
  cin >> n >> x >> y;
  vector<vector<ll>> graph(n);
  graph[0].push_back(1);
  for (ll i = 1; i < n - 1; i++) {
    graph[i].push_back(i - 1);
    graph[i].push_back(i + 1);
  }
  graph[n - 1].push_back(n - 2);
  graph[x - 1].push_back(y - 1);
  graph[y - 1].push_back(x - 1);
  ll visited[n];
  for (ll k = 1; k < n; k++) {
    ll ans = 0;
    for (ll i = 0; i < n; i++) {
      fill(visited, visited + n, n + 1);
      visited[i] = 0;
      queue<ll> que;
      que.push(i);
      while (!que.empty()) {
        ll a = que.front();
        if (visited[a] == k) {
          break;
        }
        que.pop();
        for (ll j = 0; j < graph[a].size(); j++) {
          if (visited[graph[a][j]] > visited[a] + 1) {
            visited[graph[a][j]] = visited[a] + 1;
            que.push(graph[a][j]);
          }
        }
      }
      for (ll j = 0; j < n; j++) {
        if (visited[j] == k)
          ans++;
      }
    }
    cout << ans / 2 << " ";
  }
  cout << endl;
  return 0;
}