Problem p03334 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define show(x) cerr << #x << " = " << x << endl
using namespace std;
using ll = long long;
int main() {
  int N, D1, D2;
  cin >> N >> D1 >> D2;
  vector<int> sq(4 * N * N + 1, -1);
  for (int i = 0; i <= 2 * N; i++) {
    sq[i * i] = i;
  }
  using P = pair<int, int>;
  vector<P> d1, d2;
  for (int i = 0; i <= 2 * N; i++) {
    if (i * i <= D1 and sq[D1 - i * i] >= 0) {
      d1.push_back({i, sq[D1 - i * i]}), d1.push_back({i, -sq[D1 - i * i]}),
          d1.push_back({-i, sq[D1 - i * i]}),
          d1.push_back({-i, -sq[D1 - i * i]});
    }
    if (i * i <= D2 and sq[D2 - i * i] >= 0) {
      d2.push_back({i, sq[D2 - i * i]}), d2.push_back({i, -sq[D2 - i * i]}),
          d2.push_back({-i, sq[D2 - i * i]}),
          d2.push_back({-i, -sq[D2 - i * i]});
    }
  }
  const int SZ = 4 * N * N;
  vector<vector<int>> g1(SZ), g2(SZ);
  auto enc = [N](const int i, const int j) { return i * 2 * N + j; };
  auto in = [N](const int i, const int j) {
    return 0 <= i and i < 2 * N and 0 <= j and j < 2 * N;
  };
  for (int i = 0; i < 2 * N; i++) {
    for (int j = 0; j < 2 * N; j++) {
      for (const auto &d : d1) {
        const int di = d.first, dj = d.second;
        if (in(i + di, j + dj)) {
          g1[enc(i, j)].push_back(enc(i + di, j + dj));
        }
      }
      for (const auto &d : d2) {
        const int di = d.first, dj = d.second;
        if (in(i + di, j + dj)) {
          g2[enc(i, j)].push_back(enc(i + di, j + dj));
        }
      }
    }
  }
  vector<bool> ok(SZ, true);
  vector<bool> used(SZ, false);
  auto bfs = [&](const vector<vector<int>> &g, const int s) {
    using P = pair<int, bool>;
    queue<P> q;
    vector<int> b, w;
    used[s] = true, q.push({s, false}), b.push_back(s);
    while (not q.empty()) {
      const auto p = q.front();
      q.pop();
      for (const int to : g[p.first]) {
        if (used[to] or not ok[to]) {
          continue;
        }
        used[to] = true, q.push({to, not p.second}),
        (p.second ? b : w).push_back(to);
      }
    }
    if (b.size() < w.size()) {
      swap(b, w);
    }
    for (const int W : w) {
      ok[W] = false;
    }
  };
  for (int i = 0; i < SZ; i++) {
    if (not used[i]) {
      bfs(g1, i);
    }
  }
  fill(used.begin(), used.end(), false);
  for (int i = 0; i < SZ; i++) {
    if (not used[i]) {
      bfs(g2, i);
    }
  }
  auto dump = [N](const int code) {
    cout << code / 2 / N << " " << code % (2 * N) << endl;
  };
  int ans = 0;
  for (int i = 0; i < SZ and ans < N * N; i++) {
    if (ok[i]) {
      dump(i), ans++;
    }
  }
  assert(ans == N * N);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define show(x) cerr << #x << " = " << x << endl
using namespace std;
using ll = long long;
int main() {
  int N, D1, D2;
  cin >> N >> D1 >> D2;
  vector<int> sq(4 * N * N + 1, -1);
  for (int i = 0; i <= 2 * N; i++) {
    sq[i * i] = i;
  }
  using P = pair<int, int>;
  vector<P> d1, d2;
  for (int i = 0; i <= 2 * N; i++) {
    if (i * i <= D1 and sq[D1 - i * i] >= 0) {
      d1.push_back({i, sq[D1 - i * i]}), d1.push_back({i, -sq[D1 - i * i]}),
          d1.push_back({-i, sq[D1 - i * i]}),
          d1.push_back({-i, -sq[D1 - i * i]});
    }
    if (i * i <= D2 and sq[D2 - i * i] >= 0) {
      d2.push_back({i, sq[D2 - i * i]}), d2.push_back({i, -sq[D2 - i * i]}),
          d2.push_back({-i, sq[D2 - i * i]}),
          d2.push_back({-i, -sq[D2 - i * i]});
    }
  }
  const int SZ = 4 * N * N;
  vector<vector<int>> g1(SZ), g2(SZ);
  auto enc = [N](const int i, const int j) { return i * 2 * N + j; };
  auto in = [N](const int i, const int j) {
    return 0 <= i and i < 2 * N and 0 <= j and j < 2 * N;
  };
  for (int i = 0; i < 2 * N; i++) {
    for (int j = 0; j < 2 * N; j++) {
      for (const auto &d : d1) {
        const int di = d.first, dj = d.second;
        if (in(i + di, j + dj)) {
          g1[enc(i, j)].push_back(enc(i + di, j + dj));
        }
      }
      for (const auto &d : d2) {
        const int di = d.first, dj = d.second;
        if (in(i + di, j + dj)) {
          g2[enc(i, j)].push_back(enc(i + di, j + dj));
        }
      }
    }
  }
  vector<bool> ok(SZ, true);
  vector<bool> used(SZ, false);
  auto bfs = [&](const vector<vector<int>> &g, const int s) {
    using P = pair<int, bool>;
    queue<P> q;
    vector<int> b, w;
    used[s] = true, q.push({s, false}), b.push_back(s);
    while (not q.empty()) {
      const auto p = q.front();
      q.pop();
      for (const int to : g[p.first]) {
        if (used[to] or not ok[to]) {
          continue;
        }
        used[to] = true, q.push({to, not p.second}),
        (p.second ? b : w).push_back(to);
      }
    }
    if (b.size() < w.size()) {
      swap(b, w);
    }
    for (const int W : w) {
      ok[W] = false;
    }
  };
  for (int i = 0; i < SZ; i++) {
    if (not used[i]) {
      bfs(g1, i);
    }
  }
  fill(used.begin(), used.end(), false);
  for (int i = 0; i < SZ; i++) {
    if (ok[i] and not used[i]) {
      bfs(g2, i);
    }
  }
  auto dump = [N](const int code) {
    cout << code / 2 / N << " " << code % (2 * N) << endl;
  };
  int ans = 0;
  for (int i = 0; i < SZ and ans < N * N; i++) {
    if (ok[i]) {
      dump(i), ans++;
    }
  }
  assert(ans == N * N);
  return 0;
}