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

/*Stay true to yourself, yet always be open to learn. Work hard, and never give
up on your dreams, even when nobody else believes they can come true but you.
These are not cliches but real tools you need no matter what you do in life to
stay focused on your path.
*/

/*Pppppppp    UU      UU     CCCCCCCC    CCCCCCCC     HH     HH     UU      UU
  pp    pp    UU      UU     CC          CC           HH     HH     UU      UU
  pp    pp    UU      UU     CC          CC           HHHHHHHHH     UU      UU
  pppppppp    UU      UU     CC          CC           HHHHHHHHH     UU      UU
  pp          UU      UU     CC          CC           HH     HH     UU      UU
  pp          UUUUUUUUUU     CCCCCCCC    CCCCCCCC     HH     HH     UUUUUUUUUU
  */

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

#define int long long
typedef long long ll;
#define loop(i, n) for (int i = 0; i < n; i++)
#define ford(i, a, b) for (int i = b; i >= a; i--)
#define read(a)                                                                \\
  int a;                                                                       \\
  cin >> a;
#define readarr(a, n)                                                          \\
  int a[n];                                                                    \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> a[i];
#define readmat(a, n, m)                                                       \\
  int a[n][m];                                                                 \\
  for (int i = 0; i < n; i++)                                                  \\
    for (j = 0; j < m; j++)                                                    \\
      cin >> a[i][j];
#define pb push_back
#define ff first
#define ss second
#define vi vector<int>
#define pi pair<int, int>
#define vii vector<vector<int>>
#define sti set<int>
#define vpi vector<pair<int, int>>
#define print(a, n)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
    cout << a[i] << " ";                                                       \\
  cout << endl;
#define printmat(a, n, m)                                                      \\
  for (int i = 0; i < n; i++) {                                                \\
    for (j = 0; j < m; j++)                                                    \\
      cout << a[i][j] << " ";                                                  \\
    cout << endl;                                                              \\
  }
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define endl "\\n"
#define mp make_pair
const ll inf = 1e18;
const ll mod = 1e9 + 7;

void add_edge(vector<int> adj[], int src, int dest) {
  adj[src].push_back(dest);
  adj[dest].push_back(src);
}

// a modified version of BFS that stores predecessor
// of each vertex in array p
// and its distance from source in array d
bool BFS(vector<int> adj[], int src, int dest, int v, int pred[], int dist[]) {
  // a queue to maintain queue of vertices whose
  // adjacency list is to be scanned as per normal
  // DFS algorithm
  list<int> queue;

  // boolean array visited[] which stores the
  // information whether ith vertex is reached
  // at least once in the Breadth first search
  bool visited[v];

  // initially all vertices are unvisited
  // so v[i] for all i is false
  // and as no path is yet constructed
  // dist[i] for all i set to infinity
  for (int i = 0; i < v; i++) {
    visited[i] = false;
    dist[i] = INT_MAX;
    pred[i] = -1;
  }

  // now source is first to be visited and
  // distance from source to itself should be 0
  visited[src] = true;
  dist[src] = 0;
  queue.push_back(src);

  // standard BFS algorithm
  while (!queue.empty()) {
    int u = queue.front();
    queue.pop_front();
    for (int i = 0; i < adj[u].size(); i++) {
      if (visited[adj[u][i]] == false) {
        visited[adj[u][i]] = true;
        dist[adj[u][i]] = dist[u] + 1;
        pred[adj[u][i]] = u;
        queue.push_back(adj[u][i]);

        // We stop BFS when we find
        // destination.
        if (adj[u][i] == dest)
          return true;
      }
    }
  }

  return false;
}

// utility function to print the shortest distance
// between source vertex and destination vertex
int ShortestDistance(vector<int> adj[], int s, int dest, int v) {
  // predecessor[i] array stores predecessor of
  // i and distance array stores distance of i
  // from s
  int pred[v], dist[v];

  if (BFS(adj, s, dest, v, pred, dist) == false) {

    return -1;
  }
  vector<int> path;
  int crawl = dest;
  path.push_back(crawl);
  while (pred[crawl] != -1) {
    path.push_back(pred[crawl]);
    crawl = pred[crawl];
  }

  // distance from source is in distance array

  return (dist[dest]);

  // printing path from source to destination
}

int minEdgeBFS(vector<int> edges[], int u, int v, int n) {
  // visited[n] for keeping track of visited
  // node in BFS
  vector<bool> visited(n, 0);

  // Initialize distances as 0
  vector<int> distance(n, 0);

  // queue to do BFS.
  queue<int> Q;
  distance[u] = 0;

  Q.push(u);
  visited[u] = true;
  while (!Q.empty()) {
    int x = Q.front();
    Q.pop();

    for (int i = 0; i < edges[x].size(); i++) {
      if (visited[edges[x][i]])
        continue;

      // update distance for i
      distance[edges[x][i]] = distance[x] + 1;
      Q.push(edges[x][i]);
      visited[edges[x][i]] = 1;
    }
  }
  return distance[v];
}

