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

#include <bits/stdc++.h>
using namespace std;
#pragma region Macros
#define ll long long
#define ld long double
#define FOR(i, l, r) for (ll i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (ll i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n, 1)
#define pb push_back
#define eb emplace_back
#define SZ(x) ((ll)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T = ll> using V = vector<T>;
template <class T = ll> using VV = V<V<T>>;
using P = pair<ll, ll>;
#define ios                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr);
#define VEC(type, name, size)                                                  \\
  V<type> name(size);                                                          \\
  IN(name)
#define VVEC(type, name, h, w)                                                 \\
  VV<type> name(h, V<type>(w));                                                \\
  IN(name)
#define INT(...)                                                               \\
  int __VA_ARGS__;                                                             \\
  IN(__VA_ARGS__)
#define LL(...)                                                                \\
  ll __VA_ARGS__;                                                              \\
  IN(__VA_ARGS__)
#define STR(...)                                                               \\
  string __VA_ARGS__;                                                          \\
  IN(__VA_ARGS__)
#define CHAR(...)                                                              \\
  char __VA_ARGS__;                                                            \\
  IN(__VA_ARGS__)
#define DOUBLE(...)                                                            \\
  DOUBLE __VA_ARGS__;                                                          \\
  IN(__VA_ARGS__)
#define LD(...)                                                                \\
  LD __VA_ARGS__;                                                              \\
  IN(__VA_ARGS__)
template <class T> void scan(T a) { cin >> a; }
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(long double &a) { cin >> a; }
void scan(char a[]) { scanf("%s", a); }
void scan(string &a) { cin >> a; }
template <class T> void scan(V<T> &);
template <class T, class L> void scan(pair<T, L> &);
template <class T> void scan(V<T> &a) {
  for (auto &i : a)
    scan(i);
}
template <class T, class L> void scan(pair<T, L> &p) {
  scan(p.first);
  scan(p.second);
}
template <class T> void scan(T &a) { cin >> a; }
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &...tail) {
  scan(head);
  IN(tail...);
}
template <class T> inline void print(T x) { cout << x << '\\n'; }
template <class T> V<T> press(V<T> &x) {
  V<T> res = x;
  sort(all(res));
  res.erase(unique(all(res)), res.end());
  REP(i, SZ(x)) { x[i] = lower_bound(all(res), x[i]) - res.begin(); }
  return res;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\\n'; }
inline void err(bool b = true) {
  if (b) {
    cout << -1 << '\\n';
    exit(0);
  }
}
template <class T> inline void fin(bool b = true, T e = 0) {
  if (b) {
    cout << e << '\\n';
    exit(0);
  }
}
template <class T> T Roundup_div(T x, T y) { return (x + (y - 1)) / y; }
template <typename T> T pow(T a, long long n, T e = 1) {
  T ret = e;
  while (n) {
    if (n & 1)
      ret *= a;
    a *= a;
    n >>= 1;
  }
  return ret;
}
const ll INF = 1e18;
#pragma endregion

// Graph Template
struct Edge {
  ll to, cost;
  Edge(ll to, ll cost) : to(to), cost(cost) {}
  bool operator<(const Edge &a) const { return cost < a.cost; }
};
using Graph = VV<>;
using WGraph = VV<Edge>;

void Read_Graph(Graph &g, int m, bool directed = false) {
  REP(i, m) {
    LL(u, v);
    u--;
    v--;
    g[u].pb(v);
    if (!directed)
      g[v].pb(u);
  }
}
void Read_Tree(Graph &g, bool directed = false) {
  Read_Graph(g, SZ(g) - 1, directed);
}

void Read_Graph(WGraph &g, int m, bool directed = false) {
  REP(i, m) {
    LL(u, v, c);
    u--;
    v--;
    g[u].pb({v, c});
    if (!directed)
      g[v].pb({u, c});
  }
}
void Read_Tree(WGraph &g, bool directed = false) {
  Read_Graph(g, SZ(g) - 1, directed);
}

// s始点のdijkstraを行い、各点のsからの距離を返す
// g:WGraph, s:始点(0-indexed)
V<> dijkstra(WGraph g, int s) {
  V<> dist(SZ(g), INF);
  priority_queue<P, vector<P>, greater<P>> que;
  dist[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (dist[v] < p.first)
      continue;
    REP(i, SZ(g[v])) {
      Edge e = g[v][i];
      if (dist[e.to] > dist[v] + e.cost) {
        dist[e.to] = dist[v] + e.cost;
        que.push(P(dist[e.to], e.to));
      }
    }
  }
  return dist;
}

// grid
int h, w;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {
    1, 1, 0, -1, -1, -1, 0, 1,
};
int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
inline bool inside(int x, int y) {
  return x >= 0 and x < h and y >= 0 and y < w;
}

struct UnionFind {
public:
  vector<int> par;
  UnionFind(int n) { par = vector<int>(n, -1); }
  int root(int x) {
    if (par[x] < 0)
      return x;
    return par[x] = root(par[x]);
  }
  bool issame(int x, int y) { return root(x) == root(y); }
  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }
  int size(int x) { return -par[root(x)]; }
};

