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):
h,w,m = map(int,input().split())
s = set([])
H = [0]*(h+1)
W = [0]*(w+1)
for i in range(m):
    x,y = map(int,input().split())
    H[x] += 1
    W[y] += 1
    s.add((x,y))
    
p = [0]*(h+1)
q = [0]*(w+1)
for i in range(h+1):
    p[i] = [i, H[i]]
for i in range(w+1):
    q[i] = [i, W[i]]
    
p.sort(key= lambda val : val[1],reverse=True)
q.sort(key= lambda val : val[1],reverse=True)

mh = max(H)
mw = max(W)
I = H.count(mh)
J = W.count(mw)

sw = 0
for i in range(I):
    if sw == 1:
        break
    for j in range(J):
        if (p[i][0], q[j][0]) in s:
            pass
        else:
            sw = 1
            break

print(mh+mw-1+sw)

Solution (Python):
import sys
from collections import defaultdict,Counter
input = sys.stdin.readline
h,w,m=map(int, input().split())
hs=[]
ws=[]
hw=defaultdict(int)
for i in range(m):
    h0,w0=map(int, input().split())
    hw[(h0,w0)]+=1
    hs.append(h0)
    ws.append(w0)

hd=Counter(hs)
wd=Counter(ws)
hmax=max(hd.values())
wmax=max(wd.values())

max_h_list=[kv[0] for kv in hd.items() if kv[1] == hmax]
max_w_list=[kv[0] for kv in wd.items() if kv[1] == wmax]
ans=float('inf')

for x in max_h_list:
    for y in max_w_list:
        if hw[(x,y)]==1:
            ans=min(ans,1)
        else:
            ans=min(ans,0)
            break
print(hmax+wmax-ans)

Solution (Python):
H,W,M=map(int,input().split())
h,w=map(list,zip(*[list(map(int,input().split())) for i in range(M)]))
from collections import Counter
hc,wc=Counter(h),Counter(w)
hx,wx=hc.most_common()[0][1],wc.most_common()[0][1]
hl,wl=set([k for k,v in hc.items() if v==hx]),set([k for k,v in wc.items() if v==wx])
x=sum([1 for i in range(M) if h[i] in hl and w[i] in wl])
print(hx+wx if len(hl)*len(wl)-x else hx+wx-1)

Solution (Python):
from itertools import product

def zero_origin(s: str) -> int:
  return int(s) - 1

H, W, M = map(int, input().split())
h_obj = [0 for _ in range(H)]
w_obj = [0 for _ in range(W)]
obj = set()
for _ in range(M):
  h, w = map(zero_origin, input().split())
  h_obj[h] += 1
  w_obj[w] += 1
  obj.add((h, w))
max_h = max(h_obj)
max_w = max(w_obj)
h_list = [h for h in range(H) if h_obj[h] == max_h]
w_list = [w for w in range(W) if w_obj[w] == max_w]
for h, w in product(h_list, w_list):
  if (h, w) not in obj:
    print(max_h + max_w)
    break
else:
  print(max_h + max_w - 1)

Solution (Python):

import numpy as np


def f(h,w,m,ins):
    yp = np.zeros(h,dtype=np.int32)
    xp = np.zeros(w,dtype=np.int32)
    s = set()
    for hi,wi in ins:
        s.add((hi-1,wi-1))
        yp[hi-1] += 1
        xp[wi-1] += 1
    ypm = yp.max()
    xpm = xp.max()
    yps = np.where(yp == ypm)[0].tolist()
    xps = np.where(xp == xpm)[0].tolist()
    ans = yp[yps[0]]+xp[xps[0]]
    for ypsi in yps:
        for xpsi in xps:
            if not (ypsi,xpsi) in s:
                return ans
    return ans-1

if __name__ == "__main__":
    if False:
        while True:
            h,w = randint(1,10**5*3),randint(10**5,10**5*3)
            m = randint(1,min(h*w,10**5*3))
            ins = [(randint(1,h),randint(1,w)) for i in range(m)]
            ans = f(h,w,m,ins)
            print(ans)
    else:
        h,w,m = map(int,input().split())
        ans = f(h,w,m,[list(map(int,input().split())) for i in range(m)])
        print(ans)

Solution (Python):
from collections import Counter

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

bom_v = Counter(b[0] for b in beds)
bom_h = Counter(b[1] for b in beds)

v_max = max(bom_v.values())
h_max = max(bom_h.values())

cand = list(bom_v.values()).count(v_max) * list(bom_h.values()).count(h_max)
bombs = len([... for i, j in beds if bom_v[i] ==  v_max and bom_h[j] ==  h_max])

if cand - bombs > 0:
    print(v_max + h_max)
else:
    print(v_max + h_max - 1)

