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

#include <bits/stdc++.h>
#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)
using namespace std;

using pii = pair<int, int>;
#define fst first
#define snd second

const int dy[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dx[8] = {0, 1, 0, -1, 1, -1, 1, -1};

inline bool check(int i, int j, int h, int w) {
  if (i < 0 or h <= i)
    return false;
  if (j < 0 or w <= j)
    return false;
  return true;
}

auto convert(vector<string> board) {
  const int h = board.size();
  const int w = board[0].size();

  vector<vector<int>> used(h, vector<int>(w, -1));

  vector<int> ret;
  int total = 0;

  while (1) {
    int ci = -1, cj = -1;
    rep(i, h) rep(j, w) {
      if (used[i][j] == -1) {
        ci = i, cj = j;
        i = h, j = w;
      }
    }

    if (ci == -1)
      break;

    int par = -1;
    const int num = (board[ci][cj] == '#') ? 8 : 4;

    rep(i, num) {
      pii nxt = pii(ci, cj);
      nxt.fst += dy[i], nxt.snd += dx[i];
      if (check(nxt.fst, nxt.snd, h, w) == false)
        continue;

      if (used[nxt.fst][nxt.snd] != -1) {
        par = used[nxt.fst][nxt.snd];
        break;
      }
    }

    ret.push_back(par);

    queue<pii> q;
    q.push(pii(ci, cj));
    used[ci][cj] = total;

    while (!q.empty()) {
      pii cur = q.front();
      q.pop();
      rep(i, num) {
        pii nxt = cur;
        nxt.fst += dy[i], nxt.snd += dx[i];

        if (check(nxt.fst, nxt.snd, h, w) == false)
          continue;

        if (used[nxt.fst][nxt.snd] == -1 and
            board[nxt.fst][nxt.snd] == board[ci][cj]) {
          used[nxt.fst][nxt.snd] = total;
          q.push(nxt);
        }
      }
    }

    total++;
  }

  return ret;
}

using ll = long long;
ll R[410];

const ll mod = 1000000009LL;

ll dfs(vector<int> tree, int v, int d) {
  const int n = tree.size();

  vector<ll> ary;
  rep(v2, n) if (tree[v2] == v) ary.push_back(dfs(tree, v2, d + 1));

  ll ret = 1LL;
  for (auto &it : ary)
    ret = 1LL * ret * (R[d] + it) % mod;
  return ret;
}

int main(void) {
  int h, w;

  random_device rnd;
  mt19937 mt(rnd());
  uniform_int_distribution<ll> rand(0, mod);

  rep(i, 400) R[i] = rand(mt);

  while (cin >> h >> w, h) {
    vector<int> tree, tree2;

    {
      vector<string> board(h + 2, string(w + 2, '.'));
      rep(i, 1, h + 1) rep(j, 1, w + 1) cin >> board[i][j];
      tree = convert(board);
    }

    cin >> h >> w;

    {
      vector<string> board(h + 2, string(w + 2, '.'));
      rep(i, 1, h + 1) rep(j, 1, w + 1) cin >> board[i][j];
      tree2 = convert(board);
    }

    if (dfs(tree, 0, 0) == dfs(tree2, 0, 0))
      puts("yes");
    else
      puts("no");

    /*
    for(auto &it:tree) cerr << it << " ";
    cerr << endl;

    for(auto &it:tree2) cerr << it << " ";
    cerr << endl;
    */
  }

  return 0;
}

Error: 0
-1 0 1 1 
-1 0 1 1 
-1 0 0 
-1 0 
-1 
-1 0 
-1 0 1 
-1 0 
-1 0 1 2 3 
-1 0 1 1 
-1 0 1 2 3 
-1 0 1 2 0 3 4 
-1 0 1 1 1 
-1 0 1 1 1 
-1 0 
-1 0 


Fixed submission:

#include <bits/stdc++.h>
#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)
using namespace std;

using pii = pair<int, int>;
#define fst first
#define snd second

const int dy[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dx[8] = {0, 1, 0, -1, 1, -1, 1, -1};

inline bool check(int i, int j, int h, int w) {
  if (i < 0 or h <= i)
    return false;
  if (j < 0 or w <= j)
    return false;
  return true;
}

auto convert(vector<string> board) {
  const int h = board.size();
  const int w = board[0].size();

  vector<vector<int>> used(h, vector<int>(w, -1));

  vector<int> ret;
  int total = 0;

  while (1) {
    int ci = -1, cj = -1;
    rep(i, h) rep(j, w) {
      if (used[i][j] == -1) {
        ci = i, cj = j;
        i = h, j = w;
      }
    }

    if (ci == -1)
      break;

    int par = -1;
    const int num = (board[ci][cj] == '#') ? 8 : 4;

    rep(i, num) {
      pii nxt = pii(ci, cj);
      nxt.fst += dy[i], nxt.snd += dx[i];
      if (check(nxt.fst, nxt.snd, h, w) == false)
        continue;

      if (used[nxt.fst][nxt.snd] != -1) {
        par = used[nxt.fst][nxt.snd];
        break;
      }
    }

    ret.push_back(par);

    queue<pii> q;
    q.push(pii(ci, cj));
    used[ci][cj] = total;

    while (!q.empty()) {
      pii cur = q.front();
      q.pop();
      rep(i, num) {
        pii nxt = cur;
        nxt.fst += dy[i], nxt.snd += dx[i];

        if (check(nxt.fst, nxt.snd, h, w) == false)
          continue;

        if (used[nxt.fst][nxt.snd] == -1 and
            board[nxt.fst][nxt.snd] == board[ci][cj]) {
          used[nxt.fst][nxt.snd] = total;
          q.push(nxt);
        }
      }
    }

    total++;
  }

  return ret;
}

using ll = long long;
ll R[410];

const ll mod = 1000000009LL;

ll dfs(vector<int> tree, int v, int d) {
  const int n = tree.size();

  vector<ll> ary;
  rep(v2, n) if (tree[v2] == v) ary.push_back(dfs(tree, v2, d + 1));

  ll ret = 1LL;
  for (auto &it : ary)
    ret = 1LL * ret * (R[d] + it) % mod;
  return ret;
}

int main(void) {
  int h, w;

  random_device rnd;
  mt19937 mt(rnd());
  uniform_int_distribution<ll> rand(0, mod);

  rep(i, 400) R[i] = rand(mt);

  while (cin >> h >> w, h) {
    vector<int> tree, tree2;

    {
      vector<string> board(h + 2, string(w + 2, '.'));
      rep(i, 1, h + 1) rep(j, 1, w + 1) cin >> board[i][j];
      tree = convert(board);
    }

    cin >> h >> w;

    {
      vector<string> board(h + 2, string(w + 2, '.'));
      rep(i, 1, h + 1) rep(j, 1, w + 1) cin >> board[i][j];
      tree2 = convert(board);
    }

    if (dfs(tree, 0, 0) == dfs(tree2, 0, 0))
      puts("yes");
    else
      puts("no");

    for (auto &it : tree)
      cerr << it << " ";
    cerr << endl;

    for (auto &it : tree2)
      cerr << it << " ";
    cerr << endl;
  }

  return 0;
}