Problem p03334 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define to(x) edge[x][i].v
#define mp make_pair
#define rint register int
#define debug(x) cerr << #x << "=" << x << endl
#define fgx cerr << "-------------" << endl
#define N 1000000
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
pii go[N];
int id[1000][1000], head[2][N], vis[2][N], col[2][N], tot[2], num[10], T;
struct Edge {
  int v, next;
  Edge(int _v = 0, int _n = 0) : v(_v), next(_n) {}
} edge[2][N];
inline void add(int p, int x, int y) {
  edge[p][++tot[p]] = Edge(y, head[p][x]);
  head[p][x] = tot[p];
}
void dfs(int p, int x, int c) {
  vis[p][x] = 1;
  col[p][x] = c;
  for (rint i = head[p][x]; i; i = edge[p][i].next)
    if (!vis[p][to(p)])
      dfs(p, to(p), c ^ 1);
    else if (col[p][to(p)] == col[p][x])
      assert(0);
}
vector<pii> q1, q2;
int main() {
  int n, d1, d2, cur, t = 0, x, y;
  cin >> n >> d1 >> d2;
  for (rint i = 0; i < n + n; i++)
    for (rint j = 0; j < n + n; j++)
      id[i][j] = ++t, go[t] = mp(i, j);
  for (rint i = 0; i < n + n; i++)
    for (rint j = 0; j < n + n; j++) {
      if (i * i + j * j == d1)
        q1.push_back(mp(i, j));
      if (i * i + j * j == d2)
        q2.push_back(mp(i, j));
    }
  for (rint i = 0; i < n + n; i++)
    for (rint j = 0; j < n + n; j++) {
      for (rint k = 0; k < q1.size(); k++) {
        x = i + q1[k].first, y = j + q1[k].second;
        if (0 <= x && x < n + n && 0 <= y && y < n + n)
          add(0, id[i][j], id[x][y]);
        x = i - q1[k].first, y = j + q1[k].second;
        if (0 <= x && x < n + n && 0 <= y && y < n + n)
          add(0, id[i][j], id[x][y]);
        x = i + q1[k].first, y = j - q1[k].second;
        if (0 <= x && x < n + n && 0 <= y && y < n + n)
          add(0, id[i][j], id[x][y]);
        x = i - q1[k].first, y = j - q1[k].second;
        if (0 <= x && x < n + n && 0 <= y && y < n + n)
          add(0, id[i][j], id[x][y]);
      }
      for (rint k = 0; k < q2.size(); k++) {
        x = i + q2[k].first, y = j + q2[k].second;
        if (0 <= x && x < n + n && 0 <= y && y < n + n)
          add(1, id[i][j], id[x][y]);
        x = i - q2[k].first, y = j + q2[k].second;
        if (0 <= x && x < n + n && 0 <= y && y < n + n)
          add(1, id[i][j], id[x][y]);
        x = i + q2[k].first, y = j - q2[k].second;
        if (0 <= x && x < n + n && 0 <= y && y < n + n)
          add(1, id[i][j], id[x][y]);
        x = i - q2[k].first, y = j - q2[k].second;
        if (0 <= x && x < n + n && 0 <= y && y < n + n)
          add(1, id[i][j], id[x][y]);
      }
    }
  for (rint i = 1; i <= t; i++)
    if (!vis[0][i])
      dfs(0, i, 0);
  for (rint i = 1; i <= t; i++)
    if (!vis[1][i])
      dfs(1, i, 0);
  for (rint i = 1; i <= t; i++)
    num[col[0][i] * 2 + col[1][i]]++;
  for (rint i = 0; i < 4; i++)
    if (num[i] >= n * n) {
      for (rint j = 1, now = 0; now < n * n && j <= t; j++)
        if (col[0][j] * 2 + col[1][j] == i)
          now++, printf("%d %d\n", go[j].first, go[j].second);
      return 0;
    }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define to(x) edge[x][i].v
#define mp make_pair
#define rint register int
#define debug(x) cerr << #x << "=" << x << endl
#define fgx cerr << "-------------" << endl
#define N 1000000
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
pii go[N];
int id[1000][1000], head[2][N], vis[2][N], col[2][N], tot[2], num[10], T;
struct Edge {
  int v, next;
  Edge(int _v = 0, int _n = 0) : v(_v), next(_n) {}
} edge[2][N * 10];
inline void add(int p, int x, int y) {
  edge[p][++tot[p]] = Edge(y, head[p][x]);
  head[p][x] = tot[p];
}
void dfs(int p, int x, int c) {
  vis[p][x] = 1;
  col[p][x] = c;
  for (rint i = head[p][x]; i; i = edge[p][i].next)
    if (!vis[p][to(p)])
      dfs(p, to(p), c ^ 1);
    else if (col[p][to(p)] == col[p][x])
      assert(0);
}
vector<pii> q1, q2;
int main() {
  int n, d1, d2, cur, t = 0, x, y;
  cin >> n >> d1 >> d2;
  for (rint i = 0; i < n + n; i++)
    for (rint j = 0; j < n + n; j++)
      id[i][j] = ++t, go[t] = mp(i, j);
  for (rint i = 0; i < n + n; i++)
    for (rint j = 0; j < n + n; j++) {
      if (i * i + j * j == d1)
        q1.push_back(mp(i, j));
      if (i * i + j * j == d2)
        q2.push_back(mp(i, j));
    }
  for (rint i = 0; i < n + n; i++)
    for (rint j = 0; j < n + n; j++) {
      for (rint k = 0; k < q1.size(); k++) {
        x = i + q1[k].first, y = j + q1[k].second;
        if (0 <= x && x < n + n && 0 <= y && y < n + n)
          add(0, id[i][j], id[x][y]);
        x = i - q1[k].first, y = j + q1[k].second;
        if (0 <= x && x < n + n && 0 <= y && y < n + n)
          add(0, id[i][j], id[x][y]);
        x = i + q1[k].first, y = j - q1[k].second;
        if (0 <= x && x < n + n && 0 <= y && y < n + n)
          add(0, id[i][j], id[x][y]);
        x = i - q1[k].first, y = j - q1[k].second;
        if (0 <= x && x < n + n && 0 <= y && y < n + n)
          add(0, id[i][j], id[x][y]);
      }
      for (rint k = 0; k < q2.size(); k++) {
        x = i + q2[k].first, y = j + q2[k].second;
        if (0 <= x && x < n + n && 0 <= y && y < n + n)
          add(1, id[i][j], id[x][y]);
        x = i - q2[k].first, y = j + q2[k].second;
        if (0 <= x && x < n + n && 0 <= y && y < n + n)
          add(1, id[i][j], id[x][y]);
        x = i + q2[k].first, y = j - q2[k].second;
        if (0 <= x && x < n + n && 0 <= y && y < n + n)
          add(1, id[i][j], id[x][y]);
        x = i - q2[k].first, y = j - q2[k].second;
        if (0 <= x && x < n + n && 0 <= y && y < n + n)
          add(1, id[i][j], id[x][y]);
      }
    }
  for (rint i = 1; i <= t; i++)
    if (!vis[0][i])
      dfs(0, i, 0);
  for (rint i = 1; i <= t; i++)
    if (!vis[1][i])
      dfs(1, i, 0);
  for (rint i = 1; i <= t; i++)
    num[col[0][i] * 2 + col[1][i]]++;
  for (rint i = 0; i < 4; i++)
    if (num[i] >= n * n) {
      for (rint j = 1, now = 0; now < n * n && j <= t; j++)
        if (col[0][j] * 2 + col[1][j] == i)
          now++, printf("%d %d\n", go[j].first, go[j].second);
      return 0;
    }
  return 0;
}