Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <stack>
using namespace std;
int main() {
  int h, w, m;
  cin >> h >> w >> m;

  vector<int> hcnt(h, 0);
  vector<int> wcnt(w, 0);
  int hm = 0;
  int wm = 0;
  set<pair<int, int>> bomb;
  int cnt = -1;
  for (int i = 0; i < m; i++) {
    int y;
    int x;
    cin >> y >> x;
    y--;
    x--;

    bomb.insert(make_pair(y, x));
    hcnt[y]++;
    wcnt[x]++;
    if (hcnt[y] > hm)
      hm = hcnt[y];
    if (wcnt[x] > wm)
      wm = wcnt[x];
  }

  vector<int> hn;
  vector<int> wn;
  for (int i = 0; i < h; i++) {
    if (hcnt[i] == hm)
      hn.push_back(i);
  }
  for (int i = 0; i < w; i++) {
    if (wcnt[i] == wm)
      wn.push_back(i);
  }

  for (int i = 0; i < hn.size(); i++) {
    for (int j = 0; j < wn.size(); j++) {
      if (bomb.count(pair<int, int>(hn[i], wn[j])))
        continue;
      cout << hm + wm << endl;
      return 0;
    }
  }
  cout << hm + wm - 1 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <stack>
using namespace std;
int main() {
  int h, w, m;
  cin >> h >> w >> m;

  // vector<vector<char>> field(h,vector<char>(w,'.'));

  stack<pair<int, int>> hs;
  stack<pair<int, int>> ws;
  vector<int> hcnt(h, 0);
  vector<int> wcnt(w, 0);
  int hm = 0;
  int wm = 0;
  vector<pair<int, int>> bomb;
  int cnt = -1;
  for (int i = 0; i < m; i++) {
    int y;
    int x;
    cin >> y >> x;
    y--;
    x--;

    // field[y][x] = '#';

    bomb.push_back(make_pair(y, x));
    hcnt[y]++;
    wcnt[x]++;
    if (hcnt[y] > hm)
      hm = hcnt[y];
    if (wcnt[x] > wm)
      wm = wcnt[x];

    if (hcnt[y] == hm)
      hs.push(make_pair(hm, y));
    if (wcnt[x] == wm)
      ws.push(make_pair(wm, x));
  }

  ///////////////////////////////////
  // cout << endl;
  // for(int i=0; i<h; i++){
  //     for(int j=0; j<w; j++){
  //         cout << field[i][j];
  //     }
  //     cout << endl;
  // }
  // for(int i=0; i<h; i++){
  //     cout << hcnt[i] << " ";
  //     if(i == h-1) cout << endl;
  // }
  // for(int i=0; i<w; i++){
  //     cout << wcnt[i] << " ";
  //     if(i == w-1) cout << endl;
  // }
  ///////////////////////////////////

  vector<int> hn;
  vector<int> wn;
  while (!hs.empty()) {
    if (hs.top().first != hm)
      break;
    hn.push_back(hs.top().second);
    hs.pop();
  }
  while (!ws.empty()) {
    if (ws.top().first != wm)
      break;
    wn.push_back(ws.top().second);
    ws.pop();
  }

  //
  // cout << "hn: ";
  // for(int i=0; i<hn.size(); i++){
  //     cout << hn[i] << " ";
  // }
  // cout << endl;
  // cout << "wn: ";
  // for(int i=0; i<wn.size(); i++){
  //     cout << wn[i] << " ";
  // }
  // cout << endl;
  // cout << endl;
  // cout << "bomb" << endl;
  // for(int i=0; i<m; i++){
  //     cout << bomb[i].first << " " << bomb[i].second << endl;
  // }
  //

  for (int i = 0; i < hn.size(); i++) {
    for (int j = 0; j < wn.size(); j++) {
      bool b = true;
      for (int k = 0; k < m; k++) {
        if (bomb[k].first == hn[i] && bomb[k].second == wn[j]) {
          b = false;
        }
      }
      if (b) {
        cout << hm + wm << endl;
        return 0;
      }
    }
  }
  cout << hm + wm - 1 << endl;
}