Problem p00492 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 25;
const ll INF = 1ll << 50;
const double pi = acos(-1);
const double eps = 1e-8;
const vi emp;

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

int h, w, ans;
vvb a;
vp p;

int func(vp q) {
  int ans = 0;
  vvb b(h, vb(w));
  for (int i = 0; i < q.size(); i++) {
    int x = q[i].first, y = q[i].second;
    int tmp = 6;
    b[x][y] = 1;
    for (int j = 0; j < 6; j++) {
      int cx = x + dx[j], cy = y + dy[x % 2][j];
      if (cx >= 0 && cx < h && cy >= 0 && cy < w && b[cx][cy])
        tmp -= 2;
    }
    ans += tmp;
  }
  return ans;
}

void bfs(int x, int y) {
  queue<P> que;
  que.push(P(x, y));
  while (!que.empty()) {
    P q = que.front();
    que.pop();
    a[q.first][q.second] = 1;
    for (int i = 0; i < 6; i++) {
      int cx = q.first + dx[i], cy = q.second + dy[q.first % 2][i];
      if (cx >= 0 && cx < h && cy >= 0 && cy < w && !a[cx][cy])
        que.push(P(cx, cy));
    }
  }
}

void dfs(int x, int y) {
  a[x][y] = 1;
  for (int i = 0; i < 6; i++) {
    int cx = x + dx[i], cy = y + dy[x % 2][i];
    if (cx >= 0 && cx < h && cy >= 0 && cy < w && !a[cx][cy])
      dfs(cx, cy);
  }
}

int main() {
  cin >> w >> h;
  a = vvb(h, vb(w));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      int tmp;
      cin >> tmp;
      if (tmp) {
        a[i][j] = 1;
        p.push_back(P(i, j));
      }
    }
  ans = func(p);
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      if ((!i || !j || i == h - 1 || j == w - 1) && !a[i][j])
        dfs(i, j);
    }
  vp q;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (!a[i][j])
        q.push_back(P(i, j));
  cout << ans - func(q) << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 25;
const ll INF = 1ll << 50;
const double pi = acos(-1);
const double eps = 1e-8;
const vi emp;

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

int h, w, ans;
vvb a;
vp p;

int func(vp q) {
  int ans = 0;
  vvb b(h, vb(w));
  for (int i = 0; i < q.size(); i++) {
    int x = q[i].first, y = q[i].second;
    int tmp = 6;
    b[x][y] = 1;
    for (int j = 0; j < 6; j++) {
      int cx = x + dx[j], cy = y + dy[x % 2][j];
      if (cx >= 0 && cx < h && cy >= 0 && cy < w && b[cx][cy])
        tmp -= 2;
    }
    ans += tmp;
  }
  return ans;
}

void bfs(int x, int y) {
  queue<P> que;
  que.push(P(x, y));
  while (!que.empty()) {
    P q = que.front();
    que.pop();
    a[q.first][q.second] = 1;
    for (int i = 0; i < 6; i++) {
      int cx = q.first + dx[i], cy = q.second + dy[q.first % 2][i];
      if (cx >= 0 && cx < h && cy >= 0 && cy < w && !a[cx][cy])
        que.push(P(cx, cy));
    }
  }
}

int main() {
  cin >> w >> h;
  a = vvb(h, vb(w));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      int tmp;
      cin >> tmp;
      if (tmp) {
        a[i][j] = 1;
        p.push_back(P(i, j));
      }
    }
  ans = func(p);
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      if ((!i || !j || i == h - 1 || j == w - 1) && !a[i][j])
        bfs(i, j);
    }
  vp q;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (!a[i][j])
        q.push_back(P(i, j));
  cout << ans - func(q) << endl;
}