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

#include <algorithm>
#include <assert.h>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c)                                                           \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int h, w;
char field[100][100];
bool visit[100][100];
bool dfsvisit[50];
bool charvisit[200];
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
set<int> edges[50];

bool dfs(int where, int from) {
  if (where == from && dfsvisit[where]) {
    return false;
  }
  if (dfsvisit[where]) {
    return true;
  }
  dfsvisit[where] = true;
  FORIT(it, edges[where]) {
    if (!dfs(*it, from)) {
      return false;
    }
  }
  return true;
}

int main() {
  int test;
  scanf("%d", &test);
  while (test--) {
    REP(i, 50) { edges[i].clear(); }
    scanf("%d %d", &h, &w);
    MEMSET(charvisit, false);
    REP(y, h) { scanf("%s", field[y]); }
    REP(iniy, h) {
      REP(inix, w) {
        if (field[iniy][inix] == '.') {
          continue;
        }
        if (charvisit[(int)field[iniy][inix]]) {
          continue;
        }
        charvisit[(int)field[iniy][inix]] = true;
        MEMSET(visit, false);
        queue<pair<int, int>> que;
        que.push(make_pair(inix, iniy));
        int minx = inix;
        int miny = iniy;
        int maxx = inix;
        int maxy = iniy;
        while (!que.empty()) {
          int x = que.front().first;
          int y = que.front().second;
          que.pop();
          if (visit[y][x]) {
            continue;
          }
          visit[y][x] = true;
          if (field[y][x] == field[iniy][inix]) {
            minx = min(minx, x);
            miny = min(miny, y);
            maxx = max(maxx, x);
            maxy = max(maxy, y);
          }
          REP(i, 4) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx < 0 || nx >= w || ny < 0 || ny >= h) {
              continue;
            }
            que.push(make_pair(nx, ny));
          }
        }
        FOREQ(ny, miny, maxy) FOREQ(nx, minx, maxx) {
          if (field[ny][nx] == '.') {
            puts("SUSPICIOUS");
            goto next;
          } else if (field[ny][nx] != field[iniy][inix]) {
            edges[(int)field[iniy][inix] - 'A'].insert(field[ny][nx] - 'A');
          }
        }
      }
    }
    REP(i, 50) {
      MEMSET(dfsvisit, false);
      if (!dfs(i, i)) {
        puts("SUSPICIOUS");
        goto next;
      }
    }
    puts("SAFE");
  next:;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c)                                                           \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int h, w;
char field[100][100];
bool visit[100][100];
bool dfsvisit[10];
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
set<int> edges[10];

bool dfs(int where, int from) {
  if (where == from && dfsvisit[where]) {
    return false;
  }
  if (dfsvisit[where]) {
    return true;
  }
  dfsvisit[where] = true;
  FORIT(it, edges[where]) {
    if (!dfs(*it, from)) {
      return false;
    }
  }
  return true;
}

int main() {
  int test;
  scanf("%d", &test);
  while (test--) {
    REP(i, 10) { edges[i].clear(); }
    scanf("%d %d", &h, &w);
    MEMSET(visit, false);
    REP(y, h) { scanf("%s", field[y]); }
    REP(y, h) {
      REP(x, w) {
        if (visit[y][x] || field[y][x] == '.') {
          continue;
        }
        queue<pair<int, int>> que;
        que.push(make_pair(x, y));
        int minx = x;
        int miny = y;
        int maxx = x;
        int maxy = y;
        while (!que.empty()) {
          int x = que.front().first;
          int y = que.front().second;
          que.pop();
          if (visit[y][x]) {
            continue;
          }
          visit[y][x] = true;
          minx = min(minx, x);
          miny = min(miny, y);
          maxx = max(maxx, x);
          maxy = max(maxy, y);
          REP(i, 4) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx < 0 || nx >= w || ny < 0 || ny >= h) {
              continue;
            }
            if (field[y][x] != field[ny][nx]) {
              continue;
            }
            que.push(make_pair(nx, ny));
          }
        }
        FOREQ(ny, miny, maxy) FOREQ(nx, minx, maxx) {
          if (field[ny][nx] == '.') {
            puts("SUSPICIOUS");
            goto next;
          } else if (field[ny][nx] != field[y][x]) {
            edges[(int)field[y][x] - 'A'].insert(field[ny][nx] - 'A');
          }
        }
      }
    }
    REP(i, 10) {
      MEMSET(dfsvisit, false);
      if (!dfs(i, i)) {
        puts("SUSPICIOUS");
        goto next;
      }
    }
    puts("SAFE");
  next:;
  }
}