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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<pair<int, int>> hb(H);
  vector<pair<int, int>> wb(W);
  vector<vector<int>> bomb(H);

  for (int i = 0; i < M; i++) {
    int h, w;
    cin >> h >> w;
    pair<int, int> ph = hb[h - 1];
    pair<int, int> pw = wb[w - 1];
    hb[h - 1] = pair<int, int>(ph.first + 1, h - 1);
    wb[w - 1] = pair<int, int>(pw.first + 1, w - 1);
    bomb[h - 1].push_back(w - 1);
  }
  sort(hb.begin(), hb.end());
  reverse(hb.begin(), hb.end());
  sort(wb.begin(), wb.end());
  reverse(wb.begin(), wb.end());

  int max_hb = 0;
  vector<pair<int, int>> h_bomb;
  for (auto bom : hb) {
    int index = bom.second;
    int num = bom.first;
    if (num < max_hb)
      break;
    max_hb = num;
    pair<int, int> p(index, num);
    h_bomb.push_back(p);
  }
  int max_wb = 0;
  vector<pair<int, int>> w_bomb;
  for (auto bom : wb) {
    int index = bom.second;
    int num = bom.first;
    if (num < max_wb)
      break;
    max_wb = num;
    pair<int, int> p(index, num);
    w_bomb.push_back(p);
  }
  int max_bomb = max_hb + max_wb;
  bool found = false;
  for (auto hbom : h_bomb) {
    for (auto wbom : w_bomb) {
      int h_index = hbom.first;
      int w_index = wbom.first;
      auto result =
          std::find(bomb[h_index].begin(), bomb[h_index].end(), w_index);
      if (result == bomb[h_index].end()) {
        found = true;
        break;
      }
    }
    if (found)
      break;
  }
  if (found)
    cout << max_bomb << endl;
  else
    cout << max_bomb - 1 << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<pair<int, int>> hb(H);
  vector<pair<int, int>> wb(W);
  vector<vector<int>> bomb(H);

  for (int i = 0; i < M; i++) {
    int h, w;
    cin >> h >> w;
    pair<int, int> ph = hb[h - 1];
    pair<int, int> pw = wb[w - 1];
    hb[h - 1] = pair<int, int>(ph.first + 1, h - 1);
    wb[w - 1] = pair<int, int>(pw.first + 1, w - 1);
    bomb[h - 1].push_back(w - 1);
  }
  sort(hb.begin(), hb.end());
  reverse(hb.begin(), hb.end());
  sort(wb.begin(), wb.end());
  reverse(wb.begin(), wb.end());

  int max_hb = 0;
  vector<pair<int, int>> h_bomb;
  for (auto bom : hb) {
    int index = bom.second;
    int num = bom.first;
    if (num < max_hb)
      break;
    max_hb = num;
    pair<int, int> p(index, num);
    h_bomb.push_back(p);
  }
  int max_wb = 0;
  vector<pair<int, int>> w_bomb;
  for (auto bom : wb) {
    int index = bom.second;
    int num = bom.first;
    if (num < max_wb)
      break;
    max_wb = num;
    pair<int, int> p(index, num);
    w_bomb.push_back(p);
  }
  int max_bomb = max_hb + max_wb;
  bool found = false;
  for (auto hbom : h_bomb) {
    for (auto wbom : w_bomb) {
      int h_index = hbom.first;
      int w_index = wbom.first;
      auto result =
          std::find(bomb[h_index].begin(), bomb[h_index].end(), w_index);
      if (result != bomb[h_index].end()) {
      } else {
        found = true;
        // break;
      }
    }
    if (found)
      break;
  }
  if (found)
    cout << max_bomb << endl;
  else
    cout << max_bomb - 1 << endl;
}