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

// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdlib.h>
#include <vector>
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
const int MAXN = 3e5 + 10;
const int mod = 1e9 + 7;
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
#define eps 1e-8
#define PI 3.1415926535898
#define lowbit(x) (x & (-x))
using namespace std;
// getline(cin,s);
// set<ll>::iterator it;
// map<int,int>::iterator it;
typedef long long ll;
int hang[MAXN], lie[MAXN];
map<pair<int, int>, int> mp;
vector<int> v1, v2;
int main() {
  int n, m, t;
  int flag = 0;
  scanf("%d %d %d", &n, &m, &t);
  for (int i = 1; i <= t; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    mp[{x, y}] = 1;
    hang[x]++;
    lie[y]++;
  }
  int max1 = 0, max2 = 0;
  for (int i = 1; i <= n; i++) {
    max1 = max(max1, hang[i]);
  }
  for (int i = 1; i <= n; i++) {
    if (hang[i] == max1)
      v1.push_back(i);
  }
  for (int i = 1; i <= m; i++) {
    max2 = max(max2, lie[i]);
  }
  for (int i = 1; i <= m; i++) {
    if (lie[i] == max2)
      v2.push_back(i);
  }
  int size1 = v1.size(), size2 = v2.size();
  for (int i = 0; i < size1; i++) {
    for (int j = 0; j < size2; j++) {
      int x = v1[i], y = v2[j];
      if (!mp[{x, y}]) {
        flag = 1;
        break;
      }
    }
    if (flag)
      break;
  }
  if (flag)
    printf("%d\\n", max1 + max2);
  else
    printf("%d\\n", max1 + max2 - 1);
  return 0;
}

Error: TLE

Fixed submission:

// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdlib.h>
#include <vector>
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
const int MAXN = 3e5 + 10;
const int mod = 1e9 + 7;
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
#define eps 1e-8
#define PI 3.1415926535898
#define lowbit(x) (x & (-x))
using namespace std;
// getline(cin,s);
// set<ll>::iterator it;
// map<int,int>::iterator it;
typedef long long ll;
vector<int> v1[MAXN], v2[MAXN];
map<pair<int, int>, int> mp;
vector<int> vec;
int main() {
  int n, m, t;
  int ans = 0;
  scanf("%d %d %d", &n, &m, &t);
  while (t--) {
    int h, w;
    scanf("%d %d", &h, &w);
    mp[{h, w}] = 1;
    v1[h].push_back(w);
    v2[w].push_back(h);
  }
  int max1 = 0;
  for (int i = 1; i <= n; i++) {
    int x = v1[i].size();
    max1 = max(max1, x);
  }
  for (int i = 1; i <= n; i++) {
    if (v1[i].size() == max1) {
      vec.push_back(i);
    }
  }
  int ans1 = 0;
  int size = vec.size();
  for (int j = 0; j < vec.size(); j++) {
    int index = vec[j];
    for (int i = 1; i <= m; i++) {
      int sum;
      if (mp[{index, i}])
        sum = v2[i].size() - 1;
      else
        sum = v2[i].size();
      ans1 = max(ans1, sum);
    }
    ans = max(ans, ans1 + max1);
  }
  printf("%d\\n", ans);
}