Solution (Python):
import sys
 
h, w, m = map(int, input().split(' '))
H = [0 for _ in range(h+1)]
W = [0 for _ in range(w+1)]
bomb = set()
for m in range(m):
  i, j = map(int, input().split(' '))
  bomb.add((i, j))
  H[i] += 1
  W[j] += 1
  
max_h = max(H)
max_w = max(W)
target_h = [i for i, v in enumerate(H) if v == max_h]
target_w = [i for i, v in enumerate(W) if v == max_w]
 
for m in target_h:
  for n in target_w:
    if (m, n) not in bomb:
      print(max_h + max_w)
      sys.exit()
	
print(max_h + max_w - 1)

Solution (Python):

import numpy as np



def f(h,w,m,ins):
    yp = np.zeros(h,dtype=np.int)
    xp = np.zeros(w,dtype=np.int)
    
    
    ans = 0
    s = set()
    for hi,wi in ins:
        s.add((hi-1,wi-1))
        yp[hi-1] += 1
        xp[wi-1] += 1
    ypm = yp[yp.argmax()]
    xpm = xp[xp.argmax()]
    yps = np.where(yp == ypm)[0].tolist()
    xps = np.where(xp == xpm)[0].tolist()
    
    for ypsi in yps:
        for xpsi in xps:
            a = yp[ypsi]+xp[xpsi]
            if (ypsi,xpsi) in s:
                a -= 1
                ans = max(ans,a)
            else:
                return a
    return ans


if False:
    while True:
        h,w = randint(1,10**5*3),randint(1,10**5*3)
        m = randint(1,min(h*w,10**5*3))
        ins = [(randint(1,h),randint(1,w)) for i in range(m)]
        ans = f(h,w,m,ins)
        print(ans)
else:
    h,w,m = map(int,input().split())
    ans = f(h,w,m,[tuple(map(int,input().split())) for i in range(m)])
    print(ans)

Solution (Python):
import sys

h,w,m = map(int, sys.stdin.readline().split())
num_in_h = [0] * h
num_in_w = [0] * w
targets = set()
for _ in range(m):
    t_h, t_w = map(lambda x: int(x)-1, sys.stdin.readline().split())
    num_in_h[t_h] += 1
    num_in_w[t_w] += 1
    targets.add((t_h, t_w))

max_h = max(num_in_h)
max_w = max(num_in_w)
max_h_set = set([i for i, v in enumerate(num_in_h) if v == max_h])
max_w_set = set([i for i, v in enumerate(num_in_w) if v == max_w])
ans = max_h + max_w - 1

flag = False
for i in max_h_set:
    for j in max_w_set:
        if (i, j) not in targets:
            flag = True
            break
    if flag: break

print(ans+1 if flag else ans)

Solution (Python):
import sys
H, W, M = map(int, input().split())
h_list, w_list = [0]*H, [0]*W
check = set()
for _ in range(M):
    h, w = map(int, input().split())
    check.add((h-1, w-1))
    h_list[h-1] += 1
    w_list[w-1] += 1
h_max, w_max = max(h_list), max(w_list)
h_bom = [i for i in range(H) if h_list[i]==h_max]
w_bom = [i for i in range(W) if w_list[i]==w_max]
for h in h_bom:
    for w in w_bom:
        if not (h, w) in check:
            print(h_max+w_max)
            sys.exit()
print(h_max+w_max-1)

Solution (Python):
import sys
h,w,m = map(int,input().split())
hight = [0]*h
width = [0]*w
S = set()

for i in range(m):
    hi,wi = map(int,input().split())
    S.add((hi-1,wi-1))
    hight[hi-1] += 1
    width[wi-1] += 1

max_vh = max(hight)
max_vw = max(width)

max_h = [i for i in range(h) if hight[i] == max_vh]
max_w = [i for i in range(w) if width[i] == max_vw]
for i in max_h:
    for j in max_w:
        if not(i,j) in S:
            print(max_vh+max_vw)
            sys.exit()

print(max_vh+max_vw-1)

Solution (Python):
H,V,M=map(int,input().split())
h=[0 for i in range(M)]
w=[0 for i in range(M)]
for i in range(M):
    h[i],w[i]=map(int,input().split())
Dh=dict()
Dw=dict()
for i in range(M):
    if h[i] in Dh:
        Dh[h[i]]+=1
    else:
        Dh[h[i]]=1
    if w[i] in Dw:
        Dw[w[i]]+=1
    else:
        Dw[w[i]]=1
Hmax=0
Wmax=0
for i in Dh:
    if Dh[i]>Hmax:
        Hmax=Dh[i]
for i in Dw:
    if Dw[i]>Wmax:
        Wmax=Dw[i]
