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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef int itn;
const ll LINF = 1e18;
const int INF = 1e9;

// マクロ定義
#define vvint(vec, n, m, l)                                                    \\
  vector<vector<int>> vec(n, vector<int>(m, l)); // lで初期化
#define vvll(vec, n, m, l) vector<vector<ll>> vec(n, vector<ll>(m, l));
#define vint vector<int>
#define pint pair<int, int>
#define rep(i, a) for (ll i = 0; i < (a); i++)
#define all(x) (x).begin(), (x).end()
#define debug system("pause") // デバッグ用
#define ret return 0

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using Graph = vector<vector<ll>>;

#define ketasitei setprecision(15) // 15桁表示

const ll MOD = 1000000007;
const double PI = 3.1415926535897932;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll h, w, m;
  cin >> h >> w >> m;
  vector<ll> x(m), y(m);
  vector<ll> xx(h, 0), yy(w, 0);
  rep(i, m) {
    cin >> x[i] >> y[i];
    xx[x[i] - 1]++;
    yy[y[i] - 1]++;
  }

  ll maxx = 0, maxy = 0;

  rep(i, h) {
    if (maxx < xx[i]) {
      maxx = xx[i];
    }
  }
  ll kohox = 0;
  rep(i, h) {
    if (maxx == xx[i])
      kohox++;
  }

  rep(i, w) {
    if (maxy < yy[i])
      maxy = yy[i];
  }
  ll kohoy = 0;
  rep(i, w) {
    if (maxy == yy[i])
      kohoy++;
  }
  ll ans = kohox * kohoy;

  rep(i, m) {
    if (maxx + maxy == xx[x[i] - 1] + yy[y[i] - 1])
      ans--;
  }

  if (ans)
    cout << maxx + maxy << endl;
  else
    cout << maxx + maxy - 1 << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef int itn;
const ll LINF = 1e18;
const int INF = 1e9;

// マクロ定義
#define vvint(vec, n, m, l)                                                    \\
  vector<vector<int>> vec(n, vector<int>(m, l)); // lで初期化
#define vvll(vec, n, m, l) vector<vector<ll>> vec(n, vector<ll>(m, l));
#define vint vector<int>
#define pint pair<int, int>
#define rep(i, a) for (ll i = 0; i < (a); i++)
#define all(x) (x).begin(), (x).end()
#define debug system("pause") // デバッグ用
#define ret return 0

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using Graph = vector<vector<ll>>;

#define ketasitei setprecision(15) // 15桁表示

const ll MOD = 1000000007;
const double PI = 3.1415926535897932;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll h, w, m;
  cin >> h >> w >> m;
  vector<ll> x(m), y(m);
  vector<ll> xx(h, 0), yy(w, 0);
  rep(i, m) {
    cin >> x[i] >> y[i];
    xx[x[i] - 1]++;
    yy[y[i] - 1]++;
  }

  ll maxx = 0, maxy = 0;

  rep(i, h) {
    if (maxx < xx[i]) {
      maxx = xx[i];
    }
  }
  ll kohox = 0;
  rep(i, h) {
    if (maxx == x[i])
      kohox++;
  }

  rep(i, w) {
    if (maxy < yy[i])
      maxy = yy[i];
  }
  ll kohoy = 0;
  rep(i, w) {
    if (maxy == yy[i])
      kohoy++;
  }
  ll ans = kohox * kohoy;

  rep(i, m) {
    if (maxx + maxy == xx[x[i] - 1] + yy[y[i] - 1])
      ans--;
  }

  if (ans)
    cout << maxx + maxy << endl;
  else
    cout << maxx + maxy - 1 << endl;

  return 0;
}