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

#include <bits/stdc++.h>

#define X first
#define Y second
#define pb push_back
#define eb emplace_back
#define mkp make_pair
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)

using namespace std;

using I = int;
using L = long;
using F = float;
using D = double;
using S = string;
using SS = stringstream;
using C = char;
using LL = long long;
using VI = vector<int>;
using VL = vector<long>;
using VF = vector<float>;
using VS = vector<string>;
using VVL = vector<vector<long>>;
using PLL = pair<long, long>;
using PLF = pair<long, float>;
using PLS = pair<long, string>;
using PSL = pair<string, long>;
using PSF = pair<string, float>;
using PSS = pair<string, string>;
using MLL = map<long, long>;
using MLF = map<long, float>;
using MLS = map<long, string>;
using MSL = map<string, long>;
using MSF = map<string, float>;
using MSS = map<string, string>;

const long kMax2 = 109;
const long kMax3 = 1009;
const long kMax4 = 10009;
const long kMax5 = 100009;
const long kMax6 = 1000009;
const long kInfI = 0x3F3F3F3F;
const float kInfF = 1e15;

L w, h;
VVL mp(kMax2 + 1, VL(kMax2 + 1, -1));
VVL is_visited(kMax2 + 1, VL(kMax2 + 1, 0));
VVL is_visited2(kMax2 + 1, VL(kMax2 + 1, 0));
long node_num = 0;
long edge_num = 0;

template <class T> void PrintVector(vector<T> &v, unsigned n) {
  rep(i, 0, n - 1) { cout << v[i] << " "; }
  cout << endl;
}

vector<PLL> Adjacencies(L i, L j) {
  vector<PLL> result;
  if (i % 2 == 0) {
    if (j > 1) {
      result.eb(i - 1, j - 1);
      result.eb(i, j - 1);
      if (i < h) {
        result.eb(i + 1, j - 1);
      }
    }
    result.eb(i - 1, j);
    if (j < w) {
      result.eb(i, j + 1);
    }
    if (i < h) {
      result.eb(i + 1, j);
    }
  } else {
    if (i > 1) {
      result.eb(i - 1, j);
    }
    if (j > 1) {
      result.eb(i, j - 1);
    }
    if (i < h) {
      result.eb(i + 1, j);
    }
    if (j < w) {
      if (i > 1) {
        result.eb(i - 1, j + 1);
      }
      result.eb(i, j + 1);
      if (i < h) {
        result.eb(i + 1, j + 1);
      }
    }
  }

  return result;
}

void FirstDFSVisit(L i, L j) {
  is_visited[i][j] = 1;
  for (const auto &e : Adjacencies(i, j)) {
    if (mp[e.X][e.Y] == 0 && is_visited[e.X][e.Y] == 0) {
      FirstDFSVisit(e.X, e.Y);
    }
  }
}

void FirstDFS() {
  rep(i, 1, h) {
    if (mp[i][1] == 0 && is_visited[i][1] == 0) {
      FirstDFSVisit(i, 1);
    }
    if (mp[i][w] == 0 && is_visited[i][w] == 0) {
      FirstDFSVisit(i, w);
    }
  }
  rep(j, 1, w) {
    if (mp[1][j] == 0 && is_visited[1][j] == 0) {
      FirstDFSVisit(1, j);
    }
    if (mp[h][j] == 0 && is_visited[h][j] == 0) {
      FirstDFSVisit(h, j);
    }
  }
}

void SecondDFSVisit(L i, L j) {
  ++node_num;
  is_visited2[i][j] = 1;
  for (const auto &e : Adjacencies(i, j)) {
    if (mp[e.X][e.Y] == 0) {
      continue;
    }
    ++edge_num;
    if (is_visited2[e.X][e.Y] == 0) {
      SecondDFSVisit(e.X, e.Y);
    }
  }
}

void SecondDFS() {
  rep(i, 1, h) {
    rep(j, 1, w) {
      if (mp[i][j] == 1 && is_visited2[i][j] == 0) {
        SecondDFSVisit(i, j);
      }
    }
  }
}

int main(int argc, char const *argv[]) {
  cin >> w >> h;

  rep(i, 1, h) {
    rep(j, 1, w) { cin >> mp[i][j]; }
  }

  FirstDFS();
  rep(i, 1, h) {
    rep(j, 1, w) {
      if (mp[i][j] == 0 && is_visited[i][j] == 0) {
        mp[i][j] = 1;
      }
    }
  }
  SecondDFS();

  cout << 6 * node_num - edge_num << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define X first
#define Y second
#define pb push_back
#define eb emplace_back
#define mkp make_pair
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)