signed main() {

  fast;
  int n, x, y;
  cin >> n >> x >> y;
  vector<int> adj[n];
  for (int i = 0; i < n - 1; i++) {
    adj[i].pb(i + 1);
    adj[i + 1].pb(i);
  }
  adj[x - 1].pb(y - 1);
  adj[y - 1].pb(x - 1);
  // vi d;

  // cout<<"hello"<<endl;
  vi c(n);
  x--;
  y--;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      int d = 0;
      if (j <= x)
        d = j - i;
      else if (j <= y) {
        if (i <= x)
          d = min(j - i, x - i + 1 + y - j);
        else
          d = min(j - i, i - x + 1 + y - j);
      } else {
        if (i <= x)
          d = min(j - i, x - i + 1 + j - y);
        else if (i <= y)
          d = min(j - i, i - x + 1 + j - y);
        else
          d = j - i;
      }

      c[d]++;
    }
  }

  for (int k = 1; k <= n - 1; k++) {
    cout << c[k] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

/*Stay true to yourself, yet always be open to learn. Work hard, and never give
up on your dreams, even when nobody else believes they can come true but you.
These are not cliches but real tools you need no matter what you do in life to
stay focused on your path.
*/

/*Pppppppp    UU      UU     CCCCCCCC    CCCCCCCC     HH     HH     UU      UU
  pp    pp    UU      UU     CC          CC           HH     HH     UU      UU
  pp    pp    UU      UU     CC          CC           HHHHHHHHH     UU      UU
  pppppppp    UU      UU     CC          CC           HHHHHHHHH     UU      UU
  pp          UU      UU     CC          CC           HH     HH     UU      UU
  pp          UUUUUUUUUU     CCCCCCCC    CCCCCCCC     HH     HH     UUUUUUUUUU
  */

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

#define int long long
typedef long long ll;
#define loop(i, n) for (int i = 0; i < n; i++)
#define ford(i, a, b) for (int i = b; i >= a; i--)
#define read(a)                                                                \\
  int a;                                                                       \\
  cin >> a;
#define readarr(a, n)                                                          \\
  int a[n];                                                                    \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> a[i];
#define readmat(a, n, m)                                                       \\
  int a[n][m];                                                                 \\
  for (int i = 0; i < n; i++)                                                  \\
    for (j = 0; j < m; j++)                                                    \\
      cin >> a[i][j];
#define pb push_back
#define ff first
#define ss second
#define vi vector<int>
#define pi pair<int, int>
#define vii vector<vector<int>>
#define sti set<int>
#define vpi vector<pair<int, int>>
#define print(a, n)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
    cout << a[i] << " ";                                                       \\
  cout << endl;
#define printmat(a, n, m)                                                      \\
  for (int i = 0; i < n; i++) {                                                \\
    for (j = 0; j < m; j++)                                                    \\
      cout << a[i][j] << " ";                                                  \\
    cout << endl;                                                              \\
  }
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define endl "\\n"
#define mp make_pair
const ll inf = 1e18;
const ll mod = 1e9 + 7;

void add_edge(vector<int> adj[], int src, int dest) {
  adj[src].push_back(dest);
  adj[dest].push_back(src);
}

// a modified version of BFS that stores predecessor
// of each vertex in array p
// and its distance from source in array d
bool BFS(vector<int> adj[], int src, int dest, int v, int pred[], int dist[]) {
  // a queue to maintain queue of vertices whose
  // adjacency list is to be scanned as per normal
  // DFS algorithm
  list<int> queue;

  // boolean array visited[] which stores the
  // information whether ith vertex is reached
  // at least once in the Breadth first search
  bool visited[v];

  // initially all vertices are unvisited
  // so v[i] for all i is false
  // and as no path is yet constructed
  // dist[i] for all i set to infinity
  for (int i = 0; i < v; i++) {
    visited[i] = false;
    dist[i] = INT_MAX;
    pred[i] = -1;
  }

  // now source is first to be visited and
  // distance from source to itself should be 0
  visited[src] = true;
  dist[src] = 0;
  queue.push_back(src);

  // standard BFS algorithm
  while (!queue.empty()) {
    int u = queue.front();
    queue.pop_front();
    for (int i = 0; i < adj[u].size(); i++) {
      if (visited[adj[u][i]] == false) {
        visited[adj[u][i]] = true;
        dist[adj[u][i]] = dist[u] + 1;
        pred[adj[u][i]] = u;
        queue.push_back(adj[u][i]);

        // We stop BFS when we find
        // destination.
        if (adj[u][i] == dest)
          return true;
      }
    }
  }

  return false;
}

// utility function to print the shortest distance
// between source vertex and destination vertex

int minEdgeBFS(vector<int> edges[], int u, int v, int n) {
  // visited[n] for keeping track of visited
  // node in BFS
  vector<bool> visited(n, 0);

  // Initialize distances as 0
  vector<int> distance(n, 0);

  // queue to do BFS.
  queue<int> Q;
  distance[u] = 0;

  Q.push(u);
  visited[u] = true;
  while (!Q.empty()) {
    int x = Q.front();
    Q.pop();

    for (int i = 0; i < edges[x].size(); i++) {
      if (visited[edges[x][i]])
        continue;

      // update distance for i
      distance[edges[x][i]] = distance[x] + 1;
      Q.push(edges[x][i]);
      visited[edges[x][i]] = 1;
    }
  }
  return distance[v];
}

signed main() {

  fast;
  int n, x, y;
  cin >> n >> x >> y;
  vector<int> adj[n];
  for (int i = 0; i < n - 1; i++) {
    adj[i].pb(i + 1);
    adj[i + 1].pb(i);
  }
  adj[x - 1].pb(y - 1);
  adj[y - 1].pb(x - 1);
  // vi d;

  // cout<<"hello"<<endl;
  vi c(n);
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      int d = minEdgeBFS(adj, i, j, n);
      if (d >= 1)
        c[d]++;
    }
  }

  for (int k = 1; k <= n - 1; k++) {
    cout << c[k] << endl;
  }
  return 0;
}