A=0
B=0
for i in Dh:
    if Dh[i]==Hmax:
        A+=1
for i in Dw:
    if Dw[i]==Wmax:
        B+=1
tmp=0
for i in range(M):
    if Dh[h[i]]==Hmax and Dw[w[i]]==Wmax:
        tmp+=1
if A*B>tmp:
    print(Hmax+Wmax)
else:
    print(Hmax+Wmax-1)

Solution (Python):
H,W,M=map(int,input().split())
bomb=[]
h=[0]*H
w=[0]*W
for i in range(M):
    hi,wi=map(int,input().split())
    h[hi-1] += 1
    w[wi-1] += 1
    bomb.append([hi,wi])    
maxh = max(h)
maxw = max(w)
ans = maxh +  maxw
lh=[]
lw=[]
for i in range(len(h)):
    if h[i] == maxh:
        lh.append(i+1)
for i in range(len(w)):
    if w[i] == maxw:
        lw.append(i+1)
a = 0
for i in range(M):
    if ans == h[bomb[i][0]-1]+w[bomb[i][1]-1]:
        a += 1
if a == len(lh)*len(lw):
    print(ans-1)
else:
    print(ans)

Solution (Python):
import sys
H,W,m = map(int, input().split())

high = [0] * H
width = [0] * W

stone = set()
for i in range(m):
    h, w = map(int,input().split())
    h -= 1
    w -= 1
    
    high[h] += 1
    width[w] += 1
    stone.add((h,w))
    
dic_h = {}
for i in range(len(high)):
    dic_h.setdefault(high[i], [])
    
    dic_h[high[i]].append(i)
    
dic_w = {}
for i in range(len(width)):
    dic_w.setdefault(width[i], [])
    
    dic_w[width[i]].append(i)
    
h_max = max(dic_h.keys())
w_max = max(dic_w.keys())

for i in dic_h[h_max]:
    for j in dic_w[w_max]:
        if (i,j) not in stone:
            print(h_max+w_max)
            sys.exit(0)
            
print(h_max+w_max-1)

Solution (Python):
H,W,M=map(int,input().split())
bombh=[]
bombw=[]
bomb=[]
w=[0]*W
h=[0]*H
for i in range(M):
    hs,ws=map(int,input().split())
    w[ws-1]+=1
    h[hs-1]+=1
    bomb.append([hs-1,ws-1])
maxh=max(h)
maxw=max(w)
ans=maxh+maxw
count=0
for i in range(M):
    if ans==h[bomb[i][0]]+w[bomb[i][1]]:
        count+=1
a=h.count(maxh)*w.count(maxw)
if a==count:
    ans-=1
print(ans)

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

bomb = set()
row = [0] * H
column = [0] * W

for i in range(M):
    h, w = map(int, input().split())
    h -= 1
    w -= 1
    
    bomb.add((h, w))
    
    row[h] += 1
    column[w] += 1
    
row_max = max(row)
col_max = max(column)

r_check = []
c_check = []
for i in range(H):
    if row[i] == row_max:
        r_check.append(i)

for i in range(W):
    if column[i] == col_max:
        c_check.append(i)

num = len(r_check) * len(c_check)

ans = row_max + col_max - 1
flg = False
if M < num:
    ans += 1
else:
    for r in r_check:
        if flg:
            break
        for c in c_check:
            if not (r, c) in bomb:
                ans += 1
                flg = True
                break

print(ans)

Solution (Python):
h, w, m = map(int, input().split())
table = []
sum_rows = [0 for i in range(h)]
sum_cols = [0 for i in range(w)]
for i in range(m):
    h_, w_ = map(int, input().split())
    table.append((h_, w_))
    sum_rows[h_-1] += 1
    sum_cols[w_-1] += 1

r = max(sum_rows)
c = max(sum_cols)
ans_ = (r + c)
count = 0
for i in range(m):
    if ans_ == (sum_rows[table[i][0]-1]+sum_cols[table[i][1]-1]):
        count += 1

if count == (sum_rows.count(r) * sum_cols.count(c)):
    print(ans_-1)
else:
    print(ans_)

Solution (Python):
h,w,m=map(int, input().split())
hw=[list(map(int, input().split())) for _ in range(m)]

y_list=[0]*h
x_list=[0]*w

for h_i, w_i in hw:
    y_list[h_i-1]+=1
    x_list[w_i-1]+=1

my_cnt=max(y_list)
mx_cnt=max(x_list)

y_cnt=0
x_cnt=0

my_set=set()
mx_set=set()

for h_i, w_i in hw:
    if y_list[h_i-1]==my_cnt:
        my_set.add(h_i-1)
    if x_list[w_i-1]==mx_cnt:
        mx_set.add(w_i-1)

