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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(), (a).end()
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
// s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ
// vector<vector<ll>> dp(n,vector<ll>(n))
// exist x map o setconst ll MOD = 1e9+7;return fac[n]*(finv[k]*finv[n-k] % MOD)
// % MOD; 2-dim:vector<vector<Type>> vv(n, vector<Type>(m, d));
// 3-dim:vector<vector<vector<Type>>> vvv(n, vector<vector<Type>>(m,
// vector<Type>(l, d)));

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int h, w;
  cin >> h >> w;
  int m;
  cin >> m;
  vector<int> num1(h + 1, 0), num2(w + 1, 0);
  vector<P> v;
  map<P, bool> mp;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    num1[x]++;
    num2[y]++;
    v.pb({x, y});
    mp[{x, y}] = 1;
  }

  int mxm = 0;
  int nx = 0, ny = 0;
  rep(i, h + 1) {
    if (mxm < num1[i]) {
      mxm = num1[i];
      nx = i;
    }
  }
  mxm = 0;
  rep(i, w + 1) {
    if (mxm < num2[i]) {
      mxm = num2[i];
      ny = i;
    }
  }
  int ans = 0;
  int s1 = 0, s2 = 0;
  rep(i, m) {
    if (v[i].fi == nx || v[i].se == ny)
      ans++;
    if (v[i].fi == nx && v[i].se == ny)
      s1++;
  }

  // cout << nx << " " << ny << endl;

  int s3 = 0;
  rep(i, h + 1) {
    //{i,ny}
    if (num1[i] == num1[nx] && mp[{i, ny}] == 0)
      s3++;
  }

  if (s3 > 0)
    s2 = 1;
  s3 = 0;
  rep(i, w + 1) {
    //{nx,i}
    if (num2[i] == num2[ny] && mp[{nx, i}] == 0)
      s3++;
  }
  if (s3 > 0)
    s2 = 1;

  if (s1 == 1 && s2 == 1 && ans != m)
    ans++;

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(), (a).end()
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
// s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ
// vector<vector<ll>> dp(n,vector<ll>(n))
// exist x map o setconst ll MOD = 1e9+7;return fac[n]*(finv[k]*finv[n-k] % MOD)
// % MOD; 2-dim:vector<vector<Type>> vv(n, vector<Type>(m, d));
// 3-dim:vector<vector<vector<Type>>> vvv(n, vector<vector<Type>>(m,
// vector<Type>(l, d)));

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int h, w;
  cin >> h >> w;
  int m;
  cin >> m;
  vector<int> num1(h + 1, 0), num2(h + 2, 0);
  vector<P> v;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    num1[x]++;
    num2[y]++;
    v.pb({x, y});
  }
  int mxm = 0;
  int nx = 0, ny = 0;
  rep(i, h + 1) {
    if (mxm < num1[i]) {
      mxm = num1[i];
      nx = i;
    }
  }
  mxm = 0;
  rep(i, w + 1) {
    if (mxm < num2[i]) {
      mxm = num2[i];
      ny = i;
    }
  }
  int ans = 0;
  int s1 = 0, s2 = 0;
  rep(i, m) {
    if (v[i].fi == nx || v[i].se == ny)
      ans++;
    if (v[i].fi == nx)
      s1++;
    if (v[i].se == ny)
      s2++;
  }
  // cout << nx << " " << ny << endl;
  if (s1 == 1 || s2 == 1) {
    if (ans != m)
      ans++;
  }
  cout << ans << endl;
}