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

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++) // n回繰り返す
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, n) for (int i = 1; i <= (int)n; i++) // i=1からi=nまで
#define RREP(i, n) for (int i = n; i >= 1; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using graph = vector<vector<int>>;
const ll mod = pow(10, 9) + 7;
const int INF = 1e9;
const ll LINF = 1e18;
const double pi = 3.14159265358979323846;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<int> BH(H + 1, 0), BW(W + 1, 0);
  map<P, int> b;
  rep(i, M) {
    int h, w;
    cin >> h >> w;
    BH[h]++;
    BW[w]++;
    P a = make_pair(h, w);
    b[a]++;
  }
  int maxh = max_element(BH.begin(), BH.end()) - BH.begin();
  int maxw = max_element(BW.begin(), BW.end()) - BW.begin();
  vector<int> bh, bw;
  REP(i, H) {
    if (BH[maxh] == BH[i]) {
      bh.push_back(i);
    }
  }
  REP(i, W) {
    if (BW[maxw] == BW[i]) {
      bw.push_back(i);
    }
  }
  for (int &e : bh) {
    for (int &el : bw) {
      if (b[make_pair(e, el)] == 0) {
        cout << BH[maxh] + BW[maxw] << endl;
        return 0;
      }
    }
  }

  cout << BH[maxh] + BW[maxw] - b[make_pair(maxh, maxw)] << endl;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++) // n回繰り返す
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, n) for (int i = 1; i <= (int)n; i++) // i=1からi=nまで
#define RREP(i, n) for (int i = n; i >= 1; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using graph = vector<vector<int>>;
const ll mod = pow(10, 9) + 7;
const int INF = 1e9;
const ll LINF = 1e18;
const double pi = 3.14159265358979323846;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<int> BH(H + 1, 0), BW(W + 1, 0);
  map<P, int> b;
  rep(i, M) {
    int h, w;
    cin >> h >> w;
    BH[h]++;
    BW[w]++;
    P a = make_pair(h, w);
    b[a]++;
  }
  int maxh = max_element(BH.begin(), BH.end()) - BH.begin();
  int maxw = max_element(BW.begin(), BW.end()) - BW.begin();
  vector<int> bh, bw;
  REP(i, H) {
    if (BH[maxh] == BH[i]) {
      bh.push_back(i);
    }
  }
  REP(i, W) {
    if (BW[maxw] == BH[i]) {
      bw.push_back(i);
    }
  }
  for (int &e : bh) {
    for (int &el : bh) {
      if (b[make_pair(e, el)] == 0) {
        cout << BH[maxh] + BW[maxw] << endl;
        return 0;
      }
    }
  }

  cout << BH[maxh] + BW[maxw] - b[make_pair(maxh, maxw)] << endl;
}