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

#include <bits/stdc++.h>
typedef long long int lli;
using namespace std;
#pragma region DEBUG
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif
#pragma endregion DEBUG
#define MOD 1000000007
#define MAX 500005
#define endl "\\n"
lli row[MAX];
lli col[MAX];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  lli t;
  t = 1;
  while (t--) {
    lli h, w;
    cin >> h >> w;
    lli m;
    cin >> m;
    vector<pair<lli, lli>> pts;
    for (lli i = 0; i < m; i++) {
      lli x, y;
      cin >> x >> y;
      x--;
      y--;
      pts.push_back({x, y});
      row[x]++;
      col[y]++;
    }
    lli finalx = 0;
    lli finaly = 0;
    lli maxmr = *max_element(row, row + h);
    lli maxmc = *max_element(col, col + w);
    for (lli i = 0; i < h; i++) {
      if (row[i] == maxmr)
        finalx++;
    }
    for (lli i = 0; i < w; i++) {
      if (col[i] == maxmc)
        finaly++;
    }
    lli ans = maxmr + maxmc - 1;
    lli count = 0;
    for (lli i = 0; i < m; i++) {
      lli x, y;
      tie(x, y) = pts[i];
      if (row[x] == maxmr && col[y] == maxmc)
        count++;
    }
    lli a = finalx;
    lli b = finaly;
    if (count == a * b)
      cout << ans << endl;
    else {
      cout << ans + 1 << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
typedef long long int lli;
using namespace std;
#pragma region DEBUG
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif
#pragma endregion DEBUG
#define MOD 1000000007
#define MAX 500005
#define endl "\\n"
lli row[MAX];
lli col[MAX];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  lli t;
  t = 1;
  while (t--) {
    lli h, w;
    cin >> h >> w;
    lli m;
    cin >> m;
    vector<set<lli>> adj(h + 5);
    for (lli i = 0; i < m; i++) {
      lli x, y;
      cin >> x >> y;
      x--;
      y--;
      adj[x].insert(y);
      adj[y].insert(x);
      row[x]++;
      col[y]++;
    }
    set<lli> finalx;
    set<lli> finaly;
    lli maxmr = *max_element(row, row + h);
    lli maxmc = *max_element(col, col + w);
    for (lli i = 0; i < h; i++) {
      if (row[i] == maxmr)
        finalx.insert(i);
    }
    for (lli i = 0; i < w; i++) {
      if (col[i] == maxmc)
        finaly.insert(i);
    }
    // debug(maxmr,maxmc);
    // debug(adj);
    // debug(finalx,finaly);
    lli ans = maxmr + maxmc - 1;
    lli count = 0;
    for (lli i = 0; i < h; i++) {
      for (auto j : adj[i]) {
        if (finalx.count(i) && finaly.count(j)) {
          count++;
        }
      }
    }
    lli a = finalx.size();
    lli b = finaly.size();
    if (count == a * b)
      cout << ans << endl;
    else {
      cout << ans + 1 << endl;
    }
  }
}