cnt=0
for h_i, w_i in hw:
    if y_list[h_i-1]==my_cnt and x_list[w_i-1]==mx_cnt:
        cnt+=1

print(my_cnt+mx_cnt-1 if cnt==len(my_set)*len(mx_set) else my_cnt+mx_cnt)

Solution (Python):
H, W, M = map(int,input().split())
R = [0] * H
C = [0] * W

L = []

for _ in range(M):
    h, w = map(int,input().split())
    R[h-1] += 1
    C[w-1] += 1
    L.append([h-1, w-1])

r = max(R)
c = max(C)

print(r + c - (R.count(r) * C.count(c) == sum(R[h] + C[w] == r + c for h, w in L)))

Solution (Python):
import sys
H, W, M = map(int, input().split())
hn = [0]*H
wn = [0]*W
flag = set()
for i in range(M):
  h, w = map(int, input().split())
  hn[h-1] += 1
  wn[w-1] += 1
  flag.add((h,w))
  



hmax = max(hn)
wmax = max(wn)

hmaxlist = {i+1 for i,h in enumerate(hn) if h == hmax}



  
wmaxlist = {i+1 for i,w in enumerate(wn) if w == wmax}



  

for i in hmaxlist:
  for j in wmaxlist:
    if (i,j) not in flag:
      print(hmax+wmax)
      sys.exit()
print(hmax+wmax-1)

Solution (Python):
import sys

H, W, M = map(int,input().split())
R = [0] * H
C = [0] * W

S = set()

for _ in range(M):
    h, w = map(int,input().split())
    S.add((h-1, w-1))
    R[h-1] += 1
    C[w-1] += 1

r = max(R)
c = max(C)

HL = [i for i in range(H) if R[i] == r]
WL = [i for i in range(W) if C[i] == c]

for h in HL:
    for w in WL:
        if not (h, w) in S:
            print(r + c)
            sys.exit()

print(r + c - 1)

Solution (Python):
import sys

H, W, M = map(int,input().split())
R = [0] * H
C = [0] * W
S = set()
for _ in range(M):
    h, w = map(int,input().split())
    S.add((h-1, w-1))
    R[h-1] += 1
    C[w-1] += 1

r = max(R)
c = max(C)

HL = [i for i in range(H) if R[i] == r]
WL = [i for i in range(W) if C[i] == c]

for h in HL:
    for w in WL:
        if not (h, w) in S:
            print(r + c)
            sys.exit()

print(r + c - 1)

Solution (Python):
h,w,m=map(int,input().split())
A=[0]*h ; B=[0]*w ;G=[]
for i in range(m):
    x,y=map(int,input().split())
    A[x-1]+=1 ; B[y-1]+=1
    G.append([x-1,y-1])

q=max(A) ; e=max(B)
Q=[i for i in range(h) if A[i]==q] 
W=[j for j in range(w) if B[j]==e];
cnt=len(Q)*len(W)



for i in range(m):
    x,y=G[i]
    if A[x]==q and B[y]==e:
        cnt-=1

if cnt==0:
    print(q+e-1)
else:
    print(q+e)

Solution (Python):
h,w,m = map(int,input().split())
bombX = [0]*h
bombY = [0]*w
bomb = set()
maxX = maxY = 0
for i in range(m):
    i,j = map(lambda x:int(x)-1, input().split())
    bomb.add((i,j))
    bombX[i] += 1
    bombY[j] += 1
    maxX = max(maxX,bombX[i])
    maxY = max(maxY,bombY[j])
maxX_index = list(i for i in range(h) if bombX[i] == maxX)
maxY_index = list(i for i in range(w) if bombY[i] == maxY)
for i in maxX_index:
    for j in maxY_index:
        if (i,j) in bomb: continue
        print(maxX+maxY)
        exit()
print(maxX+maxY-1)

Solution (Python):
H, W, M = map(int, input().split())
X = [0 for i in range(H)]
Y = [0 for j in range(W)]
s = set([])
for i in range(M):
  h, w = map(int, input().split())
  X[h-1] += 1
  Y[w-1] += 1
  s.add((h-1, w-1)) 
mX = max(X)
mY = max(Y)
I = [i for i in range(H) if X[i] == mX]
J = [j for j in range(W) if Y[j] == mY]
flag = 0
for i in I:
  for j in J:
    if {(i, j)} <= s:
      continue
    flag = 1
    print(mX+mY)
    break
  if flag == 1:
    break
if flag == 0:
  print(mX+mY-1)

Solution (Python):
H,W,M=map(int,input().split())
s=[list(map(int,input().split())) for _ in range(M)]
Tr=[0 for _ in range(H+1)]
Tc=[0 for _ in range(W+1)]
for i in range(M):
    Tr[s[i][0]]+=1
    Tc[s[i][1]]+=1
