Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
bool custom1(pair<int, int> a, pair<int, int> b) { return (a.first < b.first); }
bool custom2(pair<int, int> a, pair<int, int> b) {
  return (a.second < b.second);
}

const int MAX = 200005;
const long long MOD = 1000000007;
const long long MODMOD = 998244353;
const double PI = acos(-1);

long long fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
struct UnionFind {
  vector<int> p;
  vector<int> s;
  void init(int n) {
    p.resize(n, -1);
    s.resize(n, 1);
  }
  int find(int n) {
    if (p[n] == -1)
      return n;
    else
      return find(p[n]);
  }
  int size(int n) { return s[find(n)]; }
  void unite(int n, int m) {
    int pn = find(n);
    int pm = find(m);
    if (pn == pm)
      return;
    if (s[pn] > s[pm]) {
      s[pn] = s[pn] + s[pm];
      p[pm] = pn;
    } else {
      s[pm] = s[pm] + s[pn];
      p[pn] = pm;
    }
  }
};

long long gcd(long long a, long long b) {
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}
int main() {
  int h, w;
  cin >> h >> w;
  vector<P> black;
  vector<vector<char>> g(h + 2, vector<char>(w + 2));
  rep(i, h) rep(j, w) {
    cin >> g[i + 1][j + 1];
    if (g[i + 1][j + 1] == '#')
      black.emplace_back(make_pair(i + 1, j + 1));
  }
  vector<vector<int>> depth(h + 2, vector<int>(w + 2, 100100100));
  rep(i, black.size()) {
    queue<P> que;
    que.emplace(black[i]);
    depth[black[i].first][black[i].second] = 0;
    while (!que.empty()) {
      P p = que.front();
      que.pop();
      int r = p.first, c = p.second;
      int d = depth[r][c];
      if (g[r][c] == '#')
        depth[r][c] = 0;
      if (g[r + 1][c] == '.' && depth[r + 1][c] > d + 1) {
        depth[r + 1][c] = d + 1;
        que.emplace(make_pair(r + 1, c));
      }
      if (g[r - 1][c] == '.' && depth[r - 1][c] > d + 1) {
        depth[r - 1][c] = d + 1;
        que.emplace(make_pair(r - 1, c));
      }
      if (g[r][c + 1] == '.' && depth[r][c + 1] > d + 1) {
        depth[r][c + 1] = d + 1;
        que.emplace(make_pair(r, c + 1));
      }
      if (g[r][c - 1] == '.' && depth[r][c - 1] > d + 1) {
        depth[r][c - 1] = d + 1;
        que.emplace(make_pair(r, c - 1));
      }
    }
  }
  int ans = 0;
  rep(i, h) rep(j, w) chmax(ans, depth[i + 1][j + 1]);
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
bool custom1(pair<int, int> a, pair<int, int> b) { return (a.first < b.first); }
bool custom2(pair<int, int> a, pair<int, int> b) {
  return (a.second < b.second);
}

const int MAX = 200005;
const long long MOD = 1000000007;
const long long MODMOD = 998244353;
const double PI = acos(-1);

long long fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
struct UnionFind {
  vector<int> p;
  vector<int> s;
  void init(int n) {
    p.resize(n, -1);
    s.resize(n, 1);
  }
  int find(int n) {
    if (p[n] == -1)
      return n;
    else
      return find(p[n]);
  }
  int size(int n) { return s[find(n)]; }
  void unite(int n, int m) {
    int pn = find(n);
    int pm = find(m);
    if (pn == pm)
      return;
    if (s[pn] > s[pm]) {
      s[pn] = s[pn] + s[pm];
      p[pm] = pn;
    } else {
      s[pm] = s[pm] + s[pn];
      p[pn] = pm;
    }
  }
};

long long gcd(long long a, long long b) {
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}
int main() {
  int h, w;
  cin >> h >> w;
  vector<P> black;
  vector<vector<char>> g(h + 2, vector<char>(w + 2));
  rep(i, h) rep(j, w) {
    cin >> g[i + 1][j + 1];
    if (g[i + 1][j + 1] == '#')
      black.emplace_back(make_pair(i + 1, j + 1));
  }
  vector<vector<int>> depth(h + 2, vector<int>(w + 2, 100100100));
  queue<P> que;
  rep(i, black.size()) que.emplace(black[i]);
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int r = p.first, c = p.second;
    if (g[r][c] == '#')
      depth[r][c] = 0;
    int d = depth[r][c];
    if (g[r + 1][c] == '.' && depth[r + 1][c] > d + 1) {
      depth[r + 1][c] = d + 1;
      que.emplace(make_pair(r + 1, c));
    }
    if (g[r - 1][c] == '.' && depth[r - 1][c] > d + 1) {
      depth[r - 1][c] = d + 1;
      que.emplace(make_pair(r - 1, c));
    }
    if (g[r][c + 1] == '.' && depth[r][c + 1] > d + 1) {
      depth[r][c + 1] = d + 1;
      que.emplace(make_pair(r, c + 1));
    }
    if (g[r][c - 1] == '.' && depth[r][c - 1] > d + 1) {
      depth[r][c - 1] = d + 1;
      que.emplace(make_pair(r, c - 1));
    }
  }
  int ans = 0;
  rep(i, h) rep(j, w) chmax(ans, depth[i + 1][j + 1]);
  cout << ans << endl;
}