Problem:
 Score : 500 points    Problem Statement We have a two-dimensional grid with H \times W squares. There are M targets to destroy in this grid - the position of the i -th target is \left(h_i, w_i \right) .  Takahashi will choose one square in this grid, place a bomb there, and ignite it. The bomb will destroy all targets that are in the row or the column where the bomb is placed. It is possible to place the bomb at a square with a target.  Takahashi is trying to maximize the number of targets to destroy. Find the maximum number of targets that can be destroyed.      Constraints  All values in input are integers.  1 \leq H, W \leq 3 \times 10^5  1 \leq M \leq \min\left(H\times W, 3 \times 10^5\right)  1 \leq h_i \leq H  1 \leq w_i \leq W  \left(h_i, w_i\right) \neq \left(h_j, w_j\right) \left(i \neq j\right)         Input Input is given from Standard Input in the following format:  H  W  M  h_1  w_1  \vdots  h_M  w_M       Output Print the answer.        Sample Input 1 2 3 3
2 2
1 1
1 3      Sample Output 1 3  We can destroy all the targets by placing the bomb at \left(1, 2\right) .       Sample Input 2 3 3 4
3 3
3 1
1 1
1 2      Sample Output 2 3       Sample Input 3 5 5 10
2 5
4 3
2 3
5 5
2 2
5 4
5 3
5 1
3 5
1 4      Sample Output 3 6