mr=max(Tr)
mc=max(Tc)
Mr=[0 for _ in range(H+1)]
Mc=[0 for _ in range(W+1)]
Nmr=0
Nmc=0
for i in range(len(Tr)):
    if Tr[i]==mr:
        Mr[i]=1
        Nmr+=1
for i in range(len(Tc)):
    if Tc[i]==mc:
        Mc[i]=1
        Nmc+=1
cnt=0
for i in range(M):
    if Mr[s[i][0]]==1 and Mc[s[i][1]]==1:
        cnt+=1
if cnt<Nmr*Nmc:
    print(mr+mc)
else:
    print(mr+mc-1)

Solution (Python):
x, y, m = map(int,input().split())
mat = set()
r = [0]*x
c = [0]*y
smx = 0
smy = 0
for _ in range(m):
    p, q = map(int,input().split())
    p -= 1
    q -= 1
    mat.add((p,q))
    r[p] += 1
    c[q] += 1
    smx = max(smx,r[p])
    smy = max(smy,c[q])
xs = []
ys = []
for i in range(x):
    if smx == r[i]: xs.append(i)
for i in range(y):
    if smy == c[i]: ys.append(i)

ans = smx + smy - 1

for h in xs:
    for w in ys:
        if (h, w) in mat: continue
        print(ans + 1)
        exit()
print(ans)

Solution (Python):
def solve(h, w, m, hw_list):
    h_count = [0] * (h + 1)
    w_count = [0] * (w + 1)
    for h_, w_ in hw_list:
        h_count[h_] += 1
        w_count[w_] += 1

    h_max = max(h_count)
    w_max = max(w_count)
    res = h_max + w_max

    h_max_count = 0
    w_max_count = 0
    for i in range(h + 1):
        if h_count[i] == h_max:
            h_max_count += 1
    for j in range(w + 1):
        if w_count[j] == w_max:
            w_max_count += 1

    max_count = h_max_count * w_max_count
    max_count_cross = 0
    for h_, w_ in hw_list:
        if h_count[h_] == h_max and w_count[w_] == w_max:
            max_count_cross += 1

    if max_count_cross == max_count:
        return res - 1
    else:
        return res


def main():
    h, w, m = map(int, input().split())
    hw_list = []
    for _ in range(m):
        hw_list.append(list(map(int, input().split())))
    res = solve(h, w, m, hw_list)
    print(res)


def test():
    assert solve(2, 3, 3, [[2, 2], [1, 1], [1, 3]]) == 3
    assert solve(3, 3, 4, [[3, 3], [3, 1], [1, 1], [1, 2]]) == 3
    assert solve(5, 5, 10, [[2, 5], [4, 3], [2, 3], [5, 5], [2, 2], [5, 4], [5, 3], [5, 1], [3, 5], [1, 4]]) == 6


if __name__ == "__main__":
    test()
    main()

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

H,W=[0]*h,[0]*w
for i,j in HW:
    H[i] +=1
    W[j] +=1

max_h,max_w=max(H),max(W)
ans=max_h+max_w
cnt=H.count(max_h)*W.count(max_w)
for i,j in HW:
    if H[i]==max_h and W[j]==max_w:
        cnt -=1
print(ans if cnt!=0 else ans-1)

