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

#include <bits/stdc++.h>
#define pb push_back
#define fo(i, n) for (int i = 0; i < n; i++)
#define ll long long
#define Mod 1000000009
#define MAX 4294967296 // 2^32
const int M = 1e9 + 7;
long long mod(long long x) { return ((x % M + M) % M); }
long long add(long long a, long long b) { return mod(mod(a) + mod(b)); }
long long mul(long long a, long long b) { return mod(mod(a) * mod(b)); }
using namespace std;

long long gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

long long power(int x, int y) {
  if (y == 0)
    return 1;
  long long t = power(x, y / 2);
  if (y % 2 == 1)
    return x * t * t;
  return t * t;
}
double findMod(double a, double b) {
  double mod;
  // Handling negative values
  if (a < 0)
    mod = -a;
  else
    mod = a;
  if (b < 0)
    b = -b;

  // Finding mod by repeated subtraction

  while (mod >= b)
    mod = mod - b;

  // Sign of result typically depends
  // on sign of a.
  if (a < 0)
    return -mod;

  return mod;
}

ll power(ll x, ll y, ll p) {
  ll res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
             // equal to p

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}

// Returns n^(-1) mod p
ll modInverse(ll n, ll p) { return power(n, p - 2, p); }
ll ans = INT_MIN;
void dfs(vector<int> adj[], vector<int> &vis, int i, int &f) {
  vis[i] = 1;

  for (auto it = adj[i].begin(); it != adj[i].end(); it++) {
    if (!vis[*it]) {
      f++;
      dfs(adj, vis, *it, f);
    }
  }
}

int main() {

  // std::string binary = std::bitset<8>(n).to_string();
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll n, m, target;
  cin >> n >> m >> target;
  set<pair<int, int>> st;
  vector<pair<int, int>> v1;
  vector<pair<int, int>> v2;
  vector<int> vv1(n + 1, 0), vv2(m + 1, 0);
  while (target--) {
    ll a, b;
    cin >> a >> b;
    st.insert({a, b});
    vv1[a]++;
    v1.push_back({vv1[a], a});
    vv2[b]++;
    v2.push_back({vv2[b], b});
  }

  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());

  // cout<<v1[v1.size()-1].first<<" "<<v2[v2.size()-1].first<<endl;
  int x = v1[v1.size() - 1].second;
  int y = v2[v2.size() - 1].second;

  vector<int> xx;
  vector<int> yy;
  // cout<<v1[v1.size()-1].first<<" "<<v1[v1.size()-1].second<<endl;
  for (int i = 1; i <= n; i++) {
    if (vv1[i] == v1[v1.size() - 1].first) {
      xx.push_back(i);
    }
  }
  for (int i = 1; i <= m; i++) {
    if (vv2[i] == v2[v2.size() - 1].first) {
      yy.push_back(i);
    }
  }
  // for(int i=0;i<xx.size();i++)cout<<xx[i]<<" "<<vv1[xx[i]]<<" "<<endl;
  // cout<<endl;
  //  for(int i=0;i<yy.size();i++)cout<<yy[i]<<" "<<vv2[yy[i]]<<endl;
  //     cout<<endl;
  // cout<<v1[v1.size()-1].first<<" "<<v2[v2.size()-1].first<<endl;
  for (int i = 0; i < xx.size(); i++) {
    for (int j = 0; j < yy.size(); j++) {
      if (vv1[xx[i]] == v1[v1.size() - 1].first &&
          vv2[yy[j]] == v2[v2.size() - 1].first) {
        if (st.find({xx[i], yy[j]}) == st.end()) {
          cout << v1[v1.size() - 1].first + v2[v2.size() - 1].first << endl;
          return 0;
        }
      }
    }
  }
  cout << v1[v1.size() - 1].first + v2[v2.size() - 1].first - 1 << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define fo(i, n) for (int i = 0; i < n; i++)
#define ll long long
#define Mod 1000000009
#define MAX 4294967296 // 2^32
const int M = 1e9 + 7;
long long mod(long long x) { return ((x % M + M) % M); }
long long add(long long a, long long b) { return mod(mod(a) + mod(b)); }
long long mul(long long a, long long b) { return mod(mod(a) * mod(b)); }
using namespace std;

long long gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

long long power(int x, int y) {
  if (y == 0)
    return 1;
  long long t = power(x, y / 2);
  if (y % 2 == 1)
    return x * t * t;
  return t * t;
}
double findMod(double a, double b) {
  double mod;
  // Handling negative values
  if (a < 0)
    mod = -a;
  else
    mod = a;
  if (b < 0)
    b = -b;

  // Finding mod by repeated subtraction

  while (mod >= b)
    mod = mod - b;

  // Sign of result typically depends
  // on sign of a.
  if (a < 0)
    return -mod;

  return mod;
}

ll power(ll x, ll y, ll p) {
  ll res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
             // equal to p

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}

// Returns n^(-1) mod p
ll modInverse(ll n, ll p) { return power(n, p - 2, p); }
ll ans = INT_MIN;
void dfs(vector<int> adj[], vector<int> &vis, int i, int &f) {
  vis[i] = 1;

  for (auto it = adj[i].begin(); it != adj[i].end(); it++) {
    if (!vis[*it]) {
      f++;
      dfs(adj, vis, *it, f);
    }
  }
}

int main() {

  // std::string binary = std::bitset<8>(n).to_string();
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll n, m, target;
  cin >> n >> m >> target;
  set<pair<int, int>> st;
  vector<pair<int, int>> v1;
  vector<pair<int, int>> v2;
  vector<int> vv1(n + 1, 0), vv2(m + 1, 0);
  while (target--) {
    ll a, b;
    cin >> a >> b;
    st.insert({a, b});
    vv1[a]++;
    v1.push_back({vv1[a], a});
    vv2[b]++;
    v2.push_back({vv2[b], b});
  }

  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());

  // cout<<v1[v1.size()-1].first<<" "<<v2[v2.size()-1].first<<endl;
  int x = v1[v1.size() - 1].second;
  int y = v2[v2.size() - 1].second;
  vector<int> xx;
  vector<int> yy;
  for (int i = 1; i <= n; i++) {
    if (vv1[i] == v1[v1.size() - 1].first) {
      xx.push_back(i);
    }
  }
  for (int i = 1; i <= m; i++) {
    if (vv2[i] == v2[v2.size() - 1].first) {
      yy.push_back(i);
    }
  }
  for (int i = 0; i < xx.size(); i++) {
    for (int j = 0; j < yy.size(); i++) {
      if (vv1[i] == v1[v1.size() - 1].first &&
          vv2[j] == v2[v2.size() - 1].first) {
        if (st.find({i, j}) == st.end()) {
          cout << v1[v1.size() - 1].first + v2[v2.size() - 1].first << endl;
          return 0;
        }
      }
    }
  }
  cout << v1[v1.size() - 1].first + v2[v2.size() - 1].first - 1 << endl;
}