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):
def A():
    n, x, t = map(int, input().split())
    print(t * ((n+x-1)//x))

def B():
    n = list(input())
    s = 0
    for e in n:
        s += int(e)
    print("Yes" if s % 9 == 0 else "No")

def C():
    int(input())
    a = list(map(int, input().split()))
    l = 0
    ans = 0
    for e in a:
        if e < l: ans += l - e
        l = max(l, e)
    print(ans)

def E():
    h, w, m = map(int, input().split())
    row = [0] * h
    col = [0] * w
    exist = set([])
    th = 1000000000
    for i in range(m):
        x, y = map(int, input().split())
        x -= 1
        y -= 1
        row[x] += 1
        col[y] += 1
        exist.add(x*th + y)

    max_row = max(row)
    max_col = max(col)
    candi_row = []
    candi_col = []
    for i in range(h):
        if row[i] == max_row: candi_row.append(i)
    for i in range(w):
        if col[i] == max_col: candi_col.append(i)

    ans = max_row + max_col
    for x in candi_row:
        for y in candi_col:
            if x*th + y in exist: continue
            print(ans)
            return
    print(ans - 1)

E()

Solution (Python):
import sys
read = sys.stdin.read

def main():
    h, w, m = map(int, input().split())
    b_row = [0] * h
    b_col = [0] * w
    b_coor = set()
    m = map(int, read().split())
    for a, b in zip(m, m):
        a -= 1
        b -= 1
        b_row[a] += 1
        b_col[b] += 1
        b_coor.add((a, b))

    row_max = max(b_row)
    col_max = max(b_col)
    rows = [i for i, v in enumerate(b_row) if v == row_max]
    cols = [i for i, v in enumerate(b_col) if v == col_max]

    for r in rows:
        for c in cols:
            if (r, c) not in b_coor:
                print(row_max + col_max)
                sys.exit()
    print(row_max + col_max - 1)

if __name__ == '__main__':
    main()

Solution (Python):

import sys
from collections import Counter

H, W, M = map(int, sys.stdin.readline().rstrip().split())
hcnt = [0] * H
wcnt = [0] * W
bomb = []

for i in range(M):
    bh,bw=map(int,sys.stdin.readline().rstrip().split())
    hcnt[bh - 1] += 1
    wcnt[bw - 1] += 1
    bomb.append((bh - 1,bw - 1))



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

crs = 0
for (i, j) in bomb:
    if hcnt[i] == hmax and wcnt[j] == wmax:
        crs += 1

if crs == Counter(hcnt)[hmax] * Counter(wcnt)[wmax]:
    print(hmax + wmax - 1)

else:
    print(hmax + wmax)

Solution (Python):
h,w,m=map(int,input().split())
row=[0]*h
col=[0]*w
s=[]
for _ in range(m):
	x,y=map(int,input().split())
	row[x-1]+=1
	col[y-1]+=1
	s.append([x-1,y-1])
a,b=max(row),max(col)
aa=row.count(a)
bb=col.count(b)
c=0
for x,y in s:
	if row[x]==a and col[y]==b:c+=1
if c<aa*bb:print(a+b)
else:print(a+b-1)

Solution (Python):
from collections import defaultdict

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

row_bom_cnt = defaultdict(int)
col_bom_cnt = defaultdict(int)
row_max = 0
col_max = 0

boms = [list(map(int, input().split())) for _ in range(M)]

for rm, cm in boms:
    row_bom_cnt[rm] += 1
    col_bom_cnt[cm] += 1

    row_max = max(row_max, row_bom_cnt[rm])
    col_max = max(col_max, col_bom_cnt[cm])


target_row = set()
for r, val in row_bom_cnt.items():
    if val == row_max:
        target_row.add(r)

target_col = set()
for c, val in col_bom_cnt.items():
    if val == col_max:
        target_col.add(c)


cnt = 0
for rm, cm in boms:
    if rm in target_row and cm in target_col:
        cnt += 1

if len(target_row) * len(target_col) == cnt:
    print(row_max + col_max - 1)
else:
    print(row_max + col_max)

Solution (Python):
import itertools
H,W,N=[int(s) for s in input().split()]
hls=[0 for _ in range(H+1)]
wls=[0 for _ in range(W+1)]
bombs=[set() for _ in range(H+1)]
for _ in range(N):
  h,w=[int(s) for s in input().split()]
  hls[h]+=1
  wls[w]+=1
  bombs[h].add(w)
hmax=max(hls)
wmax=max(wls)
hmaxls=[i for i in range(H+1) if hls[i]==hmax]
wmaxls=[i for i in range(W+1) if wls[i]==wmax]
for h,w in itertools.product(hmaxls,wmaxls):
  if not(w in bombs[h]):
    print(hmax+wmax)
    exit()
else:
  print(hmax+wmax-1)

Solution (Python):
import sys
h,w,m = map(int,input().split())
h_lst = [[0,i] for i in range(h)]
w_lst = [[0,i] for i in range(w)]
memo = []

for i in range(m):
  x,y = map(int,input().split())
  h_lst[x-1][0] += 1
  w_lst[y-1][0] += 1
  memo.append((x-1,y-1))

h_lst.sort(reverse = True)
w_lst.sort(reverse = True)
Max_h = h_lst[0][0]
Max_w = w_lst[0][0]
h_ans = [h_lst[0][1]]
w_ans = [w_lst[0][1]]
if h != 1:
  s = 1
  while s < h and h_lst[s][0] == Max_h:
    h_ans.append(h_lst[s][1])
    s+= 1  
if w!= 1:  
  t=1  
  while t < w and w_lst[t][0] == Max_w:
    w_ans.append(w_lst[t][1])
    t += 1
  

memo = set(memo)


for j in h_ans:
  for k in w_ans:
    if (j,k) not in memo:
      print(Max_h+Max_w)
      sys.exit()
print((Max_h+Max_w)-1)

Solution (Python):
import sys
from collections import Counter
 
 
def main():
    H, W, M = map(int, sys.stdin.readline().rstrip().split())
    Mh = [0] * H
    Mw = [0] * W
    B = []
    for i in range(M):
        h, w = map(int, sys.stdin.readline().rstrip().split())
        Mh[h - 1] += 1
        Mw[w - 1] += 1
        B.append((h - 1, w - 1))
 
    nh = max(Mh)
    nw = max(Mw)
 
    
    lh = Counter(Mh)[nh]
    lw = Counter(Mw)[nw]
 
    out = 0
    for (h, w) in B:
        if Mh[h] == nh and Mw[w] == nw:
            out += 1
 
    if out == lh * lw:
        print(nh + nw - 1)
    else:
        print(nh + nw)
 
 
main()

Solution (Python):

import sys, math, heapq
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial, gcd
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline()[:-1]
pl = lambda x: print(*x, sep="\n")

H, W, M = map(int, input().split())
hw = [list(map(int, input().split())) for _ in range(M)]

hb = defaultdict(int)
wb = defaultdict(int)
bomb = defaultdict(int)

for hwi in hw:
    h, w = hwi
    hb[h] += 1
    wb[w] += 1
    bomb[(h, w)] += 1



mhb = max(hb.values())
mwb = max(wb.values())

maxh = [key for key, val in hb.items() if val == mhb]
maxw = [key for key, val in wb.items() if val == mwb]



for h in maxh:
    for w in maxw:
        if bomb[(h, w)] == 0:
            print(mhb + mwb)
            exit()
print(mhb + mwb - 1)

Solution (Python):
from collections import Counter
H, W, M = list(map(int, input().split()))
h = [0]*M
w = [0]*M
for i in range(M):
    h[i], w[i] = list(map(int, input().split()))
hc = Counter(h)
wc = Counter(w)
hmax = max(hc.values())
wmax = max(wc.values())
lh = {k[0] for k in hc.items() if k[1] == hmax}
lw = {k[0] for k in wc.items() if k[1] == wmax}

ct = 0
for i in range(M):
    if h[i] in lh and w[i] in lw:
        ct += 1
if ct >= len(lh)*len(lw):
    print(hmax+wmax-1)
else:
    print(hmax+wmax)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")

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

rows = [0] * (H + 1)
cols = [0] * (W + 1)

from collections import defaultdict
ng_cols = defaultdict(set)

for _ in range(M):
    h, w = map(int, input().split())
    rows[h] += 1
    cols[w] += 1
    ng_cols[h].add(w)

max_of_rows = max(rows)
max_of_cols = max(cols)
max_rows_index = set()
max_cols_index = set()
for i in range(H + 1):
    if rows[i] == max_of_rows:
        max_rows_index.add(i)
for i in range(W + 1):
    if cols[i] == max_of_cols:
        max_cols_index.add(i)
flag = 0
for i in max_rows_index:
    cnt = 0
    for j in ng_cols[i]:
        if j in max_cols_index:
            cnt += 1
    if cnt < len(max_cols_index):
        flag += 1
        break
        
print(max_of_rows + max_of_cols - 1 + flag)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")

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

rows = [0] * (H + 1)
cols = [0] * (W + 1)

from collections import defaultdict
ng_cols = defaultdict(set)

for _ in range(M):
    h, w = map(int, input().split())
    rows[h] += 1
    cols[w] += 1
    ng_cols[h].add(w)

max_of_rows = max(rows)
max_of_cols = max(cols)
max_rows_index = set()
max_cols_index = set()
for i in range(H + 1):
    if rows[i] == max_of_rows:
        max_rows_index.add(i)
for i in range(W + 1):
    if cols[i] == max_of_cols:
        max_cols_index.add(i)
flag = 0
for i in max_rows_index:
    for j in max_cols_index:
        if j not in ng_cols[i]:
            flag = 1
            break
print(max_of_rows + max_of_cols - 1 + flag)

Solution (Python):
h, w, m = map(int, input().split())
a_list = []
b_list = []
h_list = [0 for _ in range(h)]
w_list = [0 for _ in range(w)]
for i in range(m):
    a, b = map(int, input().split())
    a_list.append([a,b])
    h_list[a - 1] += 1
    w_list[b - 1] += 1
h_max = max(h_list)
w_max = max(w_list)
w_flag = [0 for _ in range(w)]
for i in range(w):
    if w_list[i] == w_max:
        w_flag[i] = 1
h_flag = [0 for _ in range(h)]
for i in range(h):
    if h_list[i] == h_max:
        h_flag[i] = 1
flag = 0   
for i in range(m):
    if h_flag[a_list[i][0] - 1] == 1 and w_flag[a_list[i][1] - 1] == 1:
        flag += 1
s = sum(h_flag) * sum(w_flag)
print(h_max + w_max - 1 if flag == s else h_max + w_max)

Solution (Python):
h,w,m = map(int,input().split())
x = {}
y = {}
dic = {}
for _ in range(m):
    a,b = map(int,input().split())
    dic[(a,b)] = 1
    x[a] = x.get(a,0) + 1
    y[b] = y.get(b,0) + 1
p = max(x.values())
q = max(y.values())
cnt = 1
a = [i for i in x if x[i] == p]
b = [j for j in y if y[j] == q]
for i in a:
    for j in b:
        if dic.get((i,j),-1) == -1:
            cnt = 0
            break
    if not cnt:
        break
print(p+q-cnt)

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

hlist = [0]*(H+1)
wlist = [0]*(W+1)
for l in L:
    hlist[l[0]] += 1
    wlist[l[1]] += 1

hmax = max(hlist)
wmax = max(wlist)
h = hlist.count(hmax)
w = wlist.count(wmax)
ans = hmax+wmax-1
cnt = 0
for l in L:
    if hlist[l[0]] == hmax and wlist[l[1]] == wmax:
        cnt += 1

if cnt < h*w:
    ans += 1
print(ans)

Solution (Python):
from sys import stdin
import collections

input = stdin.readline

Y, X, M = map(int, input().split())
bomb = set([tuple(map(int,inp.split())) for inp in stdin.read().splitlines()])
cx = collections.defaultdict(int)
cy = collections.defaultdict(int)
for y,x in bomb:
    cx[x] += 1
    cy[y] += 1
mx = max(cx.values())
my = max(cy.values())
candidateX = [x for (x,v) in cx.items() if v == mx]
candidateY = [y for (y,v) in cy.items() if v == my]
res = mx + my -1
for x in candidateX:
    for y in candidateY:
        if (y,x) not in bomb:
            res += 1
            break
    else:
        continue
    break
print(res)

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

hs = [0] * H
ws = [0] * W
s = set()
for i in range(M):
  h,w = map(int, input().split())
  hs[h-1] += 1
  ws[w-1] += 1
  s.add((h-1,w-1))
  
mh = 0
mw = 0
for i in range(H):
  mh = max(mh, hs[i])
for j in range(W):
  mw = max(mw, ws[j])
  
i_s = list()
j_s = list()
for i in range(H):
  if mh == hs[i]:
    i_s.append(i)
for j in range(W):
  if mw == ws[j]:
    j_s.append(j)

ans = mh + mw
for i in i_s:
  for j in j_s:
    if (i,j) in s:
      continue
    print(ans)
    exit()
ans -= 1
print(ans)

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

tate = [0]*(h+1)
yoko = [0]*(w+1)
bombpoint = set()

for x,y in hw:
    tate[x] += 1
    yoko[y] += 1
    bombpoint.add((x,y))

ans_x = []
ans_y = []
x_max = max(tate)
y_max = max(yoko)
for i in range(len(tate)):
    if tate[i] == x_max:
        ans_x.append(i)
for i in range(len(yoko)):
    if yoko[i] == y_max:
        ans_y.append(i)

ans = x_max + y_max
for x in ans_x:
    for y in ans_y:
        if (x,y) not in bombpoint:
            print(ans)
            exit()
print(ans - 1)

Solution (Python):
def main():

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

    hmax = max(h_dic)
    wmax = max(w_dic)

    h_maxs = [idx for idx, v in enumerate(h_dic) if v==hmax]
    w_maxs = [idx for idx, v in enumerate(w_dic) if v==wmax]

    
    for h in h_maxs:
        for w in w_maxs:
            if (h, w) not in boms:
                print(hmax + wmax)
                return
    print(hmax + wmax - 1)
    return

if __name__ == "__main__":
    main()

Solution (Python):


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


h_sum = [0] * H
w_sum = [0] * W
point_list = set()

for _ in range(M):
    h, w = map(int, input().split())
    
    point_list.add((h-1, w-1))
    h_sum[h-1] += 1
    w_sum[w-1] += 1

h_max = max(h_sum)
w_max = max(w_sum)

h_argmax = set()
w_argmax = set()

for i, h in enumerate(h_sum):
    if h == h_max:
        h_argmax.add(i) 

for i, w in enumerate(w_sum):
    if w == w_max:
        w_argmax.add(i)

ans = h_max + w_max

for h in h_argmax:
    for w in w_argmax:
        if (h, w) not in point_list:
        
            print(ans)
            exit()
print(ans - 1)

Solution (Python):
h,w,m=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
H=[0]*h
W=[0]*w
for i in range(m):
  H[l[i][0]-1]+=1
  W[l[i][1]-1]+=1
hmax=max(H)
wmax=max(W)
cthmax=0
ctwmax=0
for i in range(h):
  if H[i]==hmax:
    cthmax+=1
for i in range(w):
  if W[i]==wmax:
    ctwmax+=1
ct=0
for i in range(m):
  if H[l[i][0]-1]==hmax and W[l[i][1]-1]==wmax:
    ct+=1
if cthmax*ctwmax>ct:
  print(hmax+wmax)
else:
  print(hmax+wmax-1)

Solution (Python):
import numpy as np
import sys
read = sys.stdin.read

if sys.argv[-1] == 'ONLINE_JUDGE':
	from numba.pycc import CC
	cc = CC('unko')

	@cc.export('solve', '(i4[:],)')
	def solve(stdin):
		h, w, m = stdin[:3]

		row = np.zeros(h, np.int32)
		col = np.zeros(w, np.int32)
		for i in range(3, m * 2 + 3, 2):
			y, x = stdin[i: i + 2] - 1
			row[y] += 1
			col[x] += 1

		max_cnt_y = max(row)
		max_cnt_x = max(col)
		max_pair = np.sum(row == max_cnt_y) * np.sum(col == max_cnt_x)
		for i in range(3, m * 2 + 3, 2):
			y, x = stdin[i: i + 2] - 1
			if row[y] == max_cnt_y and col[x] == max_cnt_x:
				max_pair -= 1

		return max_cnt_y + max_cnt_x - (max_pair == 0)
	
	cc.compile()
	exit()

from unko import solve
print(solve(np.fromstring(read(), dtype=np.int32, sep=' ')))

Solution (Python):
import numpy as np
import sys
from numba import njit, i4
read = sys.stdin.read

@njit((i4[:],), cache=True)
def solve(stdin):
	h, w, m = stdin[:3]

	row = np.zeros(h, np.int32)
	col = np.zeros(w, np.int32)
	for i in range(3, m * 2 + 3, 2):
		y, x = stdin[i: i + 2] - 1
		row[y] += 1
		col[x] += 1

	max_cnt_y = max(row)
	max_cnt_x = max(col)
	max_pair = np.sum(row == max_cnt_y) * np.sum(col == max_cnt_x)
	for i in range(3, m * 2 + 3, 2):
		y, x = stdin[i: i + 2] - 1
		if row[y] == max_cnt_y and col[x] == max_cnt_x:
			max_pair -= 1

	return max_cnt_y + max_cnt_x - (max_pair == 0)

print(solve(np.fromstring(read(), dtype=np.int32, sep=' ')))

Solution (Python):
import numpy as np
import sys
read = sys.stdin.read

def solve(stdin):
	h, w, m = stdin[:3]

	row = np.zeros(h, np.int)
	col = np.zeros(w, np.int)
	for i in range(3, m * 2 + 3, 2):
		y, x = stdin[i: i + 2] - 1
		row[y] += 1
		col[x] += 1

	max_cnt_y = max(row)
	max_cnt_x = max(col)
	max_pair = np.sum(row == max_cnt_y) * np.sum(col == max_cnt_x)
	for i in range(3, m * 2 + 3, 2):
		y, x = stdin[i: i + 2] - 1
		if row[y] == max_cnt_y and col[x] == max_cnt_x:
			max_pair -= 1

	return max_cnt_y + max_cnt_x - (max_pair == 0)

print(solve(np.fromstring(read(), dtype=np.int, sep=' ')))

Solution (Python):
import numpy as np
h, w, m = map(int, input().split())
xy = [tuple(map(lambda x: int(x) - 1, input().split())) for _ in range(m)]
row = np.zeros(h, np.int)
col = np.zeros(w, np.int)
for y, x in xy:
	row[y] += 1
	col[x] += 1

max_cnt_y = max(row)
max_cnt_x = max(col)
max_pair = np.sum(row == max_cnt_y) * np.sum(col == max_cnt_x)
for y, x in xy:
	if row[y] == max_cnt_y and col[x] == max_cnt_x:
		max_pair -= 1

print(max_cnt_y + max_cnt_x - (max_pair == 0))

Solution (Python):



from math import gcd
from itertools import combinations,permutations,accumulate, product 

from collections import deque,defaultdict,Counter
import decimal
import re
import math
import bisect
import heapq














import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7


INF = float('inf')
from sys import stdin
readline = stdin.readline
def readInts():
  return list(map(int,readline().split()))
def readTuples():
    return tuple(map(int,readline().split()))
def I():
    return int(readline())
H,W,M = readInts()

dic1 = Counter()
dic2 = Counter()
s = set()
for i in range(M):
    h,w = map(lambda x:int(x)-1, input().split())
    dic1[h] += 1
    dic2[w] += 1
    s.add((h,w))

ans = 0



for h,w in s:
    ans = max(ans, dic1[h] + dic2[w] - 1)

dic1 = dic1.most_common()
dic2 = dic2.most_common()
max1 = dic1[0][1]
max2 = dic2[0][1]

for k1,v1 in dic1:
    if v1 < max1:
        break 
    for k2,v2 in dic2:
        if v2 < max2:
            break 
        if (k1,k2) in s: 
            continue
        
        ans = max(ans, v1 + v2)
        break
print(ans)

Solution (Python):
from collections import Counter


H, W, M, *hw = map(int, open(0).read().split())
h_cnt = Counter(hw[::2])
w_cnt = Counter(hw[1::2])
h_max = h_cnt.most_common(1)[0][1]
w_max = w_cnt.most_common(1)[0][1]

not_on_bomb = sum(1 for k, v in h_cnt.items() if v == h_max)
not_on_bomb *= sum(1 for k, v in w_cnt.items() if v == w_max)
for h, w in zip(*[iter(hw)] * 2):
    if h_cnt[h] == h_max and w_cnt[w] == w_max:
        not_on_bomb -= 1

ans = h_max + w_max
print(ans) if not_on_bomb else print(ans - 1)

Solution (Python):
def main():
    H, W, M = map(int, input().split())
    cnth = [[0, i] for i in range(H)]
    cntw = [[0, i] for i in range(W)]
    L = set()
    for _ in range(M):
        h, w = map(lambda x: int(x) - 1, input().split())
        cnth[h][0] += 1
        cntw[w][0] += 1
        L.add((h, w))
    cnth.sort(reverse=True)
    cntw.sort(reverse=True)
    mh = set()
    ph = cnth[0]
    for h in cnth:
        if ph[0] == h[0]:
            mh.add(h[1])
        else:
            break
        ph = h
    mw = set()
    pw = cntw[0]
    for w in cntw:
        if pw[0] == w[0]:
            mw.add(w[1])
        else:
            break
        pw = w
    maxans = cnth[0][0] + cntw[0][0]
    for h in mh:
        for w in mw:
            if not ((h, w) in L):
                print(maxans)
                break
        else:
            continue
        break
    else:
        print(maxans - 1)
if __name__ == "__main__":
    main()

Solution (Python):
H, W, M = map(int, input().split())
boms = []
h, w = [0] * (H + 1), [0] * (W + 1)
for _ in range(M):
    h_, w_ = map(int, input().split())
    boms.append([h_, w_])
    h[h_] += 1
    w[w_] += 1

hmax = max(h)
wmax = max(w)

cnt = h.count(hmax) * w.count(wmax)
for hh, ww in boms:
    if hmax == h[hh] and wmax == w[ww]:
        cnt -= 1

print(hmax + wmax - (cnt == 0))

Solution (Python):
from collections import defaultdict
h, w, m = map(int, input().split())

row = [[0, 0] for _ in range(h + 1)]
col = [[0, 0] for _ in range(w + 1)]
ex = defaultdict(bool)
for i in range(m):
    r, c = map(int, input().split())
    row[r][0] += 1
    col[c][0] += 1
    row[r][1] = r
    col[c][1] = c
    ex[(r, c)] = True
row = sorted(row)[::-1]
col = sorted(col)[::-1]
ans = 0
for i in range(0, h + 1):
    for j in range(0, w + 1):
        ans = max(ans, row[i][0] + col[j][0] - ex[(row[i][1], col[j][1])])
        if not ex[(row[i][1], col[j][1])]:
            
            break

print(ans)

Solution (Python):
from collections import defaultdict,Counter

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

w_list = []
h_list = []

se = set()

for _ in range(m):
    nh,nw = map(int,input().split())

    se.add((nh,nw))
    w_list.append(nw)
    h_list.append(nh)

w_count = Counter(w_list)
h_count = Counter(h_list)

max_w = 0
max_h = 0

ch = []
cw = []

for i in w_count.values():
    max_w = max(max_w,i)
    cw.append(i)

for i in h_count.values():
    max_h = max(max_h,i)
    ch.append(i)

ma = ch.count(max_h)*cw.count(max_w)

result = max_h+max_w
point = 0

for i,j in se:
    if w_count[j] == max_w and h_count[i] == max_h:
        point += 1

if point == ma:
    print(result-1)
else:
    print(result)

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
from bisect import bisect_left,bisect
def main():
    h, w, m = map(int,input().split()) 
    h_lis = [0] * (h+1)
    w_lis = [0] * (w+1)
    bombs = set([tuple(map(int,input().split())) for i in range(m)])
    for hh, ww in bombs:
        h_lis[hh] += 1
        w_lis[ww] += 1
    max_h = max(h_lis)
    max_w = max(w_lis)
    h_max_index = [i for i, h in enumerate(h_lis) if h == max_h]
    w_max_index = [i for i, w in enumerate(w_lis) if w == max_w]
    for h_i in h_max_index:
        for w_i in w_max_index:
            if not (h_i, w_i) in bombs:
                print(h_lis[h_i] + w_lis[w_i])
                sys.exit()
    else:
        print(h_lis[h_i] + w_lis[w_i] - 1)

if __name__=='__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
from bisect import bisect_left,bisect
def main():
    h, w, m = map(int,input().split()) 
    h_lis = [0] * (h+1)
    w_lis = [0] * (w+1)
    bombs = set([tuple(map(int,input().split())) for i in range(m)])
    for hh, ww in bombs:
        h_lis[hh] += 1
        w_lis[ww] += 1
    max_h = max(h_lis)
    max_w = max(w_lis)
    h_max_index = [i for i, h in enumerate(h_lis) if h == max_h]
    w_max_index = [i for i, w in enumerate(w_lis) if w == max_w]
    for h_i in h_max_index:
        for w_i in w_max_index:
            if not (h_i, w_i) in bombs:
                print(h_lis[h_i] + w_lis[w_i])
                sys.exit()
    else:
        print(h_lis[h_i] + w_lis[w_i] - 1)

if __name__=='__main__':
    main()

Solution (Python):
import sys

from collections import defaultdict


def input():
    return sys.stdin.readline().strip()


def main():
    h, w, m = [int(i) for i in input().split()]
    d_h = defaultdict(int)
    d_w = defaultdict(int)
    bom = set()
    for _ in range(m):
        h, w = [int(i) for i in input().split()]
        bom.add((h, w))
        d_h[h] += 1
        d_w[w] += 1
    H = sorted(d_h.items(), key=lambda x: -x[1])
    W = sorted(d_w.items(), key=lambda x: -x[1])
    ans = 0
    for k_h, v_h in H:
        for k_w, v_w in W:
            tmp = v_h + v_w
            if (k_h, k_w) in bom:
                tmp -= 1
                ans = max(tmp, ans)
            else:
                ans = max(tmp, ans)
                break
        else:
            continue
        break

    for k_w, v_w in W:
        for k_h, v_h in H:
            tmp = v_h + v_w
            if (k_h, k_w) in bom:
                tmp -= 1
                ans = max(tmp, ans)
            else:
                ans = max(tmp, ans)
                break
        else:
            continue
        break
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
H,W,M = map(int,input().split())
X = {}
CH = {}
CW = {}
for _ in range(M):
    h,w = map(int,input().split())
    X[(h,w)] = 1
    if h not in CH:
        CH[h] = 0
    CH[h] += 1
    if w not in CW:
        CW[w] = 0
    CW[w] += 1
hmax = 0
for h in CH:
    hmax = max(hmax,CH[h])
A = []
for h in CH:
    if CH[h]==hmax:
        A.append(h)
wmax = 0
for w in CW:
    wmax = max(wmax,CW[w])
B = []
for w in CW:
    if CW[w]==wmax:
        B.append(w)
flag = 0
for h in A:
    for w in B:
        if (h,w) not in X:
            flag = 1
            break
    if flag==1:break
if flag==1:
    print(hmax+wmax)
else:
    print(hmax+wmax-1)

Solution (Python):
import sys
input = sys.stdin.readline
H, W, M = map(int, input().split())
s = set()
ys = [0] * (H + 1)
xs = [0] * (W + 1)
for _ in range(M):
  y, x = map(int, input().split())
  s.add((y, x))
  ys[y] += 1
  xs[x] += 1

res = 0
mxy = []
mx = max(ys)
for i in range(H + 1):
  if ys[i] == mx: mxy.append(i)
res += mx

mxx = []
mx = max(xs)
for i in range(W + 1):
  if xs[i] == mx: mxx.append(i)
res += mx - 1

for x in mxx:
  for y in mxy:
    if (y, x) in s: continue
    res += 1
    break
  else: continue
  break
print(res)

Solution (Python):
import sys
import io, os

input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

h, w, m = map(int, input().split())
R = [0]*h
C = [0]*w
YX = []
for i in range(m):
    y, x = map(int, input().split())
    y, x = y-1, x-1
    R[y] +=1
    C[x] +=1
    YX.append((y, x))

max_r = max(R)
max_c = max(C)
cr = 0
cc = 0
for i in range(h):
    if R[i] == max_r:
        cr += 1
for i in range(w):
    if C[i] == max_c:
        cc +=1
c = 0
for y, x in YX:
    if R[y] == max_r and C[x] == max_c:
        c += 1
if c < cr*cc:
    print(max_r+max_c)
else:
    print(max_r+max_c-1)

Solution (Python):
H, W, M = map(int, input().split())
bombs = set([tuple(map(int, input().split())) for _ in range(M)])

cntH = [0] * (H + 1)
cntW = [0] * (W + 1)
for h, w in bombs:
    cntH[h] += 1
    cntW[w] += 1

mxH = max(cntH)
mxW = max(cntW)

ans = mxH + mxW - 1

mxH = [h for h in range(1, H + 1) if cntH[h] == mxH]
mxW = [w for w in range(1, W + 1) if cntW[w] == mxW]

for h in mxH:
    for w in mxW:
        if not (h, w) in bombs:
            ans += 1
            print(ans)
            exit()
print(ans)

Solution (Python):
def main():
    H, W, M = [int(s) for s in input().split()]
    cols = [0] * W
    rows = [0] * H
    bombs = set()
    for _ in range(M):
        x, y = [int(s)-1 for s in input().split()]
        bombs.add((x, y))
        cols[y] += 1
        rows[x] += 1

    sc = sorted([(c, i) for i, c in enumerate(cols)], reverse=True)
    sr = sorted([(c, i) for i, c in enumerate(rows)], reverse=True)
    best = 0
    for v1, c in sc:
        for v2, r in sr:
            if v1 + v2 <= best:
                break
            score = v1 + v2
            if (r, c) in bombs:
                score -= 1
            best = max(best, score)
    print(best)

main()

Solution (Python):
H, W, M = map(int, input().split())
h = [0 for _ in range(H+1)]
w = [0 for _ in range(W+1)]
hmax = 0
wmax = 0
Q = []
for _ in range(M):
  a, b = map(int, input().split())
  Q.append((a, b))
  h[a] += 1
  hmax = max(hmax, h[a])
  w[b] += 1
  wmax = max(wmax, w[b])
h_ok = False
w_ok = False
hm = [False for _ in range(H+1)]
wm = [False for _ in range(W+1)]
h_cnt = 0
w_cnt = 0
hw_cnt = 0
for a, b in Q:
  if not hm[a] and h[a] == hmax:
    hm[a] = True
    h_cnt += 1
  if not wm[b] and w[b] == wmax:
    wm[b] = True
    w_cnt += 1
  if h[a] == hmax and w[b] == wmax:
    hw_cnt += 1
if h_cnt*w_cnt - hw_cnt > 0:
  print(hmax+wmax)
else:
  print(hmax+wmax-1)

Solution (Python):
def main(): 
    h,w,m = map(int,input().split())
    X = [0]*w
    Y = [0]*h
    g = set([])
    for _ in range(m):
        a,b = map(int,input().split())
        a-=1
        b-=1
        X[b]+=1
        Y[a]+=1
        g.add((a,b))
    
    xmax = max(X)
    ymax = max(Y)

    xlist = []
    for i,x in enumerate(X):
        if x==xmax:
            xlist.append(i)
    ylist = []
    for i,y in enumerate(Y):
        if y==ymax:
            ylist.append(i)
    
    ans = xmax+ymax
    flag = True
    for x in xlist:
        for y in ylist:
            if (y,x) not in g:
                flag = False
                break
        if not flag:
            break
    
    if flag:
        ans-=1
    print(ans)
main()

Solution (Python):
h,w,m=map(int,input().split())
h=[]
w=[]
for i in range(m):
  h_,w_=map(int,input().split())
  h.append(h_)
  w.append(w_)
from collections import Counter
dic_h=Counter(h)
dic_w=Counter(w)
max_h=max(dic_h.values())
max_w=max(dic_w.values())

h_set=set()
w_set=set()
for i in dic_h:
  if dic_h[i]==max_h:
    h_set.add(i)
for j in dic_w:
  if dic_w[j]==max_w:
    w_set.add(j)

cnt=0
for i in range(m):
  if h[i] in h_set and w[i] in w_set:
    cnt+=1
if len(h_set)*len(w_set)>cnt:
  print(max_h+max_w)
else:
  print(max_h+max_w-1)

Solution (Python):
h,w,m=map(int,input().split())
h=[]
w=[]
for i in range(m):
  h_,w_=map(int,input().split())
  h.append(h_)
  w.append(w_)
from collections import Counter
dic_h=Counter(h)
dic_w=Counter(w)
max_h=max(dic_h.values())
max_w=max(dic_w.values())

h_set=set()
w_set=set()
for i in dic_h:
  if dic_h[i]==max_h:
    h_set.add(i)
for j in dic_w:
  if dic_w[j]==max_w:
    w_set.add(j)

cnt=0
for i in range(m):
  if h[i] in h_set and w[i] in w_set:
    cnt+=1
if len(h_set)*len(w_set)>cnt:
  print(max_h+max_w)
else:
  print(max_h+max_w-1)

Solution (Python):
H, W, M = map(int, input().split())
hws = [list(map(lambda x :int(x)-1, input().split())) for _ in range(M)]

booms = [set() for _ in range(H)]

yoko = [0 for _ in range(W)]
tate = [0 for _ in range(H)]

for hw in hws:
    tate[hw[0]] += 1
    yoko[hw[1]] += 1
    booms[hw[0]].add(hw[1])
    
max_tate = max(tate)
max_yoko = max(yoko)


tate_candidates = []
yoko_candidates = []

for i in range(H):
    if tate[i] == max_tate:
        tate_candidates.append(i)
        
for i in range(W):
    if yoko[i] == max_yoko:
        yoko_candidates.append(i)

exist = 0
for tate_candidate in tate_candidates:
    for yoko_candidate in yoko_candidates:
        if yoko_candidate not in booms[tate_candidate]:
            exist = 1
            break
    else:
        continue
    break
print(max_tate + max_yoko + (exist-1))

Solution (Python):
H,W,M = map(int, input().split())
hs = [0] * H
ws = [0] * W
s = set()
for i in range(M):
  h,w = map(int, input().split())
  h -= 1
  w -= 1
  s.add((h,w))
  hs[h] += 1
  ws[w] += 1
  
mh = 0
mw = 0
for i in range(H):
  mh = max(mh, hs[i])
for j in range(W):
  mw = max(mw, ws[j])
  
i_s = list()
j_s = list()
for i in range(H):
  if mh == hs[i]:
    i_s.append(i)
for j in range(W):
  if mw == ws[j]:
    j_s.append(j)
ans = mh + mw
for i in i_s:
  for j in j_s:
    if (i,j) in s:
      continue
    print(ans)
    exit()
ans -= 1
print(ans)

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

for i in range(m):
    hi, wi = list(map(int, input().split()))
    ms.append((hi - 1, wi - 1))
    hs[hi - 1] += 1
    ws[wi - 1] += 1

maxh = max(hs)
maxw = max(ws)
cnt_h = sum([hs[i] == maxh for i in range(h)])
cnt_w = sum([ws[i] == maxw for i in range(w)])
cnt = cnt_h * cnt_w

for x, y in ms:
    if hs[x] == maxh and ws[y] == maxw:
        cnt -= 1

if cnt <= 0:
    print(maxh + maxw - 1)
else:
    print(maxh + maxw)