Solution (Python):
def main():
    H, W, M = (int(i) for i in input().split())
    hc = [0]*(H+1)
    wc = [0]*(W+1)
    maps = set()
    for _ in range(M):
        h, w = (int(i) for i in input().split())
        hc[h] += 1
        wc[w] += 1
        maps.add((h, w))
    mah = max(hc)
    maw = max(wc)
    ans = mah+maw
    hmaps = []
    wmaps = []
    for i, h in enumerate(hc):
        if mah == h:
            hmaps.append(i)
    for i, w in enumerate(wc):
        if maw == w:
            wmaps.append(i)

    if M < len(hmaps) * len(wmaps):
        
        
        
        
        print(ans)
    else:
        for h in hmaps:
            for w in wmaps:
                if (h, w) not in maps:
                    print(ans)
                    return
        else:
            print(ans-1)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    H, W, M = (int(i) for i in input().split())
    hc = [0]*(H+1)
    wc = [0]*(W+1)
    maps = set()
    for _ in range(M):
        h, w = (int(i) for i in input().split())
        hc[h] += 1
        wc[w] += 1
        maps.add((h, w))
    mah = -1
    maw = -1
    hmaps = set()
    wmaps = set()
    for i, h in enumerate(hc):
        if mah < h:
            mah = h
            hmaps = {i}
        elif mah == h:
            hmaps.add(i)
    for i, w in enumerate(wc):
        if maw < w:
            maw = w
            wmaps = {i}
        elif maw == w:
            wmaps.add(i)
    ans = mah+maw
    aru = True

    for h in hmaps:
        for w in wmaps:
            if (h, w) not in maps:
                aru = False
                break
        if not aru:
            break
    if aru:
        print(ans - 1)
    else:
        print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    H, W, M = (int(i) for i in input().split())
    hc = [0]*(H+1)
    wc = [0]*(W+1)
    maps = set()
    for _ in range(M):
        h, w = (int(i) for i in input().split())
        hc[h] += 1
        wc[w] += 1
        maps.add((h, w))
    mah = -1
    maw = -1
    hmaps = set()
    wmaps = set()
    for i, h in enumerate(hc):
        if mah < h:
            mah = h
            hmaps = {i}
        elif mah == h:
            hmaps.add(i)
    for i, w in enumerate(wc):
        if maw < w:
            maw = w
            wmaps = {i}
        elif maw == w:
            wmaps.add(i)
    ans = mah+maw

    for h in hmaps:
        for w in wmaps:
            if (h, w) not in maps:
                print(ans)
                return
    else:
        print(ans-1)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    H, W, M = (int(i) for i in input().split())
    hc = [0]*(H+1)
    wc = [0]*(W+1)
    maps = set()
    for _ in range(M):
        h, w = (int(i) for i in input().split())
        hc[h] += 1
        wc[w] += 1
        maps.add((h, w))
    mah = max(hc)
    maw = max(wc)
    ans = mah+maw
    hmaps = set()
    wmaps = set()
    for i, h in enumerate(hc):
        if mah == h:
            hmaps.add(i)
    for i, w in enumerate(wc):
        if maw == w:
            wmaps.add(i)

    for h in hmaps:
        for w in wmaps:
            if (h, w) not in maps:
                print(ans)
                return
    else:
        print(ans-1)


if __name__ == '__main__':
    main()

Solution (Python):
import pprint
H, W, M = map(int, input().split())
bombs = set()
hCount = [0]*H
vCount = [0]*W

for i in range(M):
    h, w = map(int, input().split())
    h -= 1; w -= 1;
    hCount[h] += 1
    vCount[w] += 1
    bombs.add((h, w))

hMax = max(hCount)
vMax = max(vCount)
hMaxIndex = [i for i, x in enumerate(hCount) if x==hMax]
vMaxIndex = [i for i, x in enumerate(vCount) if x==vMax]

ans = -1
for x in vMaxIndex:
    for y in hMaxIndex:
        if (y, x) in bombs:
            continue
        else:
            print(hMax + vMax)
            exit()
print(hMax + vMax - 1)

Solution (Python):
def main():
    H, W, M = (int(i) for i in input().split())
    hc = [0]*(H+1)
    wc = [0]*(W+1)
    maps = set()
    for _ in range(M):
        h, w = (int(i) for i in input().split())
        hc[h] += 1
        wc[w] += 1
        maps.add((h, w))
    mah = max(hc)
    maw = max(wc)
    ans = mah+maw
    hmaps = []
    wmaps = []
    for i, h in enumerate(hc):
        if mah == h:
            hmaps.append(i)
    for i, w in enumerate(wc):
        if maw == w:
            wmaps.append(i)

    for h in hmaps:
        for w in wmaps:
            if (h, w) not in maps:
                print(ans)
                return
    else:
        print(ans-1)


if __name__ == '__main__':
    main()

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

HW = set()
sum_h = [0]*H; sum_w = [0]*W
max_sum_h = max_sum_w = 0
for _ in range(M):
    h, w = map(lambda x:int(x)-1, input().split())
    HW.add((h, w))
    sum_h[h] += 1
    sum_w[w] += 1
    max_sum_h = max(max_sum_h, sum_h[h])
    max_sum_w = max(max_sum_w, sum_w[w])

hs = []; ws = []
for i in range(H):
    if sum_h[i] == max_sum_h: hs.append(i)
for i in range(W):
    if sum_w[i] == max_sum_w: ws.append(i)

ans = max_sum_h + max_sum_w - 1
for h in hs:
    for w in ws:
        if (h, w) in HW: continue
        print(ans + 1)
        exit()
print(ans)