int gtoi(int x, int y) { return x * w + y; }

int main() {
  ios;
  cin >> h >> w;
  LL(sx, sy, gx, gy);
  sx--;
  sy--;
  gx--;
  gy--;
  VVEC(char, a, h, w);
  UnionFind uf(h * w);
  REP(i, h) REP(j, w) {
    if (a[i][j] == '#')
      continue;
    REP(k, 4) {
      int nx = i + dx[k], ny = j + dy[k];
      if (inside(nx, ny))
        if (a[nx][ny] == '.')
          uf.merge(gtoi(i, j), gtoi(nx, ny));
    }
  }
  WGraph G(h * w);
  REP(i, h) REP(j, w) {
    if (a[i][j] == '#')
      continue;
    for (int s = -2; s <= 2; s++)
      for (int t = -2; t <= 2; t++) {
        int nx = i + s, ny = j + t;
        if (inside(nx, ny) and uf.root(gtoi(i, j)) != uf.root(gtoi(nx, ny))) {
          if (a[nx][ny] == '#')
            continue;
          G[uf.root(gtoi(i, j))].pb({uf.root(gtoi(nx, ny)), 1});
          G[uf.root(gtoi(nx, ny))].pb({uf.root(gtoi(i, j)), 1});
        }
      }
  }
  auto dist = dijkstra(G, uf.root(gtoi(sx, sy)));
  if (dist[uf.root(gtoi(gx, gy))] != INF)
    print(dist[uf.root(gtoi(gx, gy))]);
  else
    print(-1);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#pragma region Macros
#define ll long long
#define ld long double
#define FOR(i, l, r) for (ll i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (ll i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n, 1)
#define pb push_back
#define eb emplace_back
#define SZ(x) ((ll)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T = ll> using V = vector<T>;
template <class T = ll> using VV = V<V<T>>;
using P = pair<ll, ll>;
#define ios                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr);
#define VEC(type, name, size)                                                  \\
  V<type> name(size);                                                          \\
  IN(name)
#define VVEC(type, name, h, w)                                                 \\
  VV<type> name(h, V<type>(w));                                                \\
  IN(name)
#define INT(...)                                                               \\
  int __VA_ARGS__;                                                             \\
  IN(__VA_ARGS__)
#define LL(...)                                                                \\
  ll __VA_ARGS__;                                                              \\
  IN(__VA_ARGS__)
#define STR(...)                                                               \\
  string __VA_ARGS__;                                                          \\
  IN(__VA_ARGS__)
#define CHAR(...)                                                              \\
  char __VA_ARGS__;                                                            \\
  IN(__VA_ARGS__)
#define DOUBLE(...)                                                            \\
  DOUBLE __VA_ARGS__;                                                          \\
  IN(__VA_ARGS__)
#define LD(...)                                                                \\
  LD __VA_ARGS__;                                                              \\
  IN(__VA_ARGS__)
template <class T> void scan(T a) { cin >> a; }
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(long double &a) { cin >> a; }
void scan(char a[]) { scanf("%s", a); }
void scan(string &a) { cin >> a; }
template <class T> void scan(V<T> &);
template <class T, class L> void scan(pair<T, L> &);
template <class T> void scan(V<T> &a) {
  for (auto &i : a)
    scan(i);
}
template <class T, class L> void scan(pair<T, L> &p) {
  scan(p.first);
  scan(p.second);
}
template <class T> void scan(T &a) { cin >> a; }
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &...tail) {
  scan(head);
  IN(tail...);
}
template <class T> inline void print(T x) { cout << x << '\\n'; }
template <class T> V<T> press(V<T> &x) {
  V<T> res = x;
  sort(all(res));
  res.erase(unique(all(res)), res.end());
  REP(i, SZ(x)) { x[i] = lower_bound(all(res), x[i]) - res.begin(); }
  return res;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\\n'; }
inline void err(bool b = true) {
  if (b) {
    cout << -1 << '\\n';
    exit(0);
  }
}
template <class T> inline void fin(bool b = true, T e = 0) {
  if (b) {
    cout << e << '\\n';
    exit(0);
  }
}
template <class T> T Roundup_div(T x, T y) { return (x + (y - 1)) / y; }
template <typename T> T pow(T a, long long n, T e = 1) {
  T ret = e;
  while (n) {
    if (n & 1)
      ret *= a;
    a *= a;
    n >>= 1;
  }
  return ret;
}
const ll INF = 1e18;
#pragma endregion

// Graph Template
struct Edge {
  ll to, cost;
  Edge(ll to, ll cost) : to(to), cost(cost) {}
  bool operator<(const Edge &a) const { return cost < a.cost; }
};
using Graph = VV<>;
using WGraph = VV<Edge>;

void Read_Graph(Graph &g, int m, bool directed = false) {
  REP(i, m) {
    LL(u, v);
    u--;
    v--;
    g[u].pb(v);
    if (!directed)
      g[v].pb(u);
  }
}
void Read_Tree(Graph &g, bool directed = false) {
  Read_Graph(g, SZ(g) - 1, directed);
}

void Read_Graph(WGraph &g, int m, bool directed = false) {
  REP(i, m) {
    LL(u, v, c);
    u--;
    v--;
    g[u].pb({v, c});
    if (!directed)
      g[v].pb({u, c});
  }
}
void Read_Tree(WGraph &g, bool directed = false) {
  Read_Graph(g, SZ(g) - 1, directed);
}

// s始点のdijkstraを行い、各点のsからの距離を返す
// g:WGraph, s:始点(0-indexed)
V<> dijkstra(WGraph g, int s) {
  V<> dist(SZ(g), INF);
  priority_queue<P, vector<P>, greater<P>> que;
  dist[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (dist[v] < p.first)
      continue;
    REP(i, SZ(g[v])) {
      Edge e = g[v][i];
      if (dist[e.to] > dist[v] + e.cost) {
        dist[e.to] = dist[v] + e.cost;
        que.push(P(dist[e.to], e.to));
      }
    }
  }
  return dist;
}

// grid
int h, w;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {
    1, 1, 0, -1, -1, -1, 0, 1,
};
int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
inline bool inside(int x, int y) {
  return x >= 0 and x < h and y >= 0 and y < w;
}

struct UnionFind {
public:
  vector<int> par;
  UnionFind(int n) { par = vector<int>(n, -1); }
  int root(int x) {
    if (par[x] < 0)
      return x;
    return par[x] = root(par[x]);
  }
  bool issame(int x, int y) { return root(x) == root(y); }
  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }
  int size(int x) { return -par[root(x)]; }
};

