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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
/*
 execution in cmd of windows:


  c


  p1

 */
typedef double dd;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define nl << "\\n"
#define con continue
#define br break
#define vll vector<ll>
#define vpll vector<pair<ll, ll>>
ll mx = INT_MAX;
ll mi = INT_MIN;
#define mem0(a) memset(a, 0, sizeof(a))
#define mem1(a) memset(a, -1, sizeof(a))
#define all(v) v.begin(), v.end()
#define fsort(v) sort(all(v))
#define rsort(v) sort(v.rbegin(), v.rend())
#define rev(v) reverse(all(v))
#define maxc(v) max_element(all(v))
#define minc(v) min_element(all(v))
#define sumv(v, x) accumulate(v.begin(), v.end(), x);
const ll mod = 1000000007;
const ll inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
#define io                                                                     \\
  {                                                                            \\
    ios ::sync_with_stdio(false);                                              \\
    cin.tie(0);                                                                \\
    cout.tie(0);                                                               \\
  }
#define cer                                                                    \\
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC       \\
       << "ms\\n";
#define cs                                                                     \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define cs1                                                                    \\
  ll t;                                                                        \\
  t = 1;                                                                       \\
  while (t--)

int main() {
  io;
  //////////////////////////////////
  cs1 {
    ll i, j, k, l, s = 0, p = 0, q = 0, h, w, m;
    cin >> h >> w >> m;
    // ll arr[h+5][w+5];
    vll vr(m), vc(m);
    vpll vx;
    map<pair<ll, ll>, ll> mi;
    map<ll, ll> mr, mc;
    set<ll> sr, sc;
    for (i = 0; i < m; i++) {
      ll x, y;
      cin >> x >> y;
      mi[make_pair(x, y)]++;
      mr[x]++;
      mc[y]++;
      sr.insert(x);
      sc.insert(y);
    }
    ll row = -1;
    ll low1 = 0;
    vll vv;
    for (auto it = sr.begin(); it != sr.end(); ++it) {
      ll val = *it;
      if (mr[val] > low1) {
        vv.clear();
        vv.pb(val);
        low1 = mr[val];
      } else if (mr[val] == low1)
        vv.pb(val);
    }
    ll low2 = 0;
    vll vvv;
    for (auto it = sc.begin(); it != sc.end(); ++it) {
      ll val = *it;
      if (mc[val] > low2) {
        vvv.clear();
        vvv.pb(val);
        low2 = mc[val];
      } else if (mc[val] == low2)
        vvv.pb(val);
    }
    for (i = 0; i < vv.size(); i++) {
      for (j = 0; j < vvv.size(); j++) {
        pair<ll, ll> mx = {vv[i], vvv[j]};

        if (mi[mx] == 0) {
          q = 1;
          br;
        }
      }
      if (q == 1)
        br;
    }
    if (q == 1)
      cout << low1 + low2;
    else
      cout << low1 + low2 - 1 nl;
    // cout<<vv.size()<<" "<<vvv.size();
  }
  //////////////////////////////////
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
/*
 execution in cmd of windows:


  c


  p1

 */
typedef double dd;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define nl << "\\n"
#define con continue
#define br break
#define vll vector<ll>
#define vpll vector<pair<ll, ll>>
ll mx = INT_MAX;
ll mi = INT_MIN;
#define mem0(a) memset(a, 0, sizeof(a))
#define mem1(a) memset(a, -1, sizeof(a))
#define all(v) v.begin(), v.end()
#define fsort(v) sort(all(v))
#define rsort(v) sort(v.rbegin(), v.rend())
#define rev(v) reverse(all(v))
#define maxc(v) max_element(all(v))
#define minc(v) min_element(all(v))
#define sumv(v, x) accumulate(v.begin(), v.end(), x);
const ll mod = 1000000007;
const ll inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
#define io                                                                     \\
  {                                                                            \\
    ios ::sync_with_stdio(false);                                              \\
    cin.tie(0);                                                                \\
    cout.tie(0);                                                               \\
  }
#define cer                                                                    \\
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC       \\
       << "ms\\n";
#define cs                                                                     \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define cs1                                                                    \\
  ll t;                                                                        \\
  t = 1;                                                                       \\
  while (t--)

int main() {
  io;
  //////////////////////////////////
  cs1 {
    ll i, j, k, l, s = 0, p = 0, q = 0, h, w, m;
    cin >> h >> w >> m;
    // ll arr[h+5][w+5];
    vll vr(m), vc(m);
    vpll vx;
    map<ll, ll> mr, mc;
    set<ll> sr, sc;
    for (i = 0; i < m; i++) {
      ll x, y;
      cin >> x >> y;
      vx.pb(mp(x, y));
      mr[x]++;
      mc[y]++;
      sr.insert(x);
      sc.insert(y);
    }
    ll row = -1;
    ll low1 = 0;
    vll vv;
    for (auto it = sr.begin(); it != sr.end(); ++it) {
      ll val = *it;
      if (mr[val] > low1) {
        vv.clear();
        vv.pb(val);
        low1 = mr[val];
      } else if (mr[val] == low1)
        vv.pb(val);
    }
    ll low2 = 0;
    vll vvv;
    for (auto it = sc.begin(); it != sc.end(); ++it) {
      ll val = *it;
      if (mc[val] > low2) {
        vvv.clear();
        vvv.pb(val);
        low2 = mc[val];
      } else if (mc[val] == low2)
        vvv.pb(val);
    }
    for (i = 0; i < vv.size(); i++) {
      for (j = 0; j < vvv.size(); j++) {
        ll rr = 0;
        for (k = 0; k < vx.size(); k++) {
          if (vx[k].ff == vv[i] && vx[k].ss == vvv[j]) {
            rr = 1;
            br;
          }
        }
        if (rr == 0) {
          q = 1;
          br;
        }
      }
      if (q == 1)
        br;
    }
    if (q == 1)
      cout << low1 + low2;
    else
      cout << low1 + low2 - 1 nl;
    // cout<<vv.size()<<" "<<vvv.size();
  }
  //////////////////////////////////
}