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

// Author: kaiyu
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(), x.end()
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define tt                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define int3(x, y, z)                                                          \\
  int x, y, z;                                                                 \\
  cin >> x >> y >> z;
#define int2(x, y)                                                             \\
  int x, y;                                                                    \\
  cin >> x >> y;
#define int1(x)                                                                \\
  int x;                                                                       \\
  cin >> x;
#define vi vector<int>
#define vii vector<vector<int>>
typedef long long ll;
#define fio                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(NULL);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// bool cmp(pair<string,int>p1,pair<string,int>p2) {
//     return p1.ss > p2.ss;
// }
// if (s1.find(s2) != std::string::npos) {
//    std::cout << "found!" << '\\n';
// }

using namespace std;

void bfs(int sn, vector<vi> adj, int n, int *level) {
  queue<int> q;
  q.push(sn);
  level[sn] = 0;
  bool vis[n + 1];
  memset(vis, false, sizeof(vis));
  vis[sn] = true;
  while (!q.empty()) {
    int p = q.front();
    q.pop();
    for (int t = 0; t < adj[p].size(); ++t) {
      if (!vis[adj[p][t]]) {
        level[adj[p][t]] = level[p] + 1;
        q.push(adj[p][t]);
        vis[adj[p][t]] = true;
      }
    }
  }
}
int main() {
  fio int3(n, x, y) vector<vi> adj(n + 1);

  for (int i = 1; i < n; ++i) {
    adj[i].push_back(i + 1);
    adj[i + 1].push_back(i);
  }
  adj[x].pb(y);
  adj[y].pb(x);
  map<int, int> levels;
  int level[n + 1];
  memset(level, 0, sizeof(level));
  for (int j = 1; j < n; ++j) {
    bfs(j, adj, n, level);
    for (int i = j; i <= n; ++i) {
      levels[level[i]]++;
    }
    memset(level, 0, sizeof(level));
  }
  for (int l = 1; l <= n - 1; ++l) {

    cout << levels[l] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

// Author: kaiyu
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(), x.end()
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define tt                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define int3(x, y, z)                                                          \\
  int x, y, z;                                                                 \\
  cin >> x >> y >> z;
#define int2(x, y)                                                             \\
  int x, y;                                                                    \\
  cin >> x >> y;
#define int1(x)                                                                \\
  int x;                                                                       \\
  cin >> x;
#define vi vector<int>
#define vii vector<vector<int>>
typedef long long ll;
#define fio                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(NULL);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// bool cmp(pair<string,int>p1,pair<string,int>p2) {
//     return p1.ss > p2.ss;
// }
// if (s1.find(s2) != std::string::npos) {
//    std::cout << "found!" << '\\n';
// }

using namespace std;

void bfs(int sn, vector<vi> adj, int n, vector<int> &level) {
  queue<int> q;
  q.push(sn);
  level[sn] = 0;
  bool vis[n + 1];
  memset(vis, false, sizeof(vis));
  vis[sn] = true;
  while (!q.empty()) {
    int p = q.front();
    q.pop();
    for (int t = 0; t < adj[p].size(); ++t) {
      if (!vis[adj[p][t]]) {
        level[adj[p][t]] = level[p] + 1;
        q.push(adj[p][t]);
        vis[adj[p][t]] = true;
      }
    }
  }
}
int main() {
  fio int3(n, x, y) vector<vi> adj(n + 1);

  for (int i = 1; i < n; ++i) {
    adj[i].push_back(i + 1);
    adj[i + 1].push_back(i);
  }
  adj[x].pb(y);
  adj[y].pb(x);
  vi levels;
  vector<int> level(n + 1, 0);
  for (int j = 1; j < n; ++j) {
    bfs(j, adj, n, level);
    for (int i = j; i <= n; ++i) {
      levels.pb(level[i]);
    }
    for (int k = 0; k < n + 1; ++k) {
      level[k] = 0;
    }
  }
  sort(all(levels));
  int ans = 0;
  for (int l = 1; l <= n - 1; ++l) {
    ans = 0;
    for (int i = 0; i < SZ(levels); ++i) {
      if (levels[i] == l) {
        ans++;
      }
    }
    cout << ans << endl;
  }

  return 0;
}