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

//
// AtCoder Beginner Contest 176
// E
//

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SIZE(x) (int)(x).size()
#define SIZEL(x) (ll)(x).size()
#define MSG(a) cout << #a << " " << a << endl;

using ll = long long;
using ld = long double;
using Graph = vector<vector<int>>;

const double PI = 3.14159265358979323846;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll gcd(ll x, ll y) { return (x % y) ? gcd(y, x % y) : y; } // 最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }           // 最小公倍数

int main() {
  ll H, W, M;
  cin >> H >> W >> M;

  vector<int> hCnt(H, 0);
  vector<int> wCnt(W, 0);
  vector<pair<int, int>> mList;
  REP(m, M) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    hCnt[h]++;
    wCnt[w]++;
    mList.emplace_back(h, w);
  }
  int hMax = *max_element(ALL(hCnt));
  int wMax = *max_element(ALL(wCnt));

  int ans = hMax + wMax;

  ll pCnt = count(ALL(hCnt), hMax) * count(ALL(wCnt), wMax);

  ll pCheck = 0;
  REP(m, M) {
    if (hCnt[mList[m].first] + wCnt[mList[m].second] == ans)
      pCheck++;
  }
  if (pCnt == pCheck)
    cout << ans - 1 << endl;
  else
    cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

//
// AtCoder Beginner Contest 176
// E
//

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SIZE(x) (int)(x).size()
#define SIZEL(x) (ll)(x).size()
#define MSG(a) cout << #a << " " << a << endl;

using ll = long long;
using ld = long double;
using Graph = vector<vector<int>>;

const double PI = 3.14159265358979323846;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll gcd(ll x, ll y) { return (x % y) ? gcd(y, x % y) : y; } // 最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }           // 最小公倍数

int main() {
  ll H, W, M;
  cin >> H >> W >> M;

  vector<pair<ll, ll>> hCnt(H);
  vector<pair<ll, ll>> wCnt(W);
  vector<vector<bool>> bomMap(H, vector<bool>(W));
  REP(m, M) {
    ll h, w;
    cin >> h >> w;
    h--;
    w--;
    bomMap[h][w] = true;
    hCnt[h].first++;
    hCnt[h].second = h;

    wCnt[w].first++;
    wCnt[w].second = w;
  }
  /*
      REP(h,H){
          cout << hCnt[h].second << ":" << hCnt[h].first << endl;
      }

      REP(w,W){
          cout << wCnt[w].second << ":" << wCnt[w].first << endl;
      }
  */
  sort(hCnt.rbegin(), hCnt.rend());
  sort(wCnt.rbegin(), wCnt.rend());
  /*
      REP(h,H){
          cout << hCnt[h].second << ":" << hCnt[h].first << endl;
      }

      REP(w,W){
          cout << wCnt[w].second << ":" << wCnt[w].first << endl;
      }
  */
  int dep = 1;

  REP(h, H) {
    if (hCnt[h].first != hCnt[0].first)
      break;
    REP(w, W) {
      if (wCnt[w].first != wCnt[0].first)
        break;

      if (bomMap[hCnt[h].second][wCnt[w].second] == false)
        dep = 0;
    }
  }

  cout << hCnt[0].first + wCnt[0].first - dep << endl;

  return 0;
}