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

#include "stdio.h"
#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>

int read_int() {
  int n;
  std::cin >> n;
  return n;
}

template <typename T> std::vector<T> read_vector(int count) {
  std::vector<T> v(count);
  for (int i = 0; i < count; i++) {
    T tmp;
    std::cin >> tmp;
    v[i] = tmp;
  }
  return v;
}

template <typename T>
std::vector<std::vector<T>> read_vector(int row, int col) {
  std::vector<std::vector<T>> vv(row);
  for (int i = 0; i < row; i++) {
    auto v = read_vector<T>(col);
    vv[i] = v;
  }
  return vv;
}

int main() {
  int h = read_int();
  int w = read_int();
  int m = read_int();

  std::vector<int> hi(h);
  std::vector<int> wi(w);
  // std::vector<int> total(m);
  std::unordered_map<int, int> total;
  for (int i = 0; i < m; i++) {
    auto jj = read_int() - 1;
    auto ii = read_int() - 1;
    hi[jj] += 1;
    wi[ii] += 1;
    total[jj * w + ii] = 1;
  }

  std::vector<int>::iterator maxh_iter = std::max_element(hi.begin(), hi.end());
  // size_t hindex = std::distance(x.begin(), maxh_iter);
  int max_h = *maxh_iter;

  std::vector<int>::iterator maxw_iter = std::max_element(wi.begin(), wi.end());
  // size_t windex = std::distance(x.begin(), maxw_iter);
  int max_w = *maxw_iter;

  int max_b = max_h + max_w;
  if (max_b == h + w) {
    std::cout << h + w - 1;
    return 0;
  }

  bool has_0 = false;

  std::vector<int> max_w_i;
  for (int j = 0; j < w; j++) {
    if (wi[j] != max_w)
      continue;
    max_w_i.push_back(j);
  }

  for (int i = 0; i < h; i++) {
    if (hi[i] != max_h)
      continue;

    // for (int j = 0; j < w; j++)
    // {
    //     if (wi[j] != max_w)
    //         continue;
    for (std::vector<int>::iterator itr = max_w_i.begin(); itr != max_w_i.end();
         itr++) {
      int j = *itr;
      if (total.find(i * w + j) == total.end()) {
        has_0 = true;
        break;
      }
    }
    if (has_0)
      break;
  }
  if (!has_0)
    max_b -= 1;

  std::cout << max_b;
  return 0;
}


Error: TLE

Fixed submission:

#include "stdio.h"
#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>

int read_int() {
  int n;
  std::cin >> n;
  return n;
}

template <typename T> std::vector<T> read_vector(int count) {
  std::vector<T> v(count);
  for (int i = 0; i < count; i++) {
    T tmp;
    std::cin >> tmp;
    v[i] = tmp;
  }
  return v;
}

template <typename T>
std::vector<std::vector<T>> read_vector(int row, int col) {
  std::vector<std::vector<T>> vv(row);
  for (int i = 0; i < row; i++) {
    auto v = read_vector<T>(col);
    vv[i] = v;
  }
  return vv;
}

int main() {
  int h = read_int();
  int w = read_int();
  int m = read_int();

  std::vector<int> hi(h);
  std::vector<int> wi(w);
  // std::vector<int> total(m);
  std::unordered_map<int, int> total;
  for (int i = 0; i < m; i++) {
    auto jj = read_int() - 1;
    auto ii = read_int() - 1;
    hi[jj] += 1;
    wi[ii] += 1;
    total[jj * w + ii] = 1;
  }

  std::vector<int>::iterator maxh_iter = std::max_element(hi.begin(), hi.end());
  // size_t hindex = std::distance(x.begin(), maxh_iter);
  int max_h = *maxh_iter;

  std::vector<int>::iterator maxw_iter = std::max_element(wi.begin(), wi.end());
  // size_t windex = std::distance(x.begin(), maxw_iter);
  int max_w = *maxw_iter;

  int max_b = max_h + max_w;
  bool has_0 = false;

  std::vector<int> max_w_i;
  for (int j = 0; j < w; j++) {
    if (wi[j] != max_w)
      continue;
    max_w_i.push_back(j);
  }

  for (int i = 0; i < h; i++) {
    if (hi[i] != max_h)
      continue;

    // for (int j = 0; j < w; j++)
    // {
    //     if (wi[j] != max_w)
    //         continue;
    for (std::vector<int>::iterator itr = max_w_i.begin(); itr != max_w_i.end();
         itr++) {
      int j = *itr;
      if (total.find(i * w + j) == total.end()) {
        has_0 = true;
      }
    }
  }
  if (!has_0)
    max_b -= 1;

  std::cout << max_b;
  return 0;
}