using namespace std;

using I = int;
using L = long;
using F = float;
using D = double;
using S = string;
using SS = stringstream;
using C = char;
using LL = long long;
using VI = vector<int>;
using VL = vector<long>;
using VF = vector<float>;
using VS = vector<string>;
using VVL = vector<vector<long>>;
using PLL = pair<long, long>;
using PLF = pair<long, float>;
using PLS = pair<long, string>;
using PSL = pair<string, long>;
using PSF = pair<string, float>;
using PSS = pair<string, string>;
using MLL = map<long, long>;
using MLF = map<long, float>;
using MLS = map<long, string>;
using MSL = map<string, long>;
using MSF = map<string, float>;
using MSS = map<string, string>;

const long kMax2 = 8;
const long kMax3 = 1009;
const long kMax4 = 10009;
const long kMax5 = 100009;
const long kMax6 = 1000009;
const long kInfI = 0x3F3F3F3F;
const float kInfF = 1e15;

L w, h;
VVL mp(kMax2 + 1, VL(kMax2 + 1, -1));
VVL is_visited(kMax2 + 1, VL(kMax2 + 1, 0));
VVL is_visited2(kMax2 + 1, VL(kMax2 + 1, 0));
long node_num = 0;
long edge_num = 0;

template <class T> void PrintVector(vector<T> &v, unsigned n) {
  rep(i, 0, n - 1) { cout << v[i] << " "; }
  cout << endl;
}

vector<PLL> Adjacencies(L i, L j) {
  vector<PLL> result;
  if (i % 2 == 0) {
    if (j > 1) {
      result.eb(i - 1, j - 1);
      result.eb(i, j - 1);
      if (i < h) {
        result.eb(i + 1, j - 1);
      }
    }
    result.eb(i - 1, j);
    if (j < w) {
      result.eb(i, j + 1);
    }
    if (i < h) {
      result.eb(i + 1, j);
    }
  } else {
    if (i > 1) {
      result.eb(i - 1, j);
    }
    if (j > 1) {
      result.eb(i, j - 1);
    }
    if (i < h) {
      result.eb(i + 1, j);
    }
    if (j < w) {
      if (i > 1) {
        result.eb(i - 1, j + 1);
      }
      result.eb(i, j + 1);
      if (i < h) {
        result.eb(i + 1, j + 1);
      }
    }
  }

  return result;
}

void FirstDFSVisit(L i, L j) {
  is_visited[i][j] = 1;
  for (const auto &e : Adjacencies(i, j)) {
    if (mp[e.X][e.Y] == 0 && is_visited[e.X][e.Y] == 0) {
      FirstDFSVisit(e.X, e.Y);
    }
  }
}

void FirstDFS() {
  rep(i, 1, h) {
    if (mp[i][1] == 0 && is_visited[i][1] == 0) {
      FirstDFSVisit(i, 1);
    }
    if (mp[i][w] == 0 && is_visited[i][w] == 0) {
      FirstDFSVisit(i, w);
    }
  }
  rep(j, 1, w) {
    if (mp[1][j] == 0 && is_visited[1][j] == 0) {
      FirstDFSVisit(1, j);
    }
    if (mp[h][j] == 0 && is_visited[h][j] == 0) {
      FirstDFSVisit(h, j);
    }
  }
}

void SecondDFSVisit(L i, L j) {
  ++node_num;
  is_visited2[i][j] = 1;
  for (const auto &e : Adjacencies(i, j)) {
    if (mp[e.X][e.Y] == 0) {
      continue;
    }
    ++edge_num;
    if (is_visited2[e.X][e.Y] == 0) {
      SecondDFSVisit(e.X, e.Y);
    }
  }
}

void SecondDFS() {
  rep(i, 1, h) {
    rep(j, 1, w) {
      if (mp[i][j] == 1 && is_visited2[i][j] == 0) {
        SecondDFSVisit(i, j);
      }
    }
  }
}

int main(int argc, char const *argv[]) {
  cin >> w >> h;
  mp.resize(w + 1);
  is_visited.resize(w + 1);
  is_visited2.resize(w + 1);

  rep(i, 1, h) {
    rep(j, 1, w) { cin >> mp[i][j]; }
  }

  FirstDFS();
  rep(i, 1, h) {
    rep(j, 1, w) {
      if (mp[i][j] == 0 && is_visited[i][j] == 0) {
        mp[i][j] = 1;
      }
    }
  }
  SecondDFS();

  cout << 6 * node_num - edge_num << endl;
  return 0;
}