Problem p03334 (C++)

Buggy submission (status: Runtime Error):
/*
    Author: Nguyen Tan Bao
    Status:
    Idea:
*/

#include <bits/stdc++.h>
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(), a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORE(i, a, b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a)                                                          \
  for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x

using namespace std;
using ll = long long;
using ld = double;
typedef pair<int, int> II;
typedef pair<II, int> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;

const ll MODBASE = 1000000007LL;
const int MAXN = 200010;
const int MAXM = 1000;
const int MAXK = 16;
const int MAXQ = 200010;

int n, d1, d2, group[MAXN], color[MAXN], cnt[10];
vector<int> edge[2][MAXN];
bool visited[MAXN];

int dist(II a, II b) {
  return (a.FI - b.FI) * (a.FI - b.FI) + (a.SE - b.SE) * (a.SE - b.SE);
}

void build(int d, int cs) {
  vector<II> a;
  FOR(i, -n, n)
  FOR(j, -n, n)
  if (i * i + j * j == d)
    a.push_back(II(i, j));
  FOR(i, 0, n - 1)
  FOR(j, 0, n - 1)
  for (II x : a) {
    int u = i + x.FI, v = j + x.SE;
    if (u >= 0 && u < n && v >= 0 && v < n)
      edge[cs][n * i + j].push_back(n * u + v);
  }
}

void dfs(int u, int col, int hs) {
  visited[u] = true;
  color[u] += hs * col;
  for (int v : edge[hs - 1][u])
    if (!visited[v])
      dfs(v, 1 - col, hs);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);
  cin >> n >> d1 >> d2;
  n <<= 1;
  build(d1, 0);
  build(d2, 1);

  FOR(i, 0, n * n - 1)
  if (!visited[i])
    dfs(i, 0, 1);
  MS(visited, false);
  FOR(i, 0, n * n - 1)
  if (!visited[i])
    dfs(i, 0, 2);

  FOR(i, 0, n * n - 1) cnt[color[i]]++;
  int res = 0, Max = 0, dem = 0;
  FOR(i, 0, 3)
  if (cnt[i] > Max) {
    Max = cnt[i];
    res = i;
  }
  FOR(i, 0, n - 1)
  FOR(j, 0, n - 1)
  if (color[n * i + j] == res) {
    cout << i << ' ' << j << "\n";
    dem++;
    if (dem * 4 == n * n)
      return 0;
  }
  return 0;
}


Fixed submission:
/*
    Author: Nguyen Tan Bao
    Status:
    Idea:
*/

#include <bits/stdc++.h>
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(), a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORE(i, a, b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a)                                                          \
  for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x

using namespace std;
using ll = long long;
using ld = double;
typedef pair<int, int> II;
typedef pair<II, int> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;

const ll MODBASE = 1000000007LL;
const int MAXN = 500010;
const int MAXM = 1000;
const int MAXK = 16;
const int MAXQ = 200010;

int n, d1, d2, group[MAXN], color[MAXN], cnt[10];
vector<int> edge[2][MAXN];
bool visited[MAXN];

int dist(II a, II b) {
  return (a.FI - b.FI) * (a.FI - b.FI) + (a.SE - b.SE) * (a.SE - b.SE);
}

void build(int d, int cs) {
  vector<II> a;
  FOR(i, -n, n)
  FOR(j, -n, n)
  if (i * i + j * j == d)
    a.push_back(II(i, j));
  FOR(i, 0, n - 1)
  FOR(j, 0, n - 1)
  for (II x : a) {
    int u = i + x.FI, v = j + x.SE;
    if (u >= 0 && u < n && v >= 0 && v < n)
      edge[cs][n * i + j].push_back(n * u + v);
  }
}

void dfs(int u, int col, int hs) {
  visited[u] = true;
  color[u] += hs * col;
  for (int v : edge[hs - 1][u])
    if (!visited[v])
      dfs(v, 1 - col, hs);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);
  cin >> n >> d1 >> d2;
  n <<= 1;
  build(d1, 0);
  build(d2, 1);

  FOR(i, 0, n * n - 1)
  if (!visited[i])
    dfs(i, 0, 1);
  MS(visited, false);
  FOR(i, 0, n * n - 1)
  if (!visited[i])
    dfs(i, 0, 2);

  FOR(i, 0, n * n - 1) cnt[color[i]]++;
  int res = 0, Max = 0, dem = 0;
  FOR(i, 0, 3)
  if (cnt[i] > Max) {
    Max = cnt[i];
    res = i;
  }
  FOR(i, 0, n - 1)
  FOR(j, 0, n - 1)
  if (color[n * i + j] == res) {
    cout << i << ' ' << j << "\n";
    dem++;
    if (dem * 4 == n * n)
      return 0;
  }
  return 0;
}