Solution (Python):
def main():
    H, W, M = (int(i) for i in input().split())
    hc = [0]*(H+1)
    wc = [0]*(W+1)
    maps = set()
    for _ in range(M):
        h, w = (int(i) for i in input().split())
        hc[h] += 1
        wc[w] += 1
        maps.add((h, w))
    mah = -1
    maw = -1
    hmaps = set()
    wmaps = set()
    for i, h in enumerate(hc):
        if mah < h:
            mah = h
            hmaps = {i}
        elif mah == h:
            hmaps.add(i)
    for i, w in enumerate(wc):
        if maw < w:
            maw = w
            wmaps = {i}
        elif maw == w:
            wmaps.add(i)
    ans = mah+maw
    aru = True
    if len(hmaps) * len(wmaps) <= 5*10**6:
        for h in list(hmaps):
            for w in list(wmaps):
                if (h, w) not in maps:
                    aru = False
                    break
    else:
        aru = False
    if aru:
        print(ans - 1)
    else:
        print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
H, W, M = map(int, input().split())
row = [0] * (H + 1)
col = [0] * (W + 1)

bom = []
for i in range(M):
    h, w = map(int, input().split())
    bom.append([h, w])
    row[h] += 1
    col[w] += 1
 
rmax = max(row)
cmax = max(col)
cnt = row.count(rmax) * col.count(cmax)

for h, w in bom:
    if rmax == row[h] and cmax == col[w]:
        cnt -= 1

print(rmax + cmax - (cnt == 0))

Solution (Python):
R, C, T = map(int, input().split())
RC = []
for _ in range(T):
    RC.append(list(map(int, input().split())))
TR = [0] * (R + 1)
TC = [0] * (C + 1)
mxr=0
mxc=0
for r, c in RC:
    TR[r] += 1
    TC[c] += 1
    if(TR[r]>mxr):
      mxr=TR[r]
    if(TC[c]>mxc):
      mxc=TC[c]
ct = TR.count(mxr) * TC.count(mxc) - len([0 for r, c in RC if TR[r] == mxr and TC[c] == mxc])
print(mxc + mxr - (ct == 0))

Solution (Python):
R, C, T = map(int, input().split())
RC = []
for _ in range(T):
    RC.append(list(map(int, input().split())))
TR = [0] * (R + 1)
TC = [0] * (C + 1)
for r, c in RC:
    TR[r] += 1
    TC[c] += 1
mxr = max(TR)
mxc = max(TC)
ct = TR.count(mxr) * TC.count(mxc) - len([0 for r, c in RC if TR[r] == mxr and TC[c] == mxc])
print(mxc + mxr - (ct == 0))

Solution (Python):
h,w,m=map(int,input().split())
bomb=[list(map(int,input().split())) for i in range(m)]

num_h = [0] * (h+1)
num_w = [0] * (w+1)
bomb_set = set()
for hi,wi in bomb:
  num_h[hi] += 1
  num_w[wi] += 1
  bomb_set.add((hi,wi))
  
max_h = max(num_h)
max_w = max(num_w)
max_hs = [k for k,v in enumerate(num_h) if v == max_h]
max_ws = [k for k,v in enumerate(num_w) if v == max_w]

result = max_h + max_w
for i in max_hs:
  for j in max_ws:
    if (i,j) not in bomb_set:
      print(result)
      exit(0)
print(result-1)

Solution (Python):
H, W, M = map(int, input().split())
HW = []
for _ in range(M):
    HW.append(list(map(int, input().split())))

A = [0] * (H + 1)
B = [0] * (W + 1)
for h, w in HW:
    A[h] += 1
    B[w] += 1

a = max(A)
b = max(B)

cnt = A.count(a) * B.count(b) - len([0 for h, w in HW if A[h] == a and B[w] == b])

print(a + b - (cnt == 0))

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

for i in range(M):
    ARR.append(list(map(int,input().split())))

def calculate(h, w, m, arr):
    points = set()



    rowSum = [0 for i in range(h)]
    columnSum = [0 for i in range(w)]

    for ar in arr:
        points.add((ar[0]-1, ar[1]-1))
        rowSum[ar[0]-1] += 1
        columnSum[ar[1]-1] += 1

    maxRowSum = max(rowSum)
    maxColumnSum = max(columnSum)


    a0 = []

    for i in range(len(rowSum)):
        if rowSum[i] == maxRowSum:
            a0.append(i)

    a1 = []
    for i in range(len(columnSum)):
        if columnSum[i] == maxColumnSum:
            a1.append(i)

    result = 0
    for hIndex in a0:
        for wIndex in a1:

            if (hIndex,wIndex) in points:
                result = max(result, maxRowSum + maxColumnSum - 1)
            else:
                result = max(result, maxRowSum + maxColumnSum)
                print(result)
                exit()

    print(result)


calculate(H, W, M, ARR)

Solution (Python):
import collections
h, w, m = map(int, input().split())
h = [None]*m
w = [None]*m
for i in range(m):
    u, v = map(int, input().split())
    h[i] = u
    w[i] = v
