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

#define _GLIBCXX_DEBUG
#include <algorithm> //next_permutation
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define dec(n) cout << fixed << setprecision(n);
#define large "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define small "abcdefghijklmnopqrstuvwxyz"
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vl>;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

const ll MOD = 1000000007;
const ll MAX = 2000001;

ll mod(ll a) { return a % MOD; }

ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

ll fac[MAX], finv[MAX], inv[MAX];

void nCrprep() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

ll nCr(ll n, ll r) {
  if (n < r)
    return 0;
  if (n < 0 || r < 0)
    return 0;
  return fac[n] * (finv[r] * finv[n - r] % MOD) % MOD;
}

ll nCrcheep(ll n, ll r) {
  if (r == 0)
    return 1;
  else if (r == 1)
    return n;
  else
    return nCrcheep(n - 1, r - 1) * n / r;
}

vector<pair<ll, ll>> prime_factorize(ll n) {
  vector<pair<ll, ll>> res;

  for (ll i = 2; i * i <= n; i++) {
    if (n % i != 0)
      continue;

    ll ex = 0;
    while (n % i == 0) {
      ex++;
      n /= i;
    }

    res.push_back({i, ex});
  }

  if (n != 1)
    res.push_back({n, 1});

  return res;
}

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  set<pair<ll, ll>> table;
  vl hcnt(h, 0);
  vl wcnt(w, 0);

  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    hcnt[a]++;
    wcnt[b]++;
    table.insert(make_pair(a, b));
  }

  set<ll> avec;
  set<ll> bvec;
  ll hmax = 0;
  ll wmax = 0;

  rep(i, h) { hmax = max(hmax, hcnt[i]); }
  rep(i, w) { wmax = max(wmax, wcnt[i]); }
  rep(i, h) {
    if (hmax == hcnt[i])
      avec.insert(i);
  }
  rep(i, w) {
    if (wmax == wcnt[i])
      bvec.insert(i);
  }

  ll k = avec.size() * bvec.size();

  for (auto p : table) {
    ll nowh = p.first;
    ll noww = p.second;
    if (avec.count(nowh) and bvec.count(noww))
      k--;
  }

  if (k <= 0)
    cout << hmax + wmax - 1 << endl;
  else
    cout << hmax + wmax << endl;
}

Error: 0

Fixed submission:

#define _GLIBCXX_DEBUG
#include <algorithm> //next_permutation
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define dec(n) cout << fixed << setprecision(n);
#define large "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define small "abcdefghijklmnopqrstuvwxyz"
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vl>;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

const ll MOD = 1000000007;
const ll MAX = 2000001;

ll mod(ll a) { return a % MOD; }

ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

ll fac[MAX], finv[MAX], inv[MAX];

void nCrprep() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

ll nCr(ll n, ll r) {
  if (n < r)
    return 0;
  if (n < 0 || r < 0)
    return 0;
  return fac[n] * (finv[r] * finv[n - r] % MOD) % MOD;
}

ll nCrcheep(ll n, ll r) {
  if (r == 0)
    return 1;
  else if (r == 1)
    return n;
  else
    return nCrcheep(n - 1, r - 1) * n / r;
}

vector<pair<ll, ll>> prime_factorize(ll n) {
  vector<pair<ll, ll>> res;

  for (ll i = 2; i * i <= n; i++) {
    if (n % i != 0)
      continue;

    ll ex = 0;
    while (n % i == 0) {
      ex++;
      n /= i;
    }

    res.push_back({i, ex});
  }

  if (n != 1)
    res.push_back({n, 1});

  return res;
}

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  vector<vector<bool>> table(h, vector<bool>(w, false));
  vl hcnt(h, 0);
  vl wcnt(w, 0);

  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    hcnt[a]++;
    wcnt[b]++;
    table[a][b] = true;
  }

  vl avec = {};
  vl bvec = {};
  ll hmax = 0;
  ll wmax = 0;

  rep(i, h) { hmax = max(hmax, hcnt[i]); }
  rep(i, w) { wmax = max(wmax, wcnt[i]); }
  rep(i, h) {
    if (hmax == hcnt[i])
      avec.push_back(i);
  }
  rep(i, w) {
    if (wmax == wcnt[i])
      bvec.push_back(i);
  }

  for (auto x : avec) {
    for (auto y : bvec) {
      if (!table[x][y]) {
        cout << hmax + wmax << endl;
        return 0;
      }
    }
  }

  cout << hmax + wmax - 1 << endl;
}