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

#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

int H, W;
char grid[51][51];

struct Data {
  int id, top, left, down, right;
  Data() {
    id = -1;
    top = left = -1;
    down = right = 100;
  }
};

bool Loop(int dst, int src, const vector<vector<int>> &g, vector<bool> &visit) {
  if (dst == src)
    return true;

  if (visit[src])
    return false;

  visit[src] = true;
  for (unsigned i = 0; i < g[src].size(); ++i) {
    if (Loop(dst, g[src][i], g, visit))
      return true;
  }

  return false;
}

bool Solve() {
  unordered_map<char, Data> hash;
  int id = 0;

  // 登録
  for (int i = 0; i < H; ++i)
    for (int j = 0; j < W; ++j)
      if (grid[i][j] != '.') {
        char c = grid[i][j];

        if (hash.count(c) == 0) { // 未登録
          hash[c].id = id++;
          hash[c].top = hash[c].down = i;
          hash[c].left = hash[c].right = j;
        } else { // 登録済み
          hash[c].top = min(hash[c].top, i);
          hash[c].down = max(hash[c].down, i);
          hash[c].left = min(hash[c].left, j);
          hash[c].right = max(hash[c].right, j);
        }
      }

  vector<vector<int>> g(id, vector<int>());
  // 長方形の確認
  auto it = hash.begin();
  while (it != hash.end()) {
    for (int y = it->second.top; y <= it->second.down; ++y)
      for (int x = it->second.left; x <= it->second.right; ++x) {
        int src = it->second.id;

        if (grid[y][x] == '.')
          return false;
        else if (grid[y][x] != it->first &&
                 count(g[src].begin(), g[src].end(), hash[grid[y][x]].id) == 0)
          g[src].push_back(hash[grid[y][x]].id);
      }
    ++it;
  }

  // ループが存在するかのチェック
  for (unsigned i = 0; i < g.size(); ++i)
    for (unsigned j = 0; j < g[i].size(); ++j) {
      vector<bool> visit(id, false);
      visit[i] = true;

      if (Loop(i, g[i][j], g, visit))
        return false;
    }

  return true;
}

int main() {
  int T;

  cin >> T;
  while (T--) {
    cin >> H >> W;

    for (int i = 0; i < H; ++i)
      for (int j = 0; j < W; ++j)
        cin >> grid[i][j];

    if (Solve())
      cout << "SAFE\\n";
    else
      cout << "SUSPICIOUS\\n";
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

int H, W;
char grid[51][51];

struct Data {
  int id, top, left, down, right;
  Data() {
    id = -1;
    top = left = -1;
    down = right = 100;
  }
};

bool Loop(int dst, int src, vector<vector<int>> &g) {
  if (dst == src)
    return true;

  for (unsigned i = 0; i < g[src].size(); ++i)
    if (Loop(dst, g[src][i], g))
      return true;

  return false;
}

bool Solve() {
  unordered_map<char, Data> hash;
  int id = 0;

  // 登録
  for (int i = 0; i < H; ++i)
    for (int j = 0; j < W; ++j)
      if (grid[i][j] != '.') {
        char c = grid[i][j];

        if (hash.count(c) == 0) { // 未登録
          hash[c].id = id++;
          hash[c].top = hash[c].down = i;
          hash[c].left = hash[c].right = j;
        } else { // 登録済み
          hash[c].top = min(hash[c].top, i);
          hash[c].down = max(hash[c].down, i);
          hash[c].left = min(hash[c].left, j);
          hash[c].right = max(hash[c].right, j);
        }
      }

  vector<vector<int>> g(id, vector<int>());
  // 長方形の確認
  auto it = hash.begin();
  while (it != hash.end()) {
    for (int y = it->second.top; y <= it->second.down; ++y)
      for (int x = it->second.left; x <= it->second.right; ++x) {
        int src = it->second.id;

        if (grid[y][x] == '.')
          return false;
        else if (grid[y][x] != it->first &&
                 count(g[src].begin(), g[src].end(), hash[grid[y][x]].id) == 0)
          g[src].push_back(hash[grid[y][x]].id);
      }
    ++it;
  }

  // ループが存在するかのチェック
  for (unsigned i = 0; i < g.size(); ++i)
    for (unsigned j = 0; j < g[i].size(); ++j)
      if (Loop(i, g[i][j], g))
        return false;

  return true;
}

int main() {
  int T;

  cin >> T;
  while (T--) {
    cin >> H >> W;
    for (int i = 0; i < H; ++i)
      for (int j = 0; j < W; ++j)
        cin >> grid[i][j];

    if (Solve())
      cout << "SAFE\\n";
    else
      cout << "SUSPICIOUS\\n";
  }

  return 0;
}