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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<string> vs;
typedef unordered_set<int> si;
typedef pair<int, int> pp;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)

template <typename T> void printV(vector<T> A) {
  if (A.empty())
    cerr << "EMTPY\\n";
  else {
    cerr << "[";
    for (int i = 0; i < A.size() - 1; ++i) {
      cerr << A[i] << ", ";
    }
    if (A.size())
      cerr << A.back() << "]\\n";
  }
}

template <typename T> void printVV(vector<vector<T>> A) {
  cerr << "[";
  for (auto v : A) {
    printV<T>(v);
  }
  cerr << "]";
}

template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}

template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, x, y;
  cin >> n >> x >> y;
  x--, y--;

  map<int, int> count;
  for (int i = 0; i < n; ++i) {
    vector<bool> visited(n, false);
    queue<int> q;
    q.push(i);

    int level = 0;
    while (!q.empty()) {
      queue<int> nq;
      while (!q.empty()) {
        int node = q.front();
        q.pop();
        if (visited[node])
          continue;

        visited[node] = true;
        count[level]++;
        if (node > 0 && !visited[node - 1])
          nq.push(node - 1);
        if (node < n - 1 && !visited[node + 1])
          nq.push(node + 1);
        if (node == x)
          nq.push(y);
        if (node == y)
          nq.push(x);
      }
      q = nq;
      ++level;
    }
  }

  for (int i = 1; i < n; ++i)
    cout << count[i] / 2 << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<string> vs;
typedef unordered_set<int> si;
typedef pair<int, int> pp;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)

template <typename T> void printV(vector<T> A) {
  if (A.empty())
    cerr << "EMTPY\\n";
  else {
    cerr << "[";
    for (int i = 0; i < A.size() - 1; ++i) {
      cerr << A[i] << ", ";
    }
    if (A.size())
      cerr << A.back() << "]\\n";
  }
}

template <typename T> void printVV(vector<vector<T>> A) {
  cerr << "[";
  for (auto v : A) {
    printV<T>(v);
  }
  cerr << "]";
}

template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}

template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

const int N = 2 * 1e3;
int dis[N][N];

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, x, y;
  cin >> n >> x >> y;
  x--, y--;

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j)
      dis[i][j] = 1 << 20;
    if (i + 1 < n)
      dis[i][i + 1] = 1;
    if (i > 0)
      dis[i][i - 1] = 1;
    dis[i][i] = 0;
  }

  dis[x][y] = 1, dis[y][x] = 1;

  for (int k = 0; k < n; ++k) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        if (dis[i][k] != 1 << 20 && dis[k][j] != 1 << 20)
          dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
      }
    }
  }

  map<int, int> count;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      count[dis[i][j]] += 1;
    }
  }

  for (int i = 1; i < n; ++i)
    cout << count[i] / 2 << '\\n';

  return 0;
}