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

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

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

typedef long long int ll;
typedef long int li;

const ll mod = 1000000007;
const ll inf = 1000000000000000000;

typedef vector<int> vi;
typedef vector<li> vli;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef vector<vli> vvli;
typedef vector<vll> vvll;
typedef vector<vs> vvs;
typedef map<ll, ll> mll;
typedef map<ll, string> mls;
typedef map<string, ll> msl;
typedef pair<ll, ll> pll;

// #define ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update>
#pragma GCC optimize("-O3")
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define PI acos(-1)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pq priority_queue
#define lb lower_bound
#define ub upper_bound
#define all(v) v.begin(), v.end()
#define mini(a) *min_element(all(a))
#define maxi(a) *max_element(all(a))
#define tol(s) transform(s.begin(), s.end(), s.begin(), ::tolower);
#define tou(s) transform(s.begin(), s.end(), s.begin(), ::toupper);
#define FOR(i, j, k, in) for (ll i = j; i < k; i += in)
#define RFOR(i, j, k, de) for (ll i = j; i >= k; i -= de)
#define rep(i, j, k) FOR(i, j, k, 1)
#define rrep(i, j, k) RFOR(i, j, k, 1)
#define repc(v) for (auto it = v.begin(); it != v.end(); it++)
#define rrepc(v) for (auto it = v.rbegin(); it != v.rend(); it--)
#define T                                                                      \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)

ll gen() {
  ll temp;
  cin >> temp;
  return temp;
}

ll power(ll a, ll b, ll m) {
  ll res = 1;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % m;
      b--;
    }
    a = (a * a) % m;
    b = b >> 1;
  }
  return res;
}

ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  else
    return gcd(b % a, a);
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

#define inpt(v) generate(all(v), gen)
#define sum(v) accumulate(all(v), 0LL)
#define vpll vector<pll>
#define umap unordered_map
#define uset unordered_set
#define print(v) rep(i, 0, n) cout << v[i] << " ";

int32_t main() {
  FAST

      ll n,
      m, b;
  cin >> n >> m >> b;

  vll row(n, 0), col(m, 0);

  set<pll> st;
  rep(i, 0, b) {
    ll x, y;
    cin >> x >> y;

    row[x - 1]++;
    col[y - 1]++;
    st.insert({x, y});
  }

  vll rmax, cmax;
  ll rmx = maxi(row), cmx = maxi(col);
  rep(i, 0, n) if (rmx == row[i]) rmax.pb(i + 1);

  rep(i, 0, m) if (cmx == col[i]) cmax.pb(i + 1);

  int f = 0;
  rep(i, 0, rmax.size()) {
    rep(j, 0, cmax.size()) {
      if (st.find({rmax[i], cmax[j]}) == st.end()) {
        f = 1;
        break;
      }
    }
    if (f)
      break;
  }

  if (f)
    cout << rmx + cmx;
  else
    cout << rmx + cmx - 1;

  return 0;
}

Error: WA

Fixed submission:

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

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

typedef long long int ll;
typedef long int li;

const ll mod = 1000000007;
const ll inf = 1000000000000000000;

typedef vector<int> vi;
typedef vector<li> vli;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef vector<vli> vvli;
typedef vector<vll> vvll;
typedef vector<vs> vvs;
typedef map<ll, ll> mll;
typedef map<ll, string> mls;
typedef map<string, ll> msl;
typedef pair<ll, ll> pll;

// #define ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update>
#pragma GCC optimize("-O3")
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define PI acos(-1)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pq priority_queue
#define lb lower_bound
#define ub upper_bound
#define all(v) v.begin(), v.end()
#define mini(a) *min_element(all(a))
#define maxi(a) *max_element(all(a))
#define tol(s) transform(s.begin(), s.end(), s.begin(), ::tolower);
#define tou(s) transform(s.begin(), s.end(), s.begin(), ::toupper);
#define FOR(i, j, k, in) for (li i = j; i < k; i += in)
#define RFOR(i, j, k, de) for (li i = j; i >= k; i -= de)
#define rep(i, j, k) FOR(i, j, k, 1)
#define rrep(i, j, k) RFOR(i, j, k, 1)
#define repc(v) for (auto it = v.begin(); it != v.end(); it++)
#define rrepc(v) for (auto it = v.rbegin(); it != v.rend(); it--)
#define T                                                                      \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)

ll gen() {
  ll temp;
  cin >> temp;
  return temp;
}

ll power(ll a, ll b, ll m) {
  ll res = 1;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % m;
      b--;
    }
    a = (a * a) % m;
    b = b >> 1;
  }
  return res;
}

ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  else
    return gcd(b % a, a);
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

#define inpt(v) generate(all(v), gen)
#define sum(v) accumulate(all(v), 0LL)
#define vpll vector<pll>
#define umap unordered_map
#define uset unordered_set
#define print(v) rep(i, 0, n) cout << v[i] << " ";

int32_t main() {
  FAST

      ll n,
      m, b;
  cin >> n >> m >> b;

  vll row(n, 0), col(m, 0);

  set<pll> st;
  rep(i, 0, b) {
    ll x, y;
    cin >> x >> y;

    row[x - 1]++;
    col[y - 1]++;
    st.insert({x, y});
  }

  vll rmax, cmax;
  ll rmx = maxi(row), cmx = maxi(col);
  rep(i, 0, n) if (rmx == row[i]) rmax.pb(i + 1);

  rep(i, 0, n) if (cmx == col[i]) cmax.pb(i + 1);

  int f = 0;
  rep(i, 0, rmax.size()) {
    rep(j, 0, cmax.size()) {
      if (st.find({rmax[i], cmax[j]}) == st.end()) {
        f = 1;
        break;
      }
    }
    if (f)
      break;
  }

  if (f)
    cout << rmx + cmx;
  else
    cout << rmx + cmx - 1;

  return 0;
}