int gtoi(int x, int y) { return x * h + y; }

int main() {
  ios;
  cin >> h >> w;
  LL(sx, sy, gx, gy);
  sx--;
  sy--;
  gx--;
  gy--;
  VVEC(char, a, h, w);
  UnionFind uf(h * w);
  REP(i, h) REP(j, w) {
    if (a[i][j] == '#')
      continue;
    REP(k, 4) {
      int nx = i + dx[k], ny = j + dy[k];
      if (inside(nx, ny) and a[nx][ny] == '.')
        uf.merge(gtoi(i, j), gtoi(nx, ny));
    }
  }
  WGraph G(h * w);
  REP(i, h) REP(j, w) {
    if (a[i][j] == '#')
      continue;
    for (int s = -2; s <= 2; s++)
      for (int t = -2; t <= 2; t++) {
        int nx = i + s, ny = j + t;
        if (inside(nx, ny) and uf.root(gtoi(i, j)) != uf.root(gtoi(nx, ny))) {
          if (a[nx][ny] == '#')
            continue;
          G[uf.root(gtoi(i, j))].pb({uf.root(gtoi(nx, ny)), 1});
          G[uf.root(gtoi(nx, ny))].pb({uf.root(gtoi(i, j)), 1});
        }
      }
  }
  V<> dist = dijkstra(G, uf.root(gtoi(sx, sy)));
  if (dist[uf.root(gtoi(gx, gy))] != INF)
    print(dist[uf.root(gtoi(gx, gy))]);
  else
    print(-1);
}