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

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

int main() {

  int h, w, m;
  cin >> h >> w >> m;

  unordered_map<int, int> row, colmn;

  map<pair<int, int>, int> tp;

  int max_val_row = 0, max_val_colmn = 0;

  for (int i = 1; i <= m; i++) {

    int x, y;
    cin >> x >> y;

    pair<int, int> g = make_pair(x, y);
    tp[g]++;

    row[x]++;
    colmn[y]++;

    if (max_val_row < row[x])
      max_val_row = row[x];

    if (max_val_colmn < colmn[y])
      max_val_colmn = colmn[y];
  }

  vector<int> row_max_var, col_max_var;

  //    cout<<max_val_row<<" "<<max_val_colmn<<endl;

  for (auto i = 1; i <= h; i++) {
    // cout<<i->first<<" "<<i->second<<endl;
    if (row[i] == max_val_row)
      row_max_var.push_back(i);
  }

  for (auto i = 1; i <= w; i++) {
    if (colmn[i] == max_val_colmn)
      col_max_var.push_back(i);
  }

  int val_max = 0;
  bool flag = false;

  for (int i = 0; i < row_max_var.size(); i++) {
    for (int j = 0; j < col_max_var.size(); j++) {

      pair<int, int> f = make_pair(row_max_var[i], col_max_var[j]);
      if (tp[f] > 0)
        val_max = max(val_max, max_val_row + max_val_colmn - 1);

      else {
        val_max = max(val_max, max_val_row + max_val_colmn);
        flag = true;
        break;
      }
    }
    if (flag)
      break;
  }

  /*
      int val_max = 0;
       for(auto it = tp.begin();it!=tp.end();it++){

             int first = it->first.first;
             int second = it->first.second;
       }*/
  cout << val_max;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
/*
string subs(string s,int a,int b)
{
    string s1="";

    for(int i = a; i < b; i++)
        s1 = s1 + s[i];

    return s1;
}
vector<string> palindromic(string s)
{
    vector<string> v ;

    for (float i = 0; i < s.length();
                                 i += .5)
    {
        float Radius = i -(int)i;
        while ((i + Radius)
         < s.length() && (i - Radius)
         >= 0 && s[((int)(i - Radius))]
             == s[((int)(i + Radius))])
        {

    v.push_back(subs(s,(int)(i -Radius), (int)(i + Radius + 1)));

         Radius++;
        }
    }

    return v;
}
int main(){

        string g;
         char str[100000];
           cin>>str;

         for(int i=0;str[i]!='\\0';i++)
             g.append(1,str[i]);

       vector<string> vect = palindromic(g);

         cout<<vect.size();
}*/
int main() {

  int h, w, m;
  cin >> h >> w >> m;

  unordered_map<int, int> row, colmn;

  map<pair<int, int>, int> tp;

  int max_val_row = 0, max_val_colmn = 0;

  for (int i = 1; i <= m; i++) {

    int x, y;
    cin >> x >> y;

    pair<int, int> g = make_pair(x, y);
    tp[g]++;

    row[x]++;
    colmn[y]++;

    if (max_val_row < row[x])
      max_val_row = row[x];

    if (max_val_colmn < colmn[y])
      max_val_colmn = colmn[y];
  }

  vector<int> row_max_var, col_max_var;

  //    cout<<max_val_row<<" "<<max_val_colmn<<endl;

  for (auto i = 1; i <= h; i++) {
    // cout<<i->first<<" "<<i->second<<endl;
    if (row[i] == max_val_row)
      row_max_var.push_back(i);
  }

  for (auto i = 1; i <= w; i++) {
    if (colmn[i] == max_val_colmn)
      col_max_var.push_back(i);
  }

  int val_max = 0;

  for (int i = 0; i < row_max_var.size(); i++) {
    for (int j = 0; j < col_max_var.size(); j++) {

      pair<int, int> f = make_pair(row_max_var[i], col_max_var[j]);
      if (tp[f] > 0)
        val_max = max(val_max, max_val_row + max_val_colmn - 1);

      else
        val_max = max(val_max, max_val_row + max_val_colmn);
    }
  }

  /*
      int val_max = 0;
       for(auto it = tp.begin();it!=tp.end();it++){

             int first = it->first.first;
             int second = it->first.second;
       }*/
  cout << val_max;
}