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

#include <bits/stdc++.h>
using namespace std;

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;
typedef tuple<long long, long long, long long> tllint;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const llint INF = 1<<21;
// static const llint MOD = 1e9 + 7;

bool compPair(const pint &arg1, const pint &arg2) {
  return arg1.first > arg2.first;
}
bool compTuple(const tllint &arg1, const tllint &arg2) {
  return get<2>(arg1) > get<2>(arg2);
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

int main(void) {
  int h, w, m;
  cin >> h >> w >> m;

  int htmp, wtmp;
  vector<pint> g;
  vector<int> hbomb(h);
  vector<int> wbomb(w);
  for (int im = 0; im < m; ++im) {
    cin >> htmp >> wtmp;
    --htmp;
    --wtmp;
    g.push_back({htmp, wtmp});
    ++hbomb[htmp];
    ++wbomb[wtmp];
  }

  int hmax = 0, wmax = 0;
  vector<int> hplace;
  vector<int> wplace;
  for (auto ih : hbomb)
    chmax(hmax, ih);
  for (auto iw : wbomb)
    chmax(wmax, iw);
  for (int ih = 0; ih < h; ++ih) {
    if (hbomb[ih] == hmax)
      hplace.push_back(ih);
  }
  for (int iw = 0; iw < w; ++iw) {
    if (wbomb[iw] == wmax)
      wplace.push_back(iw);
  }

  int cnt = 0;
  for (auto ig : g) {
    if (hbomb[ig.first] == hmax && wbomb[ig.second] == wmax)
      ++cnt;
  }
  int ans = hmax + wmax;
  if (cnt == hplace.size() * wplace.size())
    --ans;
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;
typedef tuple<long long, long long, long long> tllint;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const llint INF = 1<<21;
// static const llint MOD = 1e9 + 7;

bool compPair(const pint &arg1, const pint &arg2) {
  return arg1.first > arg2.first;
}
bool compTuple(const tllint &arg1, const tllint &arg2) {
  return get<2>(arg1) > get<2>(arg2);
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

int main(void) {
  int h, w, m;
  cin >> h >> w >> m;

  int htmp, wtmp;
  vector<vector<bool>> g(h, vector<bool>(w, false));
  vector<int> hbomb(h);
  vector<int> wbomb(w);
  for (int im = 0; im < m; ++im) {
    cin >> htmp >> wtmp;
    --htmp;
    --wtmp;
    g[htmp][wtmp] = true;
    ++hbomb[htmp];
    ++wbomb[wtmp];
  }

  int hmax = 0, wmax = 0;
  vector<int> hplace;
  vector<int> wplace;
  for (auto ih : hbomb)
    chmax(hmax, ih);
  for (auto iw : wbomb)
    chmax(wmax, iw);
  for (int ih = 0; ih < h; ++ih) {
    if (hbomb[ih] == hmax)
      hplace.push_back(ih);
  }
  for (int iw = 0; iw < w; ++iw) {
    if (wbomb[iw] == wmax)
      wplace.push_back(iw);
  }

  bool flg = true;
  for (auto ih : hplace) {
    for (auto iw : wplace) {
      if (g[ih][iw] == false) {
        flg = false;
        break;
      }
    }
    if (!flg)
      break;
  }

  int ans = hmax + wmax;
  if (flg)
    --ans;
  cout << ans << endl;
  return 0;
}