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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#pragma GCC optimize(3, "Ofast", "inline")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define x first
#define y second
#define pb push_back
#define ls rt << 1
#define rs rt << 1 | 1
typedef long long ll;
const double esp = 1e-6, pi = acos(-1);
typedef pair<int, int> PII;
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
int a[N], b[N];
map<PII, int> mp;
int work() {
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= k; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    a[x]++, b[y]++;
    mp[{x, y}] = 1;
  }
  vector<int> va, vb;
  int maxn = 0;
  for (int i = 1; i <= n; i++)
    if (a[i] > maxn)
      maxn = a[i], va.clear(), va.emplace_back(i);
    else if (a[i] == maxn)
      va.emplace_back(i);
  maxn = 0;
  for (int i = 1; i <= m; i++) {
    if (b[i] > maxn)
      maxn = b[i], vb.clear(), vb.emplace_back(i);
    else if (b[i] == maxn)
      vb.emplace_back(i);
  }
  int ans = 0;
  for (auto x : va) {
    for (auto y : vb) {
      if (!mp[{x, y}])
        return cout << a[x] + b[y] << endl, 0;
    }
  }
  cout << a[va.back()] + b[vb.back()] - 1 << endl;
  return 0;
}
int main() {
  //   freopen("C:UsersspnooyseedDesktopin.txt" , "r" , stdin) ;
  //   freopen("C:UsersspnooyseedDesktopout.txt" , "w" , stdout) ;

  work();
  return 0;
}
/*
 */


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#pragma GCC optimize(3, "Ofast", "inline")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define x first
#define y second
#define pb push_back
#define ls rt << 1
#define rs rt << 1 | 1
typedef long long ll;
const double esp = 1e-6, pi = acos(-1);
typedef pair<int, int> PII;
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
int a[N], b[N];
map<PII, int> mp;
int work() {
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= k; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    a[x]++, b[y]++;
    mp[{x, y}] = 1;
  }
  vector<int> va, vb;
  int maxn = 0;
  for (int i = 1; i <= n; i++)
    if (a[i] > maxn)
      maxn = a[i], va.clear(), va.emplace_back(i);
    else if (a[i] == maxn)
      va.emplace_back(i);
  maxn = 0;
  for (int i = 1; i <= m; i++) {
    if (b[i] > maxn)
      maxn = b[i], vb.clear(), vb.emplace_back(i);
    else if (b[i] == maxn)
      vb.emplace_back(i);
  }
  int ans = 0;
  for (auto x : va) {
    for (auto y : vb) {
      ans = max(ans, a[x] + b[y] - mp[{x, y}]);
    }
  }
  cout << ans << endl;
  return 0;
}
int main() {
  //   freopen("C:UsersspnooyseedDesktopin.txt" , "r" , stdin) ;
  //   freopen("C:UsersspnooyseedDesktopout.txt" , "w" , stdout) ;

  work();
  return 0;
}
/*
 */