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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define uu unsigned
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define MOD 1000000007
#define pi pair<int, int>
#define pl pair<ll, ll>
#define mkp make_pair
#define ss second
#define ff first
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define vl vector<ll>
#define vb vector<bool>
#define pb push_back
#define pf push_front
#define printcas cout << "Case " << cas << ": "
#define _ continue;
#define endl '\\n'
#define __                                                                     \\
  cout << endl;                                                                \\
  continue;
#define vc4d(nam, type, a, b, c, d, fix)                                       \\
  vector<vector<vector<vector<type>>>> nam(                                    \\
      a, vector<vector<vector<type>>>(                                         \\
             b, vector<vector<type>>(c, vector<type>(d, fix))))
const ll oo = (1ll << 60);
int gg() {
  int x = 0, w = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    w = 0, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return w ? x : -x;
}
ll h, w, ch, cw, dh, dw;
ll X[] = {-1, 1, 0, 0}, Y[] = {0, 0, 1, -1},
   xxx[] = {-1, -1, 1, 1, -2, -2, -2, -2, -2, -1,
            -1, 0,  0, 1, 1,  2,  2,  2,  2,  2},
   yyy[] = {1, -1, -1, 1,  -2, -1, 0,  1, 2, -2,
            2, -2, 2,  -2, 2,  -2, -1, 0, 1, 2};
bool safe(ll x, ll y) {
  if (x > -1 and y > -1 and x < h and y < w)
    return true;
  return false;
}
int main() {
  io;
  int T = 1;
  // T=gg();
  // cin>>T;
  // for(int cas=1;cas<=T;cas++)
  while (T--) {
    ll i, j, k, l, cnt = 0;
    cin >> h >> w >> ch >> cw >> dh >> dw;
    vector<string> v(h);
    for (auto &x : v)
      cin >> x;
    ch--, cw--, dh--, dw--;
    queue<pl> qq({{ch, cw}}), pp;
    v[ch][cw] = '#';
    while (1) {
      if (!qq.empty()) {
        while (!qq.empty()) {
          pl u = qq.front();
          if (u.ff == dh and u.ss == dw) {
            cout << cnt;
            return 0;
          }
          pp.push(u);
          qq.pop();
          for (i = 0; i < 4; i++) {
            ll x = u.ff + X[i], y = u.ss + Y[i];
            if (safe(x, y) and v[x][y] == '.') {
              v[x][y] = '#';
              qq.push({x, y});
            }
          }
        }
      } else if (!pp.empty()) {
        while (!pp.empty()) {
          pl u = pp.front();
          pp.pop();
          for (i = 0; i < 20; i++) {
            ll x = u.ff + xxx[i], y = u.ss + yyy[i];
            if (safe(x, y) and v[x][y] == '.') {
              v[x][y] = '#';
              qq.push({x, y});
            }
          }
        }
        cnt++;
      } else {
        cout << -1;
        return 0;
      }
    }
  }
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const long long INF = 1e15;
//(i-1)*w+j-1
struct UnionFind {
  vector<int> branch;
  UnionFind(int N = 0) : branch(N, -1) {}

  int find(int x) {
    if (branch[x] < 0)
      return x;
    else
      return branch[x] = find(branch[x]); // メモ化、要素を入れて根が返ってくる
  }

  bool unite(int x, int y) {
    int rx, ry;
    rx = find(x);
    ry = find(y);
    if (rx == ry)
      return false; // 連結できたかどうか
    if (-branch[rx] < -branch[ry])
      swap(rx, ry);
    branch[rx] += branch[ry]; // 木のサイズを負の値で管理
    branch[ry] = rx;
    return true;
  }

  bool same(int x, int y) {
    if (find(x) == find(y))
      return true;
    else
      return false;
  }

  int size(int x) { return -branch[find(x)]; }
};

int main() {
  UnionFind tree;

  int h, w;
  cin >> h >> w;
  int sy, sx, gy, gx;
  cin >> sy >> sx >> gy >> gx;
  string sur[h];

  for (int i = 0; i < h; i++)
    cin >> sur[i];

  auto inout = [&](int x, int y) {
    if (0 <= x && x < w && 0 <= y && y < h)
      return true;
    else
      return false;
  };

  vector<vector<bool>> flag(h, vector<bool>(w, false));

  auto dfs = [&](pair<int, int> s, int n) {
    int dy[4] = {1, -1, 0, 0}, dx[4] = {0, 0, 1, -1};
    queue<pair<int, int>> que;
    que.push(s);
    while (!que.empty()) {
      int x = que.front().first;
      int y = que.front().second;
      que.pop();
      for (int i = 0; i < 4; i++) {
        int nx = x + dx[i], ny = y + dy[i];
        if (inout(nx, ny) && sur[ny][nx] == '.' && flag[ny][nx]) {
          flag[ny][nx] = true;
          tree.unite(n, ny * w + nx);
          que.push(make_pair(nx, ny));
        }
      }
    }
  };

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (!flag[i][j]) {
        flag[i][j] = true;
        dfs(make_pair(j, i), i * w + j);
      }
    }
  }

  return 0;
}