Solution (Python):
import bisect, copy, heapq, math, sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
def celi(a,b):
    return -(-a//b)
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

def main():
    h,w,m=map(int,input().split())
    hw=[list(map(int,input().split())) for i in range(m)]

    hlst=[[0,i] for i in range(h)]    
    wlst=[[0,i] for i in range(w)]
    dic={}
    for i in range(m):
        hh,ww=hw[i]
        hlst[hh-1][0]-=1
        wlst[ww-1][0]-=1
        dic[(hh-1,ww-1)]=1

    hlst.sort()
    wlst.sort()
    hkouho=[]
    wkouho=[]
    mi=0
    for i in range(h):
        if mi<hlst[i][0]:
            break
        hkouho.append(hlst[i][1])
        mi=hlst[i][0]
    mi=0
    for i in range(w):
        if mi<wlst[i][0]:
            break
        wkouho.append(wlst[i][1])
        mi=wlst[i][0]
    
    
    
    for i in range(len(hkouho)):
        for j in range(len(wkouho)):
            if not (hkouho[i],wkouho[j]) in dic:
                print(-hlst[0][0]-wlst[0][0])
                exit()
    print(-hlst[0][0]-wlst[0][0]-1)
    
main()

Solution (Python):
from collections import defaultdict

h, w, m = map(int, input().split())

cnt_h = [0] * h 
cnt_w = [0] * w 
dic = defaultdict(int) 

for _ in range(m): 
  h_m, w_m = map(int, input().split()) 
  cnt_h[h_m -1] += 1 
  cnt_w[w_m -1] += 1 
  dic[(h_m - 1, w_m - 1)] += 1 

max_h = max(cnt_h)
max_w = max(cnt_w)

mh = []
mw = []

for i in range(h):
  if max_h == cnt_h[i]:
    mh.append(i)
    
for j in range(w):
  if max_w == cnt_w[j]:
    mw.append(j)

for i in mh:
  for j in mw:
    if dic[(i, j)] == 0:
      print(max_h + max_w)
      exit()

print(max_h + max_w - 1)

Solution (Python):
from itertools import product
from sys import exit

def maxindexes(L,Lmax):
    ret = []
    for i in range(len(L)):
        if L[i] == Lmax:
            ret.append(i)
    
    return ret


H,W,M = map(int,input().split())
row = [0]*H
col = [0]*W
bomb = set([])

for _ in range(M):
    a,b = (int(x)-1 for x in input().split())
    row[a] += 1
    col[b] += 1
    bomb.add((a,b))

rmax = max(row)
cmax = max(col)

rmaxind= maxindexes(row,rmax)
cmaxind = maxindexes(col,cmax)
prod = product(rmaxind,cmaxind)

for p in prod:
    if p not in bomb:
        print(rmax+cmax)
        exit(0)

print(rmax+cmax-1)

Solution (Python):
H,W,M=map(int, input().split())
sumh=[0]*H
sumw=[0]*W
bomb=[]
for i in range(M):
  h,w=map(int, input().split())
  sumh[h-1]+=1
  sumw[w-1]+=1
  bomb.append((h,w))

maxh=max(sumh)
maxw=max(sumw)
ch=sumh.count(maxh)
cw=sumw.count(maxw)

cnt=0
for h,w in bomb:
  if sumh[h-1]==maxh and sumw[w-1]==maxw:
    cnt+=1

if cnt==ch*cw:
  print(maxh+maxw-1)
else:
  print(maxh+maxw)

Solution (Python):
h, w, m = map(int, input().split())

h_bomb = [0 for i in range(h)]
w_bomb = [0 for i in range(w)]
bomb_list = []

for i in range(m):
    hb, wb = map(int, input().split())
    h_bomb[hb-1] += 1
    w_bomb[wb-1] += 1
    bomb_list.append(tuple([hb, wb]))

h_bomb_max = max(h_bomb)
w_bomb_max = max(w_bomb)
h_bomb_line = []
w_bomb_line = []

for i in range(h):
    if h_bomb[i] == h_bomb_max:
        h_bomb_line.append(i)

for i in range(w):
    if w_bomb[i] == w_bomb_max:
        w_bomb_line.append(i)

bomb_set = set(bomb_list)

for i in h_bomb_line:
    for j in w_bomb_line:
        if (i+1, j+1) not in bomb_set:
            print(h_bomb_max+w_bomb_max)
            exit()

print(h_bomb_max + w_bomb_max -1)

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

h,w,m = inpl()
yd = []
for i in range(h):
    yd.append([i,0])
xd = []
for i in range(w):
    xd.append([i,0])
se = set()
def ind(i,j):
    return i*w + j
for _ in range(m):
    a,b = inpl()
    yd[a-1][1] += 1
    xd[b-1][1] += 1
    se.add((a-1,b-1))
yd.sort(key = lambda x:x[1], reverse = True)
xd.sort(key = lambda x:x[1], reverse = True)
ymax = yd[0][1]
yl = []
for i,x in yd:
    if x != ymax: break
    yl.append(i)
xmax = xd[0][1]
xl = []
for i,x in xd:
    if x != xmax: break
    xl.append(i)
res = ymax + xmax - 1

for i in yl:
    for j in xl:
        if not (i,j) in se:
            res += 1
            print(res)
            quit()
print(res)

Solution (Python):
H,W,M = map(int,input().split())
h = []
w = []

for i in range(M):
    tmp1,tmp2 = map(int,input().split())
    h.append(tmp1)
    w.append(tmp2)

dic1 = {}
dic2 = {}

for i in h:
    if i not in dic1:
        dic1[i] = 1
    else:
        dic1[i] +=1

val1 = max(dic1,key=dic1.get)
ans = 0
ans +=dic1[val1]
for j in range(M):
    if h[j] != val1:
        if w[j] not in dic2:
            dic2[w[j]] = 1
        else:
            dic2[w[j]] +=1
if dic2:
    val2 = max(dic2,key=dic2.get)
    ans +=dic2[val2]


dic1 = {}
dic2 = {}

for i in w:
    if i not in dic1:
        dic1[i] = 1
    else:
        dic1[i] +=1
val1 = max(dic1,key=dic1.get)
ans1 = 0
ans1 +=dic1[val1]
for j in range(M):
    if w[j] != val1:
        if h[j] not in dic2:
            dic2[h[j]] = 1
        else:
            dic2[h[j]] +=1
if dic2:
    val2 = max(dic2,key=dic2.get)
    ans1 +=dic2[val2]
print(max(ans,ans1))

Solution (Python):
h, w, m = map(int, input().split())
H = [0 for i in range(h)]
W = [0 for i in range(w)]
s = set()
for i in range(m):
    a,b = map(int, input().split())
    H[a-1] += 1
    W[b-1] += 1
    s.add((a,b))
hma = max(H)
wma = max(W)
h_index = [i+1 for i, x in enumerate(H) if x == hma]
w_index = [i+1 for i, x in enumerate(W) if x == wma]
val = -1
for j in range(len(h_index)):
    for k in range(len(w_index)):
        if (h_index[j],w_index[k]) in s:
            continue
        else:
            val = 0
            break
print(hma+wma+val)

Solution (Python):
def main(sample_file = ''):

    """ convenient functions
    
    
    
    
    
    """

    """convenient decorator
    
    
        
        
        
        
        
        
        
        
        
        
    """
    
    
    import sys
    sys.setrecursionlimit(10**7)
    from itertools import accumulate, combinations, permutations, product, combinations_with_replacement 
    
    from math import factorial, ceil, floor, sqrt
    def factorize(n):
        """return the factors of the Arg and count of each factor
        
        Args:
            n (long): number to be resolved into factors
        
        Returns:
            list of tuples: factorize(220) returns [(2, 2), (5, 1), (11, 1)]
        """
        fct = []  
        b, e = 2, 0  
        while b * b <= n:
            while n % b == 0:
                n = n // b
                e = e + 1
            if e > 0:
                fct.append((b, e))
            b, e = b + 1, 0
        if n > 1:
            fct.append((n, 1))
        return fct
    def combinations_count(n, r):   
        """Return the number of selecting r pieces of items from n kinds of items.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        
        if n < 0 or r < 0:
            raise Exception('combinations_count(n, r) not defined when n or r is negative')
        if n - r < r: r = n - r
        if r < 0: return 0
        if r == 0: return 1
        if r == 1: return n
        numerator = [n - r + k + 1 for k in range(r)]
        denominator = [k + 1 for k in range(r)]
        for p in range(2,r+1):
            pivot = denominator[p - 1]
            if pivot > 1:
                offset = (n - r) % p
                for k in range(p-1,r,p):
                    numerator[k - offset] /= pivot
                    denominator[k] /= pivot
        result = 1
        for k in range(r):
            if numerator[k] > 1:
                result *= int(numerator[k])
        return result
    def combinations_with_replacement_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items allowing individual elements to be repeated more than once.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        if n < 0 or r < 0:
            raise Exception('combinations_with_replacement_count(n, r) not defined when n or r is negative')
        elif n == 0:
            return 1
        else:
            return combinations_count(n + r - 1, r)
    from bisect import bisect_left, bisect_right
    from collections import deque, Counter, defaultdict 
    from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest 
    from copy import deepcopy, copy 
    import operator
    from operator import itemgetter 
    
    
    from functools import reduce, lru_cache
    def chmin(x, y):
        """change minimum
        if x > y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current minimum value
            y (long): potential minimum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x > y:
            x = y
            return (x, True)
        else:
            return (x, False)
    def chmax(x, y):
        """change maximum
        if x < y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current maximum value
            y (long): potential maximum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x < y:
            x = y
            return (x, True)
        else:
            return (x, False)

    from fractions import gcd 
    def gcds(numbers):
        return reduce(gcd, numbers)
    def lcm(x, y):
        return (x * y) // gcd(x, y)
    def lcms(numbers):
        return reduce(lcm, numbers, 1)
    def make_divisors(n, reversed=False):
        """create list of divisors
        
        Args:
            number (int): number from which list of divisors is created
            reversed (bool, optional): ascending order if False. Defaults to False.
        
        Returns:
            list: list of divisors
        """
        divisors = set()
        for i in range(1, int(n**0.5)+1):
            if n % i == 0:
                divisors.add(i)
                divisors.add(n//i)
        return sorted(list(divisors),reverse=reversed)
    
    
    
    INF = 10 ** 18
    MOD = 10 ** 9 + 7
    AtoZ = list('abcdefghijklmnopqrsruvwxyz') 
    modpow = lambda a, n, p = MOD: pow(a, n, p) 
    def modinv(a, p = MOD):
        
        
        return modpow(a, p-2, p)
    def modinv_list(n, p = MOD):
        if n <= 1:
            return [0,1][:n+1]
        else:
            inv_t = [0,1]
            for i in range(2, n+1):
                inv_t += [inv_t[p % i] * (p - int(p / i)) % p]
            return inv_t
    def modfactorial_list(n, p = MOD):
        if n == 0:
            return [1]
        else:
            l = [0] * (n+1)
            tmp = 1
            for i in range(1, n+1):
                tmp = tmp * i % p
                l[i] = tmp
            return l
    def modcomb(n, k, fac_list = [], p = MOD):
        
        
        from math import factorial
        if n < 0 or k < 0 or n < k: return 0
        if n == 0 or k == 0: return 1
        if len(fac_list) <= n:
            a = factorial(n) % p
            b = factorial(k) % p
            c = factorial(n-k) % p
        else:
            a = fac_list[n]
            b = fac_list[k]
            c = fac_list[n-k]
        return (a * modpow(b, p-2, p) * modpow(c, p-2, p)) % p
    def modadd(a, b, p = MOD):
        return (a + b) % MOD
    def modsub(a, b, p = MOD):
        return (a - b) % p
    def modmul(a, b, p = MOD):
        return ((a % p) * (b % p)) % p
    def moddiv(a, b, p = MOD):
        return modmul(a, modpow(b, p-2, p))

    class UnionFindTree:
        """union find tree class
        TODO: fix this description...
        how to use (example):
        >>  uf = UnionFindTree(N) 
        >>  if uf.find_root(a) == uf.find_root(b):
        >>      do something
        >>  else:
        >>      do something
        >>      uf.unite(a, b)
        """
        def __init__(self, N):
            self.root = [-1] * (N+1)
            self.rank   = [0]  * (N+1)
            self.connected_num = [1] * (N+1)
    
        def find_root(self,x):
            root = self.root
            while root[x] != -1:
                x = root[x]
            return x
    
        def unite(self,x,y):
            root = self.root
            rank = self.rank
            connected_num = self.connected_num
            find_root = self.find_root
    
            rx = find_root(x)
            ry = find_root(y)
            if rx != ry:
                if rank[rx] < rank[ry]:
                    root[rx] = ry
                    rx,ry = ry,rx
                else:
                    if rank[rx] == rank[ry]:
                        rank[rx] += 1
                    root[ry] = rx       
                connected_num[rx] += connected_num[ry]

    class SegTree:
        """
        ref: https://qiita.com/takayg1/items/c811bd07c21923d7ec69
        init(init_val, ide_ele): 配列init_valで初期化 O(N)
        update(k, x): k番目の値をxに更新 O(logN)
        query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN)
        """
        def __init__(self, init_val, segfunc, ide_ele):
            """
            init_val: 配列の初期値
            segfunc: 区間にしたい操作
            ide_ele: 単位元
            n: 要素数
            num: n以上の最小の2のべき乗
            tree: セグメント木(1-index)
            """
            n = len(init_val)
            self.segfunc = segfunc
            self.ide_ele = ide_ele
            self.num = 1 << (n - 1).bit_length()
            self.tree = [ide_ele] * 2 * self.num
            
            for i in range(n):
                self.tree[self.num + i] = init_val[i]
            
            for i in range(self.num - 1, 0, -1):
                self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

        def update(self, k, x):
            """
            k番目の値をxに更新
            k: index(0-index)
            x: update value
            """
            k += self.num
            self.tree[k] = x
            while k > 1:
                self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
                k >>= 1

        def query(self, l, r):
            """
            [l, r)のsegfuncしたものを得る
            l: index(0-index)
            r: index(0-index)
            """
            res = self.ide_ele
            l += self.num
            r += self.num
            while l < r:
                if l & 1:
                    res = self.segfunc(res, self.tree[l])
                    l += 1
                if r & 1:
                    res = self.segfunc(res, self.tree[r - 1])
                l >>= 1
                r >>= 1
            return res

    
    
    
    
    
    
    
    

    def BellmanFord(N, M, ABC, vertex_start, vertex_end, value_if_inf = -1, find_shortest = False):
        """to calculate furthest or shortest length between vertex_start and vertex_end using BellmanFord algorithm
        
        Args:
            N (int): number of vertices
            M (int): number of edges
            ABC (list): [(ai, bi, ci) for _ in range(N)] where i-th edge is directed from vertex ai to vertex bi and the length is ci 
            vertex_start (int): start vertex. usually use 0.
            vertex_end (int): end vertex. usually use N-1.
            value_if_inf (int or string as you like, optional): value you want when the furthest (or shortest) distance is infinite (or -infinite). Defaults to -1.
            find_shortest (bool, optional): choose False to find furthest path. Defaults to False.
        
        Returns:
            int or string: normally int (but can be str if you set value_if_inf to str)
            
        Example:
                N, M, P = R()
                ABC = [R() for _ in range(M)]
                ABC = [(a-1, b-1, c-P) for a, b, c in ABC]
                print(BellmanFord(N, M, ABC, 0, N-1, value_if_inf = 'inf'))

        """

        def make_reachable_list(N, M, ABC, vertex_start, vertex_end):
            reachable_to_direct = defaultdict(list)
            reachable_from_direct = defaultdict(list)
            reachable_from_start = [False] * N
            reachable_to_end = [False] * N
            reachable_from_start[vertex_start] = True
            reachable_to_end[vertex_end] = True
            reachable_from_both_sides = [False] * N
            dfs_from_start = []
            dfs_to_end = []
            for a, b, c in ABC:
                reachable_to_direct[a].append(b)
                reachable_from_direct[b].append(a)
                if a == vertex_start:
                    dfs_from_start.append(b)
                    reachable_from_start[b] = True
                if b == vertex_end:
                    dfs_to_end.append(a)
                    reachable_to_end[a] = True
            while dfs_from_start:
                v = dfs_from_start.pop()
                for i in reachable_to_direct[v]:
                    if not reachable_from_start[i]:
                        reachable_from_start[i] = True
                        dfs_from_start.append(i)
            while dfs_to_end:
                v = dfs_to_end.pop()
                for i in reachable_from_direct[v]:
                    if not reachable_to_end[i]:
                        reachable_to_end[i] = True
                        dfs_to_end.append(i)
            for i in range(N):
                reachable_from_both_sides[i] = reachable_from_start[i] and reachable_to_end[i]
            return reachable_from_both_sides
        
        reachable_from_both_sides = make_reachable_list(N, M, ABC, vertex_start, vertex_end)
        
        if find_shortest:
            dist = [INF for i in range(N)]
        else:
            dist = [-INF for i in range(N)]
        
        dist[vertex_start] = 0
        for i in range(N):
            updated = False
            for a, b, c in ABC:
                if not reachable_from_both_sides[a]:
                    continue
                elif find_shortest:
                    update_condition = dist[a] + c < dist[b]
                else:
                    update_condition = dist[a] + c > dist[b]
                if dist[a] != INF and update_condition:
                    dist[b] = dist[a] + c
                    updated = True
                    if i == N-1:
                        return value_if_inf
            if not updated:
                break
        return dist[vertex_end]

    
    def warshall_floyd(number_vertex, XYD, shift_one_for_vertex=False):
        
        
        
        
        
        d = [[float("inf")]*number_vertex for i in range(number_vertex)]
        for x, y, dist in XYD:
            if shift_one_for_vertex:
                x = x - 1
                y = y - 1
            d[x][y] = dist
            d[y][x] = dist
        for i in range(number_vertex):
            d[i][i] = 0 
        for k in range(number_vertex):
            for i in range(number_vertex):
                for j in range(number_vertex):
                    d[i][j] = min(d[i][j],d[i][k] + d[k][j])
        return d


    """ initialize variables and set inputs
    
        
        
            
            
    
        
        
        
        
        
        
        
    
        
        
        
        
    
        
        
    """

    
    r = lambda: sys.stdin.readline().strip()
    r_int = lambda: int(r())
    r_float = lambda: float(r())
    R = lambda: list(map(int, r().split()))
    R_str = lambda: r().split()
    R_map = lambda: map(int, r().split())
    R_float = lambda: list(map(float, r().split()))
    R_tuple = lambda: tuple(map(int, r().split()))

    """ how to treat input
    
    
    
    
    
    
    
    """

    
    if sample_file:
        sys.stdin = open(sample_file)
        
    
    
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    H, W, M = R()
    HW = [R() for _ in range(M)]
    HL = [0] * H
    WL = [0] * W
    
    set_bomb = set()
    
    for h, w in HW:
        HL[h-1] += 1
        WL[w-1] += 1
        
        set_bomb.add((h-1, w-1))
    MAX_HL = []
    MAX_WL = []
    max_h = max(HL)
    max_w = max(WL)
    
    for i, h in enumerate(HL):
        if h == max_h:
            MAX_HL.append(i)
    for i, w in enumerate(WL):
        if w == max_w:
            MAX_WL.append(i)
    
    for h in MAX_HL:
        for w in MAX_WL:
            if (h, w) not in set_bomb:
            
                print(max_h + max_w)
                return
    print(max_h + max_w - 1)
                
        
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    


    
    
    

    """memo: how to use defaultdict of list
    
    Dic = defaultdict(list)
    
    Dic[x].append(y)
    
    for k, v in Dic.items():
    """
    
    """memo: how to solve binary problems
    
    >>> a = 5
    >>> bin_str_a = format(a, '
    >>> print(bin_str_a)
    0b0101 
    """
    
    """memo: how to solve the problem
    creating simple test/answer
    greed
    simple dp
    graph
    """

if __name__ == '__main__':
    main()

Solution (Python):
h, w, m = map(int, input().split())
a = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(m)]
mh = [0 for i in range(h)]
mw = [0 for j in range(w)]

for i, j in a:
    mh[i] += 1
    mw[j] += 1 

hmax = max(mh)
wmax = max(mw)
ans = hmax + wmax

flag = mh.count(hmax) * mw.count(wmax)
for i,j in a:
    if mh[i] == hmax and mw[j] == wmax:
        flag -= 1

if flag == 0:
    print(ans - 1)
else:
    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
 
H, W, M = map(int, input().split())
Hsum = [0] * H
Wsum = [0] * W
hw = set()
for m in range(M):
  h, w = map(lambda x: int(x) - 1, input().split())
  Hsum[h] += 1
  Wsum[w] += 1
  hw.add((h, w))

mh = max(Hsum)
mw = max(Wsum)
hidx = [i for i, _ in enumerate(Hsum) if _ == mh]
widx = [i for i, _ in enumerate(Wsum) if _ == mw]
 
if len(hidx) * len(widx) > M:
  print(mh + mw)
else:
  fi_flg = False
  for h in hidx:
    for w in widx:
      if (h, w) not in hw:
        print(mh + mw)
        fi_flg = True
        break
    if fi_flg:
      break
  if fi_flg is False:
    print(mh + mw - 1)

Solution (Python):
H, W, M = map(int, input().split())

HL, WL = [0]*H, [0]*W
B = []

for _ in range(M):
  h, w = map(lambda x:int(x)-1, input().split())
  B.append((h, w))
  HL[h] += 1
  WL[w] += 1

HM, WM = max(HL), max(WL)
K = HL.count(HM) * WL.count(WM)

cnt = 0
for h, w in B:
  if HL[h] == HM and WL[w] == WM:
    cnt += 1

if K > cnt:
  print(HM+WM)
else:
  print(HM+WM-1)

Solution (Python):





import sys;          from decimal import Decimal
import math;         from numba import njit, i8, u1, b1
import bisect;       from itertools import combinations, product
import numpy as np;  from collections import Counter, deque, defaultdict


MOD = 10 ** 9 + 7
INF = 10 ** 9
PI = 3.14159265358979323846

def read_str():      return sys.stdin.readline().strip()
def read_int():      return int(sys.stdin.readline().strip())
def read_ints(x=0):  return map(lambda num: int(num) - x, sys.stdin.readline().strip().split())
def read_str_list(): return list(sys.stdin.readline().strip().split())
def read_int_list(): return list(map(int, sys.stdin.readline().strip().split()))
def lcm(a: int, b: int) -> int: return (a * b) // math.gcd(a, b)

def Main():
    h, w, m = read_ints()
    bomb = set([tuple(read_ints(x=1)) for _ in range(m)])
    
    row = [0] * h
    column = [0] * w
    for y, x in bomb:
        row[y] += 1
        column[x] += 1
        
    max_row = max(row)
    max_col = max(column)
    
    max_row_lis = [i for i, y in enumerate(row) if y == max_row]
    max_col_lis = [i for i, x in enumerate(column) if x == max_col]

    for r in max_row_lis:
        for c in max_col_lis:
            if (r, c) not in bomb:
                print(max_col + max_row)
                exit()

    print(max_col + max_row - 1)


if __name__ == '__main__':
    Main()

Solution (Python):
import sys
import bisect
import itertools
import collections
import fractions
import heapq
import math
from operator import mul
from functools import reduce
from functools import lru_cache


def solve():
    readline = sys.stdin.buffer.readline
    input = readline
    mod = 10 ** 9 + 7
    H, W, M = map(int, input().split())
    Hlist = collections.defaultdict(int)
    Wlist = collections.defaultdict(int)
    hw = []

    for _ in range(M):
        h, w = map(int, input().split())
        Hlist[h-1] += 1
        Wlist[w-1] += 1
        hw.append([h, w])

    
    

    Hlist = sorted(Hlist.items(), key=lambda x: x[1], reverse=True)
    Wlist = sorted(Wlist.items(), key=lambda x: x[1], reverse=True)

    hmaxset = set()
    wmaxset = set()

    maxH = Hlist[0][1]
    for k, v in Hlist:
        if v != maxH:
            break
        else:
            hmaxset.add(k)

    maxW = Wlist[0][1]
    for k, v in Wlist:
        if v != maxW:
            break
        else:
            wmaxset.add(k)


    flag = False
    count = 0
    for hh in hw:
        h = hh[0] - 1
        w = hh[1] - 1
        if h in hmaxset and w in wmaxset:
            count += 1

    lenhmax = len(hmaxset)
    lenwmax = len(wmaxset)

    if count != lenhmax * lenwmax:
        flag = True

    if flag:
        print(maxH + maxW)
    else:
        print(maxH + maxW - 1)





if __name__ == '__main__':
    solve()

Solution (Python):
H, W, M = map(int, input().split())

H_bomb = [0 for i in range(H)]
W_bomb = [0 for i in range(W)]
bomb_list = []

for i in range(M):
    M_temp = tuple(map(int, input().split()))
    H_bomb[M_temp[0]-1] += 1
    W_bomb[M_temp[1]-1] += 1
    bomb_list.append(M_temp)


H_bomb_max = max(H_bomb)
W_bomb_max = max(W_bomb)
H_bomb_line = []
W_bomb_line = []

for i in range(H):
    if H_bomb[i] == H_bomb_max:
        H_bomb_line.append(i)
for i in range(W):
    if W_bomb[i] == W_bomb_max:
        W_bomb_line.append(i)

bomb_set = set(bomb_list)
for i in H_bomb_line:
    for j in W_bomb_line:
        if (i+1, j+1) not in bomb_set:
            print(H_bomb_max + W_bomb_max)
            exit()
print(H_bomb_max + W_bomb_max - 1)

Solution (Python):
from collections import Counter
from collections import defaultdict
from collections import deque
from heapq import heapify
from heapq import heappop
from heapq import heappush


h,w,m=[int(x) for x in input().split()]


hcnt=defaultdict(int)
wcnt=defaultdict(int)


bomb=defaultdict()

for _ in range(m):
    hi,wi=[int(x)-1 for x in input().split()]
    hcnt[hi]+=1
    wcnt[wi]+=1
    bomb[(hi,wi)]=True


hmax=max(hcnt.values())
wmax=max(wcnt.values())


hidx=[x for x,y in hcnt.items() if y==hmax]
widx=[x for x,y in wcnt.items() if y==wmax]


ofst=-1
for i in hidx:
    for j in widx:
        if not bomb.get((i,j)):
            ofst=0
            break
                
print(hmax+wmax+ofst)

Solution (Python):
import sys


H, W, M = map(int, input().split())
bomb = [tuple(map(lambda x: int(x) - 1, s.split())) for s in sys.stdin.readlines()]
X = [0] * H  
Y = [0] * W  
for h, w in bomb:
    X[h] += 1
    Y[w] += 1
maxX = max(X)
maxY = max(Y)

R = [h for h, x in enumerate(X) if x == maxX]  
C = [w for w, y in enumerate(Y) if y == maxY]  

bomb = set(bomb)
for r in R:
    for c in C:
        
        
        if (r, c) not in bomb:
            print(maxX + maxY)
            exit()
print(maxX + maxY - 1)

Solution (Python):
def main():
    h, w, m = map(int, input().split())
    bombs_h = [0] * h
    bombs_w = [0] * w
    B = []
    for n in range(m):
        h1, w1 = map(lambda x: int(x)-1, input().split())
        B.append((h1, w1))
        bombs_w[w1] += 1
        bombs_h[h1] += 1
    max_h = max(bombs_h)
    max_w = max(bombs_w)
    h_maxindex = [i for i, x in enumerate(bombs_h) if x == max_h]
    w_maxindex = [i for i, x in enumerate(bombs_w) if x == max_w]
    cnt = 0
    k = len(h_maxindex) * len(w_maxindex)
    for i, j in B:
        if bombs_h[i] == max_h and bombs_w[j] == max_w:
            cnt += 1
    ans = max_h + max_w - (cnt == k)
    print(ans)
    
    
if __name__ == "__main__":
    main()

Solution (Python):
h,w,m=map(int,input().split())
x=[0]*h
y=[0]*w
c=[]
for _ in range(m):
  a,b=map(int,input().split())
  x[a-1]+=1
  y[b-1]+=1
  c+=[(a-1,b-1)]
mx=max(x)
my=max(y)
cnt=0
for i,j in c:
  if x[i]==mx and y[j]==my:cnt+=1
print(mx+my-(cnt==x.count(mx)*y.count(my)))

Solution (Python):

import sys
input = lambda: sys.stdin.readline().rstrip()


h, w, m = map(int, input().split())
hw = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(m)]


hcount = [0] * h
wcount = [0] * w
for a, b in hw:
  hcount[a] += 1
  wcount[b] += 1


hmax = max(hcount)
wmax = max(wcount)


x = hcount.count(hmax) * wcount.count(wmax)


for a, b in hw:
  if hcount[a] == hmax and wcount[b] == wmax:
    x -= 1



if x == 0:
  print(hmax + wmax - 1)
else:
  print(hmax + wmax)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()

h, w, m = map(int, input().split())

hw = []
hcount = [0] * h
wcount = [0] * w
for _ in range(m):
  a, b = map(int, input().split())
  a -= 1
  b -= 1
  hcount[a] += 1
  wcount[b] += 1
  hw.append([a, b])

hmax = max(hcount)
wmax = max(wcount)

x = hcount.count(hmax) * wcount.count(wmax)
for a, b in hw:
  if hcount[a] == hmax and wcount[b] == wmax:
    x -= 1

if x == 0:
  print(hmax + wmax - 1)
else:
  print(hmax + wmax)