Problem p02726 (C++) — buggy submission, verdict: Runtime Error


#include <bits/stdc++.h>

using namespace std;

#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout << fixed;                                                               \\
  cout << setprecision(10);

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef unordered_map<ll, ll> um;
typedef vector<pll> vpll;
#define f first
#define s second

const ll N = 2e3 + 5;
const ll mod = 1e9 + 7;
const ll INF = 0x7f7f7f7f7f7f7f7f;
const ll INFi = 0x7f7f7f7f;

ll n, x, y, vis[N], ans[N];
vll adj[N];
queue<pll> q;

void bfs(ll s) {
  fill_n(vis, n + 2, 0);
  vis[s] = 1;
  q.push(make_pair(s, 0));
  while (q.size()) {
    x = q.front().f;
    y = q.front().s;
    q.pop();
    ll nn = adj[x].size();
    for (int i = 0; i < nn; i++) {
      ll it = adj[x][i];
      if (!vis[it]) {
        vis[it] = 1;
        q.push(make_pair(it, y + 1));
        ans[y + 1]++;
      }
    }
  }
}

int main() {
  cin >> n >> x >> y;
  for (int i = 1; i < n; i++) {
    adj[i].push_back(i + 1);
    adj[i + 1].push_back(i);
  }
  adj[x].push_back(y);
  adj[y].push_back(x);
  for (int i = 1; i <= n; i++)
    bfs(i);
  for (int i = 1; i < n; i++)
    cout << ans[i] / 2 << endl;
}

Error: 0

Fixed submission:

#pragma optimization_level 3
#pragma GCC optimize(                                                          \\
    "Ofast") // Comment optimisations for an lleratcive problem - use endl
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout << fixed;                                                               \\
  cout << setprecision(10);

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef unordered_map<ll, ll> um;
typedef vector<pll> vpll;
#define f first
#define s second

const ll N = 2e3 + 5;
const ll mod = 1e9 + 7;
const ll INF = 0x7f7f7f7f7f7f7f7f;
const ll INFi = 0x7f7f7f7f;

ll n, x, y, vis[N], ans[N];
vll adj[N];

void bfs(ll s) {
  fill_n(vis, n + 2, 0);
  queue<pll> q;
  vis[s] = 1;
  q.push({s, 0});
  while (q.size()) {
    x = q.front().f;
    y = q.front().s;
    q.pop();
    if (y == n - 1)
      return;
    for (auto it : adj[x]) {
      if (!vis[it]) {
        vis[it] = 1;
        q.push({it, y + 1});
        ans[y + 1]++;
      }
    }
  }
}

int main() {
  cin >> n >> x >> y;
  for (int i = 1; i < n; i++) {
    adj[i].push_back(i + 1);
    adj[i + 1].push_back(i);
  }
  adj[x].push_back(y);
  adj[y].push_back(x);
  for (int i = 1; i <= n; i++)
    bfs(i);
  for (int i = 1; i < n; i++)
    cout << ans[i] / 2 << endl;
}