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

#include <memory.h>

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define MOD 1000000007

int main() {
  int h, w;
  cin >> h >> w;
  int m;
  cin >> m;
  int hcnt[300010] = {};
  int wcnt[300010] = {};
  set<pair<int, int>> s;
  for (int i = 0; i < m; i++) {
    int hi, wi;
    cin >> hi >> wi;
    hi--;
    wi--;
    s.insert(make_pair(hi, wi));
    hcnt[hi]++;
    wcnt[wi]++;
  }
  vector<pair<int, int>> sortedHcnt;
  vector<pair<int, int>> sortedWcnt;
  for (int i = 0; i < h; i++) {
    if (hcnt[i] > 0)
      sortedHcnt.push_back(make_pair(hcnt[i], i));
  }
  for (int i = 0; i < w; i++) {
    if (wcnt[i] > 0)
      sortedWcnt.push_back(make_pair(wcnt[i], i));
  }
  sort(sortedHcnt.begin(), sortedHcnt.end());
  sort(sortedWcnt.begin(), sortedWcnt.end());
  int ans = 0;
  int sh = sortedHcnt.size();
  int sw = sortedWcnt.size();
  for (int i = sh - 1; i >= 0; i--) {
    if (sortedHcnt[i].first != sortedHcnt[sh - 1].first) {
      break;
    }
    for (int j = sw - 1; j >= 0; j--) {
      if (sortedWcnt[j].first != sortedWcnt[sw - 1].first) {
        break;
      }
      int res = sortedHcnt[i].first + sortedWcnt[j].first;
      if (s.find(make_pair(sortedHcnt[i].second, sortedWcnt[j].second)) !=
          s.end()) {
        res--;
        ans = max(ans, res);
      } else {
        cout << res << endl;
        return 0;
      }
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <memory.h>

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define MOD 1000000007

int main() {
  int h, w;
  cin >> h >> w;
  int m;
  cin >> m;
  int hcnt[300010] = {};
  int wcnt[300010] = {};
  set<pair<int, int>> s;
  for (int i = 0; i < m; i++) {
    int hi, wi;
    cin >> hi >> wi;
    hi--;
    wi--;
    s.insert(make_pair(hi, wi));
    hcnt[hi]++;
    wcnt[wi]++;
  }
  vector<pair<int, int>> sortedHcnt;
  vector<pair<int, int>> sortedWcnt;
  for (int i = 0; i < h; i++) {
    if (hcnt[i] > 0)
      sortedHcnt.push_back(make_pair(hcnt[i], i));
  }
  for (int i = 0; i < w; i++) {
    if (wcnt[i] > 0)
      sortedWcnt.push_back(make_pair(wcnt[i], i));
  }
  sort(sortedHcnt.begin(), sortedHcnt.end());
  sort(sortedWcnt.begin(), sortedWcnt.end());
  int ans = 0;
  int sh = sortedHcnt.size();
  int sw = sortedWcnt.size();
  for (int i = sh - 1; i >= 0; i--) {
    if (sortedHcnt[i].first != sortedHcnt[sh - 1].first) {
      break;
    }
    for (int j = sw - 1; j >= 0; j--) {
      if (sortedWcnt[j].first != sortedWcnt[sw - 1].first) {
        break;
      }
      int res = sortedHcnt[i].first + sortedWcnt[j].first;
      if (s.find(make_pair(sortedHcnt[i].second, sortedWcnt[i].second)) !=
          s.end()) {
        res--;
        ans = max(ans, res);
      } else {
        cout << res << endl;
        return 0;
      }
    }
  }
  cout << ans << endl;
}