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

#include <bits/stdc++.h>
using namespace std;

long long int Exp(long long int exp_n, long long int exp_e, long long exp_mod);

int main() {
  int H, W, M, h, w, O = 1;
  cin >> H >> W >> M;
  int Bh[H], Bw[W];
  map<pair<int, int>, int> m;
  for (int i = 0; i < H; i++) {
    Bh[i] = 0;
  }
  for (int i = 0; i < W; i++) {
    Bw[i] = 0;
  }
  for (int i = 0; i < M; i++) {
    cin >> h >> w;
    Bh[h - 1]++;
    Bw[w - 1]++;
    m[make_pair(h - 1, w - 1)] = 1;
  }
  vector<int> vh, vw;
  int mh = 0, mw = 0;
  for (int i = 0; i < H; i++) {
    if (mh < Bh[i]) {
      mh = Bh[i];
      vh.clear();
      vh.push_back(i);
    } else if (mh == Bh[i]) {
      vh.push_back(i);
    }
  }
  for (int i = 0; i < W; i++) {
    if (mw < Bw[i]) {
      mw = Bw[i];
      vw.clear();
      vw.push_back(i);
    } else if (mw == Bw[i]) {
      vw.push_back(i);
    }
  }

  for (int i = 0; i < vh.size(); i++) {
    for (int j = 0; j < vw.size(); j++) {
      if (m[make_pair(vh[i], vw[j])] == 0) {
        O = 0;
        break;
      }
    }
    if (O == 0) {
      break;
    }
  }

  cout << mh + mw - O << endl;
}

long long int Exp(long long int exp_n, long long int exp_e,
                  long long int exp_mod) {
  long long int exp_o = 1;
  long long int exp_x = exp_n;
  long long int exp_tmp;

  while (0 < exp_e) {
    exp_tmp = exp_e / 2;
    if (exp_e - exp_tmp * 2 == 1) {
      exp_o = exp_o * exp_x;
      if (exp_o > exp_mod) {
        exp_o = exp_o % exp_mod;
      }
    }

    exp_x = exp_x * exp_x;
    if (exp_x > exp_mod) {
      exp_x = exp_x % exp_mod;
    }
    exp_e = exp_tmp;
  }
  return exp_o;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

long long int Exp(long long int exp_n, long long int exp_e, long long exp_mod);

int main() {
  int H, W, M, h, w, O = 1;
  cin >> H >> W >> M;
  int Bh[H], Bw[W];
  map<pair<int, int>, int> m;
  for (int i = 0; i < H; i++) {
    Bh[i] = 0;
  }
  for (int i = 0; i < W; i++) {
    Bw[i] = 0;
  }
  for (int i = 0; i < M; i++) {
    cin >> h >> w;
    Bh[h - 1]++;
    Bw[w - 1]++;
    m[make_pair(h - 1, w - 1)] = 1;
  }
  vector<int> vh, vw;
  int mh = 0, mw = 0;
  for (int i = 0; i < H; i++) {
    if (mh < Bh[i]) {
      mh = Bh[i];
      vh.clear();
      vh.push_back(i);
    } else if (mh == Bh[i]) {
      vh.push_back(i);
    }
  }
  for (int i = 0; i < W; i++) {
    if (mw < Bw[i]) {
      mw = Bw[i];
      vw.clear();
      vw.push_back(i);
    } else if (mw == Bw[i]) {
      vw.push_back(i);
    }
  }

  for (int i = 0; i < vh.size(); i++) {
    for (int j = 0; j < vw.size(); j++) {
      if (m[make_pair(vh[i], vw[j])] == 0) {
        O = 0;
      }
    }
  }

  cout << mh + mw - O << endl;
}

long long int Exp(long long int exp_n, long long int exp_e,
                  long long int exp_mod) {
  long long int exp_o = 1;
  long long int exp_x = exp_n;
  long long int exp_tmp;

  while (0 < exp_e) {
    exp_tmp = exp_e / 2;
    if (exp_e - exp_tmp * 2 == 1) {
      exp_o = exp_o * exp_x;
      if (exp_o > exp_mod) {
        exp_o = exp_o % exp_mod;
      }
    }

    exp_x = exp_x * exp_x;
    if (exp_x > exp_mod) {
      exp_x = exp_x % exp_mod;
    }
    exp_e = exp_tmp;
  }
  return exp_o;
}