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

#include "bits/stdc++.h"
#define rep(i, j) for (int i = 0; i < j; i++)
#define srep(i, j, k) for (int i = j; i < k; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using ull = unsigned long long;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

// エラトステネスの篩
vector<int> estora(vector<int> num) {
  int sel = 0;
  while (sel < num.size()) {
    int d = num[sel];
    for (int i = 0; i < num.size(); i++) {
      if (num[i] % d == 0 && num[i] != d && d != 1) {
        num.erase(num.begin() + i);
        i--;
      }
    }
    sel++;
  }
  return num;
}
/*ライブラリ最大公約数*/
// ユーグリッドの互除法
int gcd(int x, int y) {
  int num[3];
  num[0] = (x > y) ? x : y;
  num[1] = (x <= y) ? x : y;
  num[2] = num[0] % num[1];

  while (num[2]) {
    num[0] = num[1];
    num[1] = num[2];
    num[2] = num[0] % num[1];
  }

  return num[1];
}

/*素因数分解*/
// 自作ライブラリ
// iを常に素数にしておかないとうまくできない&&計算量ヤバイ
// エストラネスの篩で素数を振るっておく必要性がありそう
vector<ll> fac(ll n, vector<ll> num) {
  ll kazu = n;
  for (int i = 2; i <= n; i++) {
    while (1) {
      if (kazu % i != 0)
        break;

      kazu /= i;
      num[i]++;
    }
  }
  if (kazu == n && n != 1)
    num[n]++;
  return num;
  int ans = 0;
  if (kazu != 1)
    ans++;
  for (int i = 2; i <= sqrt(n); i++) {
    for (int j = 1; j <= num[i]; j++) {
      num[i] -= j;
      ans++;
    }
  }
  return num;
}
const int INF = 1001001001;

// mint用の定数 (10の9乗 + 7)
const int mod = 1000000007;

struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> dh(h), dw(w);
  set<P> abe;
  rep(i, m) {
    int n1, n2;
    cin >> n1 >> n2;
    n1--;
    n2--;
    dh[n1]++;
    dw[n2]++;
    abe.emplace(n1, n2);
  }
  int mh = 0, mw = 0;
  rep(i, h) mh = max(mh, dh[i]);
  rep(i, w) mw = max(mw, dw[i]);
  vector<int> is, js;
  rep(i, h) if (mh == dh[i]) is.emplace_back(i);
  rep(i, w) if (mw == dw[i]) js.emplace_back(i);

  int ans = mh + mw;
  for (int i : is)
    for (int j : js) {
      if (abe.count(P(i, j)))
        continue;
      cout << ans;
      return 0;
    }
  ans--;
  cout << ans << endl;
  ;
  return 0;
}


Error: WA

Fixed submission:

#include "bits/stdc++.h"
#define rep(i, j) for (int i = 0; i < j; i++)
#define srep(i, j, k) for (int i = j; i < k; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using ull = unsigned long long;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

// エラトステネスの篩
vector<int> estora(vector<int> num) {
  int sel = 0;
  while (sel < num.size()) {
    int d = num[sel];
    for (int i = 0; i < num.size(); i++) {
      if (num[i] % d == 0 && num[i] != d && d != 1) {
        num.erase(num.begin() + i);
        i--;
      }
    }
    sel++;
  }
  return num;
}
/*ライブラリ最大公約数*/
// ユーグリッドの互除法
int gcd(int x, int y) {
  int num[3];
  num[0] = (x > y) ? x : y;
  num[1] = (x <= y) ? x : y;
  num[2] = num[0] % num[1];

  while (num[2]) {
    num[0] = num[1];
    num[1] = num[2];
    num[2] = num[0] % num[1];
  }

  return num[1];
}

/*素因数分解*/
// 自作ライブラリ
// iを常に素数にしておかないとうまくできない&&計算量ヤバイ
// エストラネスの篩で素数を振るっておく必要性がありそう
vector<ll> fac(ll n, vector<ll> num) {
  ll kazu = n;
  for (int i = 2; i <= n; i++) {
    while (1) {
      if (kazu % i != 0)
        break;

      kazu /= i;
      num[i]++;
    }
  }
  if (kazu == n && n != 1)
    num[n]++;
  return num;
  int ans = 0;
  if (kazu != 1)
    ans++;
  for (int i = 2; i <= sqrt(n); i++) {
    for (int j = 1; j <= num[i]; j++) {
      num[i] -= j;
      ans++;
    }
  }
  return num;
}
const int INF = 1001001001;

// mint用の定数 (10の9乗 + 7)
const int mod = 1000000007;

struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> dh(h), dw(w);
  set<P> abe;
  rep(i, m) {
    int n1, n2;
    cin >> n1 >> n2;
    n1--;
    n2--;
    dh[n1]++;
    dh[n2]++;
    abe.emplace(n1, n2);
  }
  int mh = 0, mw = 0;
  rep(i, h) mh = max(mh, dh[i]);
  rep(i, w) mw = max(mw, dw[i]);
  vector<int> is, js;
  rep(i, h) if (mh == dh[i]) is.emplace_back(i);
  rep(i, w) if (mw == dw[i]) js.emplace_back(i);

  int ans = mh + mw;
  for (int i : is)
    for (int j : js) {
      if (abe.count(P(i, j)))
        continue;
      cout << ans;
      return 0;
    }
  ans--;
  cout << ans << endl;
  ;
  return 0;
}