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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;

int main() {
  long long int hh, ww, m;
  cin >> hh >> ww >> m;

  std::vector<int> hw[hh];
  std::vector<std::pair<int, int>> p;
  std::vector<int> mph(hh), mpw(ww); // 各行・列毎のカウンタ
  for (long long int i = 0; i < m; i++) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    mph[h]++;
    mpw[w]++;
    hw[h].push_back(w);
    p.push_back(std::make_pair(h, w));
  }

  long long int maxh = 0, maxw = 0;
  std::multimap<long long int, long long int> mh, mw;
  for (long long int i = 0; i < hh; i++) {
    mh.emplace(mph[i], i);
    if (mph[i] > maxh)
      maxh = mph[i];
  }
  for (long long int i = 0; i < ww; i++) {
    mw.emplace(mpw[i], i);
    if (mpw[i] > maxw)
      maxw = mpw[i];
  }
  // cout << maxh << ", " << maxw << endl;

  /*
  auto ph = mh.equal_range(maxh);
  for (auto it = ph.first; it != ph.second; ++it) {
      //cout << "i:" << it->second << endl;
      auto pw = mw.equal_range(maxw);
      for (auto it2 = pw.first; it2 != pw.second; ++it2) {
          //cout << "j:" << it2->second << endl;
          if(std::find(hw[it->second].begin(), hw[it->second].end(),
  it2->second) == hw[it->second].end()){ cout << maxh+maxw << endl; return 0;
          }

      }
  }
  */
  int ph = mh.count(maxh);
  int pw = mw.count(maxw);
  int cnt = 0;
  for (int i = 0; i < m; i++) {
    std::pair<int, int> tmp = p[i];
    if (mph[tmp.first] == maxh && mpw[tmp.second] == maxw)
      cnt++;
  }
  if (cnt == ph * pw)
    cout << maxh + maxw - 1 << endl;
  else
    cout << maxh + maxw << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <vector>
using namespace std;

int main() {
  long long int hh, ww, m;
  cin >> hh >> ww >> m;

  /*
  int hw[hh][ww];
  for(int i=0; i<hh; i++){
      for(int j=0; j<ww; j++){
          hw[i][j] = 0;
      }
  }
  cout << "WA"; return 0;
  */

  std::vector<int> hw[hh];

  std::vector<int> mph(hh), mpw(ww); // 各行・列毎のカウンタ
  for (long long int i = 0; i < m; i++) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    mph[h]++;
    mpw[w]++;
    hw[h].push_back(w);
  }

  long long int maxh = 0, maxw = 0;
  std::multimap<long long int, long long int> mh, mw;
  for (long long int i = 0; i < hh; i++) {
    mh.emplace(mph[i], i);
    if (mph[i] > maxh)
      maxh = mph[i];
  }
  for (long long int i = 0; i < ww; i++) {
    mw.emplace(mpw[i], i);
    if (mpw[i] > maxw)
      maxw = mpw[i];
  }
  // cout << maxh << ", " << maxw << endl;

  auto ph = mh.equal_range(maxh);
  for (auto it = ph.first; it != ph.second; ++it) {
    // cout << "i:" << it->second << endl;
    /*
    if(hw[it->second].empty() == true){
        cout << maxh+maxw << endl;
        return 0;
    }else{
        std::vector<int>::iterator tmpitr = hw[it->second].begin();
        while(tmpitr != hw[it->second].end()){
            cout << *tmpitr << ", ";
            ++tmpitr;
        }
        cout << endl;
    }
    */

    auto pw = mw.equal_range(maxw);
    for (auto it2 = pw.first; it2 != pw.second; ++it2) {
      // cout << "j:" << it2->second << endl;
      if (std::find(hw[it->second].begin(), hw[it->second].end(),
                    it2->second) == hw[it->second].end()) {
        cout << maxh + maxw << endl;
        return 0;
      }
    }
  }
  cout << maxh + maxw - 1 << endl;
  return 0;
}