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

#include <bits/stdc++.h>
using namespace std;

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

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define read freopen("in.txt", "r", stdin)
#define write freopen("out.txt", "w", stdout)
#define ms(a, x) memset(a, x, sizeof a)
#define ll long long int
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define nl cout << "\\n"
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / (gcd(a, b))
#define sq(a) (a) * (a)
#define loop(i, a, b) for (ll i = a; i <= b; i++)
#define rloop(i, a, b) for (ll i = a; i >= b; i--)
#define sz(a) a.size()
#define mod 1000000007
#define PI 2 * acos(0.0)

int main() {
  IOS int T = 1;
  // cin>>T;
  while (T--) {
    ll h, w, m;
    cin >> h >> w >> m;
    map<pair<ll, ll>, ll> v;
    map<ll, ll> r, c;
    ll r1 = 0, c1 = 0;
    loop(i, 1, m) {
      ll x, y;
      cin >> x >> y;
      v[{x, y}]++;
      r[x]++;
      c[y]++;
      r1 = max(r1, r[x]);
      c1 = max(c1, c[y]);
    }
    vector<ll> rr, cc;
    for (auto a : r) {
      if (a.ss == r1) {
        rr.pb(a.ff);
      }
    }
    for (auto a : c) {
      if (a.ss == c1) {
        cc.pb(a.ff);
      }
    }
    // debug(r1,c1,x,y);
    bool f = true;
    ll p = sz(rr) - 1, q = sz(cc) - 1;
    loop(j, 0, p) {
      loop(k, 0, q) {
        if (v[{rr[j], cc[k]}] == 0) {
          f = false;
          break;
        }
      }
      if (f == false)
        break;
    }

    if (f) {
      cout << r1 + c1 - 1;
    } else
      cout << r1 + c1;
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

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

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define read freopen("in.txt", "r", stdin)
#define write freopen("out.txt", "w", stdout)
#define ms(a, x) memset(a, x, sizeof a)
#define ll long long int
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define nl cout << "\\n"
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / (gcd(a, b))
#define sq(a) (a) * (a)
#define loop(i, a, b) for (ll i = a; i <= b; i++)
#define rloop(i, a, b) for (ll i = a; i >= b; i--)
#define sz(a) a.size()
#define mod 1000000007
#define PI 2 * acos(0.0)
ll r[100005], c[100005];
int main() {
  IOS int T = 1;
  // cin>>T;
  while (T--) {
    ll h, w, m;
    cin >> h >> w >> m;
    map<pair<ll, ll>, ll> v;
    ll r1 = 0, c1 = 0;
    loop(i, 1, m) {
      ll x, y;
      cin >> x >> y;
      v[{x, y}]++;
      r[x]++;
      c[y]++;
      r1 = max(r1, r[x]);
      c1 = max(c1, c[y]);
    }
    vector<ll> rr, cc;
    loop(i, 1, 100000) {
      if (r[i] == r1) {
        rr.pb(i);
      }
    }
    loop(i, 1, 100000) {
      if (c[i] == c1) {
        cc.pb(i);
      }
    }
    // debug(r1,c1,x,y);
    bool f = true;
    ll p = sz(rr) - 1, q = sz(cc) - 1;
    loop(j, 0, p) {
      loop(k, 0, q) {
        if (v[{rr[j], cc[k]}] == 0) {
          f = false;
          break;
        }
      }
      if (f == false)
        break;
    }

    if (f) {
      cout << r1 + c1 - 1;
    } else
      cout << r1 + c1;
  }
  return 0;
}