hc = collections.Counter(h)
wc = collections.Counter(w)
hm = hc.most_common()[0][1]
hs = set([])
for hi, ci in hc.most_common():
    if ci == hm:
        hs.add(hi)
    else:
        break
wm = wc.most_common()[0][1]
ws = set([])
for wi, ci in wc.most_common():
    if ci == wm:
        ws.add(wi)
    else:
        break
cnt = 0
for i in range(m):
    if h[i] in hs and w[i] in ws:
        cnt += 1
if cnt == len(hs)*len(ws):
    print(hm + wm -1)
else:
    print(hm + wm)

Solution (Python):
H, W, M = map(int, input().split())
h = [0 for i in range(H)]
w = [0 for i in range(W)]
DP = set()
for i in range(M):
    hh, ww = map(int, input().split())
    h[hh - 1] += 1
    w[ww - 1] += 1
    DP.add((hh - 1, ww - 1))

hhh = max(h)
www = max(w)
hhhh = [i for i, x in enumerate(h) if x == hhh]
wwww = [i for i, x in enumerate(w) if x == www]
check = 0

for i in hhhh:
    for j in wwww:
        if (i, j) not in DP:
            print(hhh + www)
            exit()
print(hhh + www - 1)

Solution (Python):
H, W, M = map(int, input().split())
h = [0 for i in range(H)]
w = [0 for i in range(W)]
DP = set()
for i in range(M):
    hh, ww = map(int, input().split())
    h[hh - 1] += 1
    w[ww - 1] += 1
    DP.add((hh - 1, ww - 1))

hhh = max(h)
www = max(w)
hhhh = [i for i, x in enumerate(h) if x == hhh]
wwww = [i for i, x in enumerate(w) if x == www]
check = 0

for i in hhhh:
    for j in wwww:
        if (i, j) not in DP:
            print(hhh + www)
            exit()
print(hhh + www - 1)

Solution (Python):
h,w,m=map(int,input().split())
d_h=[0]*(h+1)
d_w=[0]*(w+1)
d=[]
for i in range(m):
  nh,nw=map(int,input().split())
  d_h[nh]+=1
  d_w[nw]+=1
  d.append([nh,nw])
  



hantei=0
ans1=max(d_h)
memoh=set()
for i in range(1,h+1):
  hantei=d_h[i]
  if ans1==hantei:
    memoh.add(i)
    
hantei=0
ans2=max(d_w)
memow=set()
for j in range(1,w+1):
  hantei=d_w[j]
  if ans2 == hantei:
    memow.add(j)
ans = ans2+ans1-1

count = len(memoh)*len(memow)
for hw in d:
  if hw[0] in memoh and hw[1] in memow:
    count-=1
if count>0:
  print(ans+1)
else:
  print(ans)

Solution (Python):
def resolve():
    H, W, M = map(int, input().split())
    pos = []
    for _ in range(M):
        pos.append(list(map(int, input().split())))

    h_totals = [0] * (H+1)
    w_totals = [0] * (W+1)
    overlap = [set() for _ in range(H+1)]

    for ph, pw in pos:
        h_totals[ph] += 1
        w_totals[pw] += 1
        overlap[ph].add(pw)

    max_h = -1
    max_w = -1
    h_list = []
    w_list = []
    for i in range(1, H+1):
        if h_totals[i] > max_h:
            max_h = h_totals[i]
            h_list = [i]
        elif h_totals[i] == max_h:
            h_list.append(i)
    for j in range(1, W+1):
        if w_totals[j] > max_w:
            max_w = w_totals[j]
            w_list = [j]
        elif w_totals[j] == max_w:
            w_list.append(j)

    for i in h_list:
        for j in w_list:
            if j not in overlap[i]:
                print(max_h + max_w)
                return
    print(max_h + max_w - 1)

if __name__ == "__main__":
    resolve()

Solution (Python):
def e_bomber():
    import sys
    input = sys.stdin.readline

    H, W, M = [int(i) for i in input().split()]

    bomb_row = [0] * H
    bomb_col = [0] * W
    bomb_pos = set()
    for _ in range(M):
        r, c = [int(i) - 1 for i in input().split()]
        bomb_row[r] += 1
        bomb_col[c] += 1
        bomb_pos.add((r, c))

    bomb_row_max = max(bomb_row)
    bomb_col_max = max(bomb_col)
    row_index_list = [r for r, v in enumerate(bomb_row) if v == bomb_row_max]
    col_index_list = [c for c, v in enumerate(bomb_col) if v == bomb_col_max]

    
    
    
    for row in row_index_list:
        for col in col_index_list:
            if (row, col) not in bomb_pos:
                return bomb_row_max + bomb_col_max
    return bomb_row_max + bomb_col_max - 1

print(e_bomber())