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

#include <bits/stdc++.h>
using namespace std;
// 型名省略
typedef long long ll;
typedef vector<long long> vl;
typedef vector<vector<long long>> vvl;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef vector<string> vs;
// 定数
const ll MOD = 1e9 + 7;
// マクロ
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define rep2(i, x, n) for (ll i = (ll)x; i < (ll)n; i++)
#define all(A) A.begin(), A.end()
// 正誤判定
void Yes(bool ans) { cout << (ans ? "Yes" : "No") << endl; }
void YES(bool ans) { cout << (ans ? "YES" : "NO") << endl; }

// main関数
int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  vector<pair<ll, ll>> obj(m);
  vl row(h, 0), col(w, 0); // 各行、列の爆破対象の個数
  rep(i, m) {
    cin >> obj[i].first >> obj[i].second;
    obj[i].first--;
    obj[i].second--;
    row[obj[i].first]++;
    col[obj[i].second]++;
  }

  ll mrow = 0, mcol = 0; // 爆破対象数が最大である行、列の爆破対象数
  vl mrowIndex = {}, mcolIndex = {}; // ↑のインデックス
  rep(i, h) {
    if (mrow < row[i]) {
      mrow = row[i];
      mrowIndex = {i};
    } else if (mrow == row[i]) {
      mrowIndex.push_back(i);
    }
  }
  rep(i, w) {
    if (mcol < col[i]) {
      mcol = col[i];
      mcolIndex = {i};
    } else if (mcol == col[i]) {
      mcolIndex.push_back(i);
    }
  }

  ll mrs = mrowIndex.size();
  ll mcs = mcolIndex.size();
  // // 確認
  // rep(i,mrs) cout << "mrowIndex[" << i <<"]: " << mrowIndex[i] << endl;
  // rep(i,mcs) cout << "mcolIndex[" << i <<"]: " << mcolIndex[i] << endl;

  ll maxcount = 0; // 重なる点の個数
  rep(i, m) {
    if (row[obj[i].first] == mrow && col[obj[i].second] == mcol) {
      maxcount++;
    }
  }
  if (maxcount == mrs * mcs) {
    cout << mrow + mcol - 1 << endl;
  } else {
    cout << mrow + mcol << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// 型名省略
typedef long long ll;
typedef vector<long long> vl;
typedef vector<vector<long long>> vvl;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef vector<string> vs;
// 定数
const ll MOD = 1e9 + 7;
// マクロ
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define rep2(i, x, n) for (ll i = (ll)x; i < (ll)n; i++)
#define all(A) A.begin(), A.end()
// 正誤判定
void Yes(bool ans) { cout << (ans ? "Yes" : "No") << endl; }
void YES(bool ans) { cout << (ans ? "YES" : "NO") << endl; }

// main関数
int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  vector<pair<ll, ll>> obj(m);
  vl row(h, 0), col(w, 0); // 各行、列の爆破対象の個数
  rep(i, m) {
    cin >> obj[i].first >> obj[i].second;
    obj[i].first--;
    obj[i].second--;
    row[obj[i].first]++;
    col[obj[i].second]++;
  }

  ll mrow = 0, mcol = 0; // 爆破対象数が最大である行、列の爆破対象数
  vl mrowIndex = {}, mcolIndex = {}; // ↑のインデックス
  rep(i, h) {
    if (mrow < row[i]) {
      mrow = row[i];
      mrowIndex = {i};
    } else if (mrow == row[i]) {
      mrowIndex.push_back(i);
    }
  }
  rep(i, h) {
    if (mcol < col[i]) {
      mcol = col[i];
      mcolIndex = {i};
    } else if (mcol == col[i]) {
      mcolIndex.push_back(i);
    }
  }

  // // 確認
  // rep(i,mrowIndex.size()) cout << "mrowIndex[" << i <<"]: " << mrowIndex[i]
  // << endl; rep(i,mcolIndex.size()) cout << "mcolIndex[" << i <<"]: " <<
  // mcolIndex[i] << endl;

  if (mrowIndex.size() + mcolIndex.size() >
      m) { // このとき爆破対象が置かれていないマスに爆弾を置ける
    cout << mrow + mcol << endl;
    return 0;
  }

  rep(i, mrowIndex.size()) {
    rep(j, mcolIndex.size()) {
      bool flag = false; // 爆破対象＝爆弾が存在するか
      rep(k, m) {
        if (mrowIndex[i] == obj[k].first && mcolIndex[j] == obj[k].second) {
          flag = true;
        }
      }
      if (!flag) { // 爆弾と爆破対象が重ならずに最大化できるとき
        cout << mrow + mcol << endl;
        return 0;
      }
    }
  }

  // できないとき
  cout << mrow + mcol - 1 << endl;
}