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

/* *****fireice**** */

#include <bits/stdc++.h>

#define ll long long int
#define vi vector<int>
#define vc vector<char>
#define vl vector<ll>
#define vii vector<pair<int, int>>
#define vll vector<pair<ll, ll>>
#define pii pair<int, int>
#define pb push_back
#define el '\\n'
#define N 1000000007
#define re return
#define INF INT_MAX
#define F first
#define S second
#define L length
#define mp make_pair
#define PI 3.14159265
#define all(a) (a).begin(), (a).end()
#define forup(i, n) for (int i = 0; i < n; i++)
#define forf(i, x, n) for (int i = x; i < n; i++)
#define ford(i, n) for (int(i) = (n)-1; i >= 0; (i)--)
#define take(a, n) forup(i, n) cin >> a[i];
using namespace std;

int gcdExtended(int a, int b, int *x, int *y) {
  // Base Case
  if (a == 0) {
    *x = 0, *y = 1;
    return b;
  }

  int x1, y1; // To store results of recursive call
  int gcd = gcdExtended(b % a, a, &x1, &y1);

  // Update x and y using results of recursive
  // call
  *x = y1 - (b / a) * x1;
  *y = x1;

  return gcd;
}

ll modInverse(int a, int m) {
  int x, y;
  int g = gcdExtended(a, m, &x, &y);
  // m is added to handle negative x
  ll res = ((ll)x % m + (ll)m) % m;

  return res;
}

ll poww(ll x, ll n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0) // n is even
    return poww(x * x, n / 2);
  else // n is odd
    return x * poww(x * x, (n - 1) / 2);
}

ll modularExponentiation(ll x, ll n, ll M) {
  ll result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result = (result * x) % M;
    x = (x * x) % M;
    n = n / 2;
  }
  return result;
}

inline ll max(ll a, ll b) {
  if (a > b)
    re a;
  else
    re b;
}

inline ll min(ll a, ll b) {
  if (a > b)
    re b;
  else
    re a;
}

int gcd(int a, int b) {
  if (a == 0)
    return b;
  if (b == 0)
    return a;

  if (a == b)
    return a;

  if (a > b)
    return gcd(a - b, b);
  else
    return gcd(a, b - a);
}

ll ncr(int n, int r) {
  ll ans = 1;
  r = min(r, n - r);
  forup(i, r) { ans = (ans * (n - i) / (i + 1)) % N; }
  re ans;
}

ll n, m, k, x, y;
vi adj[2000];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int t = 1;
  // cin>>t;
  while (t--) {

    cin >> n >> x >> y;
    forup(i, n - 1) {
      adj[i].pb(i + 1);
      adj[i + 1].pb(i);
    }
    adj[x - 1].pb(y - 1);
    adj[y - 1].pb(x - 1);
    int a[n] = {0};
    forup(i, n - 1) {
      queue<pii> q;
      bool visited[n] = {0};
      visited[i] = 1;
      q.push({i, 0});
      while (!q.empty()) {
        pii x = q.front();
        q.pop();
        for (auto z : adj[x.F]) {
          if (!visited[z]) {
            if (z > i) {
              a[x.S + 1]++;
            }
            // cout<<x.F+1<<" "<<z+1<<" "<<x.S+1<<el;}
            visited[z] = 1;
            q.push({z, x.S + 1});
          }
        }
      }
    }
    int i = 1;
    while (i < n) {
      cout << a[i] << el;
      i++;
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

/* *****fireice**** */

#include <bits/stdc++.h>

#define ll long long int
#define vi vector<int>
#define vc vector<char>
#define vl vector<ll>
#define vii vector<pair<int, int>>
#define vll vector<pair<ll, ll>>
#define pii pair<int, int>
#define pb push_back
#define el '\\n'
#define N 1000000007
#define re return
#define INF INT_MAX
#define F first
#define S second
#define L length
#define mp make_pair
#define PI 3.14159265
#define all(a) (a).begin(), (a).end()
#define forup(i, n) for (int i = 0; i < n; i++)
#define forf(i, x, n) for (int i = x; i < n; i++)
#define ford(i, n) for (int(i) = (n)-1; i >= 0; (i)--)
#define take(a, n) forup(i, n) cin >> a[i];
using namespace std;

int gcdExtended(int a, int b, int *x, int *y) {
  // Base Case
  if (a == 0) {
    *x = 0, *y = 1;
    return b;
  }

  int x1, y1; // To store results of recursive call
  int gcd = gcdExtended(b % a, a, &x1, &y1);

  // Update x and y using results of recursive
  // call
  *x = y1 - (b / a) * x1;
  *y = x1;

  return gcd;
}

ll modInverse(int a, int m) {
  int x, y;
  int g = gcdExtended(a, m, &x, &y);
  // m is added to handle negative x
  ll res = ((ll)x % m + (ll)m) % m;

  return res;
}

ll poww(ll x, ll n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0) // n is even
    return poww(x * x, n / 2);
  else // n is odd
    return x * poww(x * x, (n - 1) / 2);
}

ll modularExponentiation(ll x, ll n, ll M) {
  ll result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result = (result * x) % M;
    x = (x * x) % M;
    n = n / 2;
  }
  return result;
}

inline ll max(ll a, ll b) {
  if (a > b)
    re a;
  else
    re b;
}

inline ll min(ll a, ll b) {
  if (a > b)
    re b;
  else
    re a;
}

int gcd(int a, int b) {
  if (a == 0)
    return b;
  if (b == 0)
    return a;

  if (a == b)
    return a;

  if (a > b)
    return gcd(a - b, b);
  else
    return gcd(a, b - a);
}

ll ncr(int n, int r) {
  ll ans = 1;
  r = min(r, n - r);
  forup(i, r) { ans = (ans * (n - i) / (i + 1)) % N; }
  re ans;
}
set<pii> s;
ll n, m, k, x, y;
map<int, int> ans;
vi adj[2000];
void shortestPath(int src) {
  set<pair<int, int>> setds;

  vector<int> dist(n, INF);

  setds.insert(make_pair(0, src));
  dist[src] = 0;

  while (!setds.empty()) {
    pair<int, int> tmp = *(setds.begin());
    setds.erase(setds.begin());

    int u = tmp.second;

    // 'i' is used to get all adjacent vertices of a vertex

    for (auto i = adj[u].begin(); i != adj[u].end(); ++i) {
      int v = *i;
      int weight = 1;

      if (dist[v] > dist[u] + weight) {

        if (dist[v] != INF)
          setds.erase(setds.find(make_pair(dist[v], v)));

        dist[v] = dist[u] + weight;
        setds.insert(make_pair(dist[v], v));
      }
    }
  }

  // Print shortest distances stored in dist[]
  forup(i, n) {
    if (dist[i] != 0 && s.find({min(i, src), max(i, src)}) == s.end()) {
      ans[dist[i]]++;
      s.insert({min(i, src), max(i, src)});
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int t = 1;
  // cin>>t;
  while (t--) {

    cin >> n >> x >> y;
    forup(i, n - 1) {
      adj[i].pb(i + 1);
      adj[i + 1].pb(i);
    }
    adj[x - 1].pb(y - 1);
    adj[y - 1].pb(x - 1);
    int i = 1;
    forup(i, n) { shortestPath(i); }
    while (i < n) {
      cout << ans[i] << el;
      i++;
    }
  }

  return 0;
}