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

/*                        _
                       _oo0oo_
                      o8888888o
                      88" . "88
                      (| -_- |)
                      0\\  =  /0
                   ____/`---'\\____
                  /  \\\\|     |//  \\
                 / \\\\||| /:\\ |||// \\
                / _||||| -:- |||||- \\
               |   | \\\\\\ \\-/ /// |   |
               | \\_|  ''\\---/''  |_/ |
               \\  .-\\__  '-'  ___/-. /
            ____'. .'  /--.--\\  `. .'_____
          /"" '<  `.___\\_<|>_/___.' >' "" \\
         | | :  `- \\`.;`\\ _ /`;.`/ - ` : | |
         \\  \\ `_.   \\_ __\\ /__ _/   .-` /  /
          `-.____`.___ \\_____/___.-`___.-'

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 JENISH MONPARA S.    IIT PATNA    ADITI GOEL IS LOVE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
// while(scanf("%d",&n)!=EOF)

#pragma GCC optimize("O3")
#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define fio                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#include <bits/stdc++.h>
#define ii int64_t
#define lli long long int
#define li long int
#define vi vector<int64_t>
#define mii unordered_map<int64_t, int64_t>
#define nl cout << "\\n"
#define deb(x) cout << "\\nhi" << x << "\\n"
#define mod 1000000007
ii n, t, i, j, k, l, x, y, z, cnt = 0, sum = 0, mx = -1, mn = INT_MAX;
bool f, f1, f2;
using namespace std;

int main() {
  fio;

  cin >> n >> x >> y;
  vi v[n + 2];
  v[1].push_back(2);
  for (i = 2; i < n; i++) {
    v[i].push_back(i + 1);
    v[i].push_back(i - 1);
  }
  v[n].push_back(n - 1);
  v[x].push_back(y);
  v[y].push_back(x);

  ii cnt[n + 2];
  memset(cnt, 0, sizeof(cnt));

  for (i = 1; i <= n; i++) {
    vi level(n + 1, 10000);
    vector<bool> f(n + 1, false);
    queue<ii> q;
    level[i] = 0;
    q.push(i);
    f[i] = true;

    while (!q.empty()) {
      x = q.front();
      q.pop();
      z = level[x] + 1;
      // if(z>k)break;
      for (j = 0; j < v[x].size(); j++) {
        if (f[v[x][j]])
          continue;
        q.push(v[x][j]);
        f[v[x][j]] = true;
        level[v[x][j]] = z;
        cnt[z]++;
        // if(z==k)cnt++;
        // cout<<v[x][j]<<" "<<level[v[x][j]];nl;
      }
    }
  }
  for (i = 1; i < n; i++) {
    cout << cnt[i] / 2;
    nl;
  }
}


Error: TLE

Fixed submission:

/*                        _
                       _oo0oo_
                      o8888888o
                      88" . "88
                      (| -_- |)
                      0\\  =  /0
                   ____/`---'\\____
                  /  \\\\|     |//  \\
                 / \\\\||| /:\\ |||// \\
                / _||||| -:- |||||- \\
               |   | \\\\\\ \\-/ /// |   |
               | \\_|  ''\\---/''  |_/ |
               \\  .-\\__  '-'  ___/-. /
            ____'. .'  /--.--\\  `. .'_____
          /"" '<  `.___\\_<|>_/___.' >' "" \\
         | | :  `- \\`.;`\\ _ /`;.`/ - ` : | |
         \\  \\ `_.   \\_ __\\ /__ _/   .-` /  /
          `-.____`.___ \\_____/___.-`___.-'

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 JENISH MONPARA S.    IIT PATNA    ADITI GOEL IS LOVE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
// while(scanf("%d",&n)!=EOF)

#pragma GCC optimize("O3")
#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define fio                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#include <bits/stdc++.h>
#define ii int64_t
#define lli long long int
#define li long int
#define vi vector<int64_t>
#define mii unordered_map<int64_t, int64_t>
#define nl cout << "\\n"
#define deb(x) cout << "\\nhi" << x << "\\n"
#define mod 1000000007
ii n, t, i, j, k, l, x, y, z, cnt = 0, sum = 0, mx = -1, mn = INT_MAX;
bool f, f1, f2;
using namespace std;

int main() {
  fio;

  cin >> n >> x >> y;
  vi v[n + 2];
  v[1].push_back(2);
  for (i = 2; i < n; i++) {
    v[i].push_back(i + 1);
    v[i].push_back(i - 1);
  }
  v[n].push_back(n - 1);
  v[x].push_back(y);
  v[y].push_back(x);

  for (k = 1; k < n; k++) {
    cnt = 0;
    for (i = 1; i <= n; i++) {
      vi level(n + 1, 10000);
      vector<bool> f(n + 1, false);
      queue<ii> q;
      level[i] = 0;
      q.push(i);
      f[i] = true;

      while (!q.empty()) {
        x = q.front();
        q.pop();
        z = level[x] + 1;
        if (z > k)
          break;
        for (j = 0; j < v[x].size(); j++) {
          if (f[v[x][j]])
            continue;
          q.push(v[x][j]);
          f[v[x][j]] = true;
          level[v[x][j]] = z;
          if (z == k)
            cnt++;
          // cout<<v[x][j]<<" "<<level[v[x][j]];nl;
        }
      }

      // cout<<i<<" "<<cnt;nl;
    }
    cout << cnt / 2;
    nl;
  }
}