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 sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

import bisect
from operator import itemgetter

h,w,m = readints()

r = [0]*h
c = [0]*w

p = [readints() for i in range(m)]
p.sort(key=itemgetter(1))

d = [[] for i in range(h)]
for i in range(m):
    a,b = (x-1 for x in p[i])
    r[a] += 1
    c[b] += 1
    d[a].append(b)

mr = max(r)
mc = max(c)
R = [i for i,x in enumerate(r) if x==mr]
C = [i for i,x in enumerate(c) if x==mc]

flag = 1
if len(R)*len(C)>m:
    flag = 0
else:
    for x in R:
        for y in C:
            z = bisect.bisect_left(d[x],y)
            if z>=len(d[x]):
                flag = 0
                break
            else:
                if y != d[x][z]:
                    flag = 0
                    break

print(max(r)+max(c)-flag)

Solution (Python):
import bisect

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

rows, columns = [0]*H, [0]*W
for h, w in hw:
    rows[h-1] += 1
    columns[w-1] += 1
max_r, max_c = max(rows), max(columns)

argmax_r, argmax_c = [], []
for i in range(H):
    if max_r == rows[i]:
        argmax_r.append(i)

for i in range(W):
    if max_c == columns[i]:
        argmax_c.append(i)

for r in argmax_r:
    for c in argmax_c:
        idx = bisect.bisect_left(hw, [r+1, c+1])
        if idx == M or hw[idx] != [r+1,c+1]:
            print(max_r + max_c)
            exit()

print(max_r + max_c - 1)

Solution (Python):
from collections import *
h, w, m = map(int, input().split())
mh = [0 for _ in range(h)]
mw = [0 for _ in range(w)]

z = []
for _ in range(m):
    x, y = map(int, input().split())
    z.append((x-1, y-1))
    mh[x-1] += 1
    mw[y-1] += 1

hc = Counter(mh)
wc= Counter(mw)
hm = max(mh)
wm= max(mw)

ans = hm + wm
tmp = 0
for x, y in z:
    if mh[x] + mw[y] == ans:
        tmp += 1

if tmp == hc[hm] * wc[wm]:
    print(ans - 1)
else:
    print(ans)

Solution (Python):

import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0
 
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
 
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
 
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
 
 
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
 


from math import gcd, ceil

def pre(s):
    n = len(s)
    pi = [0] * n
    for i in range(1, n):
        j = pi[j - 1]
        while j and s[i] != s[j]:
            j = pi[j - 1]
        if s[i] == s[j]:
            j += 1
        pi[i] =  j
    return pi

def prod(a):
    ans = 1
    for each in a:
        ans = (ans * each)
    return ans

def lcm(a, b): return a * b // gcd(a, b)

def binary(x, length=16):
    y = bin(x)[2:]
    return y if len(y) >= length else '0' * (length - len(y)) + y


h, w, m = map(int, input().split())
rs = [0] * h
cs = [0] * w

se = set()
for i in range(m):
    r, c =  map(int, input().split())
    rs[r - 1] += 1
    cs[c - 1] += 1
    se.add((r - 1) * w + c - 1)

rmax = max(rs)
cmax = max(cs)

remax = rs.count(rmax)
cemax = cs.count(cmax)
if remax * cemax > m:
    print(rmax + cmax)
    sys.exit(0)

rmaxs = [i for i, _ in enumerate(rs) if _ == rmax]
cmaxs = [i for i, _ in enumerate(cs) if _ == cmax]
for r in rmaxs:
    for c in cmaxs:
        if r * w + c not in se:
            print(rmax + cmax)
            sys.exit(0)

print(rmax + cmax - 1)

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0


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

col = [0]*W
row = [0]*H
elements = []
for i in range(M):
    h,w = map(int,readline().split())
    h,w = h-1,w-1
    elements.append((h,w))
    col[w] += 1
    row[h] += 1

def rank(lst,syoujun=False):
    res2 = list(set(lst))
    ans = []
    if syoujun:
        res2.sort()
    else:
        res2.sort(reverse=True)
    dic1 = dict()
    for i in range(len(res2)):
        dic1[res2[i]] = i
    for i in lst:
        
        ans.append(dic1[i] + 1) 
    
    return ans

col2 = rank(col)
row2 = rank(row)

cmax = col2.count(1)
rmax = row2.count(1)

counter = 0
for i,j in elements:
    if col2[j] == 1 and row2[i] == 1:
        counter += 1

if counter == cmax*rmax:
    print(max(col) + max(row)-1)
else:
    print(max(col) + max(row))

Solution (Python):


h, w, m = map(int, input().split())
bom_set = set()
h_dict = {}
w_dict = {}
for i in range(m):
    hh, ww = map(int, input().split())
    bom_set.add(tuple([hh, ww]))
    if hh in h_dict:
        h_dict[hh] += 1
    else:
        h_dict[hh] = 1
    if ww in w_dict:
        w_dict[ww] += 1
    else:
        w_dict[ww] = 1
h_max_count = max(h_dict.values())
w_max_count = max(w_dict.values())
hh_dict = {}
ww_dict = {}
for hh in h_dict:
    if h_dict[hh] == h_max_count:
        hh_dict[hh] = h_max_count
for ww in w_dict:
    if w_dict[ww] == w_max_count:
        ww_dict[ww] = w_max_count
flag = 0
for hh in hh_dict:
    for ww in ww_dict:
        if tuple([hh, ww]) not in bom_set:
            flag = 1
            break
    if flag == 1:
        break
if flag == 1:
    print(h_max_count + w_max_count)
else:
    print(h_max_count + w_max_count - 1)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
H, W, M = map(int, readline().split())
bomb = [tuple(map(lambda x: int(x) - 1, s.split())) for s in 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):
H, W, m = map(int, input().split())
hw = [list(map(int, input().split())) for _ in range(m)]

row, col = [0] * H, [0] * W
for h, w in hw:
    row[h - 1] += 1
    col[w - 1] += 1

rowmax, colmax = max(row), max(col)
cnt = row.count(rowmax) * col.count(colmax)
for h, w in hw:
    if row[h - 1] == rowmax and col[w - 1] == colmax:
        cnt -= 1
print(rowmax + colmax - (cnt == 0))

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

row, col = [0] * H, [0] * W
for h, w in hw:
    row[h - 1] += 1
    col[w - 1] += 1

rowmax, colmax = max(row), max(col)
cnt = row.count(rowmax) * col.count(colmax)
for h, w in hw:
    if row[h - 1] == rowmax and col[w - 1] == colmax:
        cnt -= 1
print(rowmax + colmax - (cnt == 0))

Solution (Python):
import itertools

H,W,M=map(int,input().split())
d={}
nh=[0]*H
nw=[0]*W
for _ in range(M):
    h,w=map(int,input().split())
    h-=1
    w-=1
    d[(h,w)]=1
    nh[h]+=1
    nw[w]+=1
hmax=max(nh)
wmax=max(nw)
is_h=[i for i in range(H) if nh[i]==hmax]
is_w=[i for i in range(W) if nw[i]==wmax]

for i_h,i_w in itertools.product(is_h,is_w):
    if (i_h,i_w) not in d:
        print(hmax+wmax)
        break
else:
    print(hmax+wmax-1)

Solution (Python):
H, W, M = map(int, input().split())
hw = set()
cnt_h, cnt_w = [0] * H, [0] * W
for i in range(M):
    x, y = map(int, input().split())
    hw.add((x - 1, y - 1))
    cnt_h[x - 1] += 1
    cnt_w[y - 1] += 1

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

h_list = [h for h in range(H) if cnt_h[h] == max_h]
w_list = [w for w in range(W) if cnt_w[w] == max_w]

res = max_h + max_w - 1
for h in h_list:
    for w in w_list:
        if (h, w) not in hw:
            print(res + 1)
            exit()
print(res)

Solution (Python):
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]

def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  H,W,M=LI()
  h=[0]*H
  w=[0]*W
  d={}
  for _ in range(M):
    a,b=LI()
    a-=1
    b-=1
    h[a]+=1
    w[b]+=1
    d[(a,b)]=1

  h_koho=[]
  w_koho=[]

  h_max=max(h)
  w_max=max(w)

  for i,x in enumerate(h):
    if x==h_max:
      h_koho.append(i)
  for i,x in enumerate(w):
    if x==w_max:
      w_koho.append(i)

  for x in h_koho:
    for y in w_koho:
      if (x,y) not in d:
        return h_max+w_max

  return h_max+w_max-1


print(main())

Solution (Python):
import numpy as np
h,w,m=map(int, input().split())
hw=[tuple(map(int,input().split())) for i in range(m)]
H=np.zeros(h)
W=np.zeros(w)
for i in range(m):
    hi,wi=hw[i]
    H[hi-1]+=1
    W[wi-1]+=1
mh=max(H)
mw=max(W)
hmax=[i for i, x in enumerate(H) if x == mh]
wmax=[i for i, x in enumerate(W) if x == mw]
f=0
for i in range(m):
    hi,wi=hw[i]
    if H[hi-1]==mh and W[wi-1]==mw:
        f+=1
if len(hmax)*len(wmax)-f<1:
    print(int(mh+mw-1))
else:
    print(int(mh+mw))

Solution (Python):
def main():
    H,W,M = map(int,input().split())
    s = []
    h_cnt = [0 for i in range(H)]
    w_cnt = [0 for i in range(W)]
    for i in range(M):
        h,w = map(int,input().split())
        s.append([h-1,w-1])
        h_cnt[h-1] += 1
        w_cnt[w-1] += 1
    h_m,w_m = max(h_cnt), max(w_cnt)
    h_mp,w_mp = [],[]
    for i in range(H):
        if h_cnt[i] == h_m:
            h_mp.append(i)
    for i in range(W):
        if w_cnt[i] == w_m:
            w_mp.append(i)
    f = 0
    for i in range(M):
        if h_cnt[s[i][0]] == h_m and w_cnt[s[i][1]] == w_m:
            f += 1
    if len(w_mp)*len(h_mp)-f<1:
        print(h_m+w_m-1)
    else:
        print(h_m+w_m)

if __name__ == "__main__":
    main()

Solution (Python):
h, w, m = map(int, input().split())
s = []
r = [0] * (h + 1)
c = [0] * (w + 1)
for i in range(m):
    bh, bw = map(int, input().split())
    s.append([bh, bw])
    r[bh] += 1
    c[bw] += 1
R = max(r)
nr = {i for i, x in enumerate(r) if x == R}
C = max(c)
nc = {i for i, x in enumerate(c) if x == C}
count = sum(x in nr and y in nc for x, y in s)
print(R + C - (len(nr) * len(nc) == count))

Solution (Python):
import sys
from collections import Counter
H,W,m = map(int,input().split())
h_ls = [0] * H
w_ls = [0] * W
bombers = [(0,0) for _ in range(m)]
for i in range(m):
    h,w = map(int,input().split())
    h_ls[h-1] += 1
    w_ls[w-1] += 1
    bombers[i] = (h-1,w-1)

h_max = max(h_ls)
h_counter = Counter(h_ls)

h_max_args = [0]*h_counter[h_max]
next_ind = 0
for i in range(H):
    if h_ls[i] == h_max:
        h_max_args[next_ind] = i
        next_ind += 1

w_max = max(w_ls)
w_counter = Counter(w_ls)
w_max_args = [0]*w_counter[w_max]
next_ind = 0
for i in range(W):
    if w_ls[i] == w_max:
        w_max_args[next_ind] = i
        next_ind += 1

bombers = set(bombers)
for h in h_max_args:
    for w in w_max_args:
        if not (h,w) in bombers:
            print(w_max+h_max)
            sys.exit()

print(w_max+h_max-1)

Solution (Python):
h, w, m = map(int, input().split())
h_cnt = [0 for _ in range(h)]
w_cnt = [0 for _ in range(w)]
xs = []
ys = []
for _ in range(m):
    y, x = map(int, input().split())
    y -= 1
    x -= 1
    h_cnt[y] += 1
    w_cnt[x] += 1
    xs.append(x)
    ys.append(y)
h_max = max(h_cnt)
w_max = max(w_cnt)
h_max_induce = {i: v for i, v in enumerate(h_cnt) if v == h_max}
w_max_induce = {i: v for i, v in enumerate(w_cnt) if v == w_max}

cnt = 0
for i in range(m):
    x = xs[i]
    y = ys[i]
    if x in w_max_induce and y in h_max_induce:
        cnt += 1
if cnt == len(h_max_induce.keys())*len(w_max_induce.keys()):
    print(h_max+w_max-1)
else:
    print(h_max+w_max)

Solution (Python):
H, W, M = map(int, input().split())
hw = set(tuple(map(lambda x: int(x) - 1,input().split())) for _ in range(M))

rows = [0] * H
cols = [0] * W
for h, w in hw:
    rows[h] += 1
    cols[w] += 1

max_rows = max(rows)
max_cols = max(cols)

max_rows_indexes = [i for i in range(H) if rows[i] == max_rows]
max_cols_indexes = [i for i in range(W) if cols[i] == max_cols]

duplicate = True
if M >= len(max_rows_indexes) * len(max_cols_indexes):
    for h in max_rows_indexes:
        for w in max_cols_indexes:
            if (h, w) not in hw:
                duplicate = False
                break
        if not duplicate:
            break
else:
    duplicate = False

if duplicate:
    print(max_rows + max_cols - 1)
else:
    print(max_rows + max_cols)

Solution (Python):
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]

def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  H,W,M=LI()
  h=[0]*H
  w=[0]*W
  d={}
  for _ in range(M):
    a,b=LI()
    a-=1
    b-=1
    h[a]+=1
    w[b]+=1
    d[(a,b)]=1

  h_koho=[]
  w_koho=[]

  h_max=max(h)
  w_max=max(w)

  for i,x in enumerate(h):
    if x==h_max:
      h_koho.append(i)
  for i,x in enumerate(w):
    if x==w_max:
      w_koho.append(i)

  for x in h_koho:
    for y in w_koho:
      if (x,y) not in d:
        return h_max+w_max

  return h_max+w_max-1


print(main())

Solution (Python):
H,W,M = map(int, input().split())
mx=[0] * W
my=[0] * H
bs ={}
mxx = 0
myy = 0
for i in range(M):
    y,x = map(int, input().split())
    mx[x-1] += 1
    my[y-1] += 1
    mxx = max(mx[x-1], mxx)
    myy = max(my[y-1], myy)
    if y-1 in bs:
        bs[y-1].add(x-1)
    else:
        bs[y-1] = set([x-1])

vx = []
vy = []
for i,x in enumerate(mx):
    if x == mxx:
        vx.append(i)
for i,y in enumerate(my):
    if y == myy:
        vy.append(i)

a = 1

for i in vx:
    for j in vy:
        if i not in bs[j]:
            a = 0
            break
    else:
        continue
    break

ans = mxx + myy - a
print(ans)

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())


H, W, M = MI()
h = [0] * H
w = [0] * W
item_list = [[0, 0] for _ in range(M)]

for i in range(M):
    item_h, item_w = MI()
    item_h -= 1
    item_w -= 1
    h[item_h] += 1
    w[item_w] += 1
    item_list[i][0] = item_h
    item_list[i][1] = item_w

h_max = max(h)
w_max = max(w)
h_cnt = h.count(h_max)
w_cnt = w.count(w_max)
ans = h_max + w_max

cnt = 0
for item in item_list:
    if h[item[0]] == h_max and w[item[1]] == w_max:
        cnt += 1

x = h_cnt * w_cnt
if cnt == x:
    ans -= 1

print(ans)

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

s = [[int(item) -1 for item in input().split()] for _ in range(M)]



col = [0 for _ in range(H)]
row = [0 for _ in range(W)]

bomb_set = set()


for i,j in s:
    col[i] += 1
    row[j] += 1
    bomb_set.add((i,j))    





max_col = max(col)
max_row = max(row)


max_col_index = []
max_row_index = []


for i in range(H):
    if col[i] == max_col:
        max_col_index.append(i)


for j in range(W):
    if row[j] == max_row:
        max_row_index.append(j)


for r in max_row_index:
    for c in max_col_index:
        if (c,r) not in bomb_set:
            ans = max_col + max_row
            print(ans)
            sys.exit()

ans = max_col + max_row -1
print(ans)

Solution (Python):
def main():
    h, w, m = map(int, input().split())
    mapa = {}
    strings = [0]*h
    rows = [0]*w
    for _ in range(m):
        h1, w1 = map(int, input().split())
        if not mapa.get(h1-1, 0):
            mapa[h1-1] = []
        mapa[h1-1].append(w1-1)
        strings[h1-1] += 1
        rows[w1-1] += 1
    
    max_s = max(strings)
    max_r = max(rows)
    strings_max = []
    rows_max = []
    for i in range(h):
        if strings[i] == max_s:
            strings_max.append(i)
    for i in range(w):
        if rows[i] == max_r:
            rows_max.append(i)
    
    forbidden_positions = set(mapa[strings_max[0]])
    for item in strings_max[1:]:
        forbidden_positions.intersection_update(set(mapa[item]))

    answer_set = set(rows_max).difference(forbidden_positions)
    if answer_set:
        print(max_s + max_r)
    else:
        print(max_s + max_r - 1)

main()

Solution (Python):



import numpy
from collections import defaultdict

def main():
  h, w, m = map(int, input().strip().split())
  fieldh = numpy.zeros(h, dtype=numpy.int)
  fieldw = numpy.zeros(w, dtype=numpy.int)
  field = defaultdict(int)
  for _ in range(m):
    ih, iw = map(int, input().strip().split())
    fieldh[ih-1] += 1
    fieldw[iw-1] += 1
    field[ih-1, iw-1] = 1
    
  max_h = numpy.max(fieldh)
  max_w = numpy.max(fieldw)
  argsort_h = [ ih for ih in range(h) if fieldh[ih] == max_h ]
  argsort_w = [ iw for iw in range(w) if fieldw[iw] == max_w ]
  
  max_cnt = 0
  for ih in argsort_h:
    for iw in argsort_w:
      if field[ih, iw] == 0:
        max_cnt = max(max_cnt, fieldh[ih]+fieldw[iw])
        print(max_cnt)
        return 
      else:
        max_cnt = max(max_cnt, fieldh[ih]+fieldw[iw]-1)
  print(max_cnt)

if __name__=='__main__':
  main()

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

def main():
    h, w, m = map(int, 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 = [i for i, v in enumerate(bomb_row) if v == bomb_row_max]
    col_index = [i for i, v in enumerate(bomb_col) if v == bomb_col_max]
    ans = bomb_col_max + bomb_row_max
    for row in row_index:
        for col in col_index:
            if (row, col) not in bomb_pos:
                print(ans)
                return
    print(ans-1)
main()

Solution (Python):
def resolve():
    '''
    code here
    '''
    H, W, M = [int(item) for item in input().split()]
    targets = [[int(item) -1 for item in input().split()] for _ in range(M)]

    col = [0 for _ in range(H)]
    row = [0 for _ in range(W)]

    bomb_set = set()
    


    for i,j in targets:
        col[i] += 1
        row[j] += 1
        bomb_set.add((i,j))

    max_col = max(col)
    max_row = max(row)
    max_col_index = []
    max_row_index = []

    for i in range(H):
        if col[i] == max_col:
            max_col_index.append(i)

    for i in range(W):
        if row[i] == max_row:
            max_row_index.append(i)

    res = 0
    for item in max_col_index:
        for jtem in max_row_index:
            if (item,jtem) not in bomb_set:
                res =max_col + max_row
                break
        else:
            res = max(res, max_col + max_row -1 )
    print(res)

if __name__ == "__main__":
    resolve()

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

hlist = [0]*H
wlist = [0]*W
sets = set()
for i in range (M):
    h,w = map(lambda x: int(x)-1,input().split())
    hlist[h]+=1
    wlist[w]+=1
    sets.add((h,w))

hmax = 0
hind = []
for i in range(H):
    if hmax < hlist[i]:
        hmax = hlist[i]
        hind = [i]
    elif hmax == hlist[i]:
        hind.append(i)


wmax = 0
wind = []
for i in range(W):
    if wmax < wlist[i]:
        wmax = wlist[i]
        wind = [i]
    elif wmax == wlist[i]:
        wind.append(i)


flag = False
for h in hind:
    for w in wind:
        if (h,w) not in sets:
            flag = True
            break
    if flag:
        break

if flag:
    print(hmax + wmax)
else:
    print(hmax + wmax - 1)

Solution (Python):
import heapq
H, W, M = map(int, input().split())
bomb = []
for i in range(M):
    h, w = map(int, input().split())
    h-=1; w-=1
    bomb.append((h, w))

row = [0] * H
col = [0] * W

for h, w in bomb:
    row[h] += 1
    col[w] += 1

lmax = max(row) + max(col)
row = [(r, i) for i, r in enumerate(row)]
col = [(c, i) for i, c in enumerate(col)]
row.sort(reverse=True)
col.sort(reverse=True)
bomb = set(bomb)

for r, i in row:
    for c, j in col:
        if r+c < lmax: break
        if not (i, j) in bomb:
            print(lmax)
            exit()
print(lmax-1)

Solution (Python):
import heapq
H, W, M = map(int, input().split())
bomb = []
for i in range(M):
    h, w = map(int, input().split())
    h-=1; w-=1
    bomb.append((h, w))

row = [0] * H
col = [0] * W

for h, w in bomb:
    row[h] += 1
    col[w] += 1

lmax = max(row) + max(col)
row = [(r, i) for i, r in enumerate(row)]
col = [(c, i) for i, c in enumerate(col)]
row.sort(reverse=True)
col.sort(reverse=True)
bomb = set(bomb)

for r, i in row:
    for c, j in col:
        if r+c < lmax: break
        if not (i, j) in bomb:
            print(lmax)
            exit()
print(lmax-1)

Solution (Python):
H, W, M = map(int, input().split())
R = [0] * H
C = [0] * W
B = []
for i in range(M):
    hi, wi = map(lambda s: int(s) - 1, input().split())
    R[hi] += 1
    C[wi] += 1
    B.append((hi, wi))

Rmax = max(R)
Cmax = max(C)
ans = Rmax + Cmax
if R.count(Rmax) * C.count(Cmax) == sum([R[hi] == Rmax and C[wi] == Cmax for hi, wi in B]):
    ans -= 1

print(ans)

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

    sh[h - 1] += 1
    sw[w - 1] += 1

mh = max(sh)
mw = max(sw)

count = 0
for i in range(M):
    if sh[hw[i][0]] == mh and sw[hw[i][1]] == mw:
        count += 1

if sh.count(mh) * sw.count(mw) > count:
    print(mh + mw)
else:
    print(mh + mw - 1)

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

bomb = []
cnth = [0 for i in range(h)]
cntw = [0 for i in range(w)]
bomh = [set() for i in range(h)]
bomw = [set() for i in range(w)]
for i in range(m):
  x, y = map(int, input().split())
  x -= 1
  y -= 1
  cnth[x] += 1
  cntw[y] += 1
  bomh[x].add(y)
  bomw[y].add(x)
  bomb.append([x,y])
maxh = max(cnth)
maxw = max(cntw)
ans = maxh + maxw - 1

xx = []
yy = []
for i in range(h):
  if maxh == cnth[i]:
    xx.append(i)
for i in range(w):
  if maxw == cntw[i]:
    yy.append(i)

for i in xx:
  for j in yy:
    if j not in bomh[i]:
      print (ans+1)
      exit()
print (ans)

Solution (Python):
def main():
    H, W, M = map(int, input().split())
    bomb_row = [0 for _ in range(H)]  
    bomb_column = [0 for _ in range(W)]  
    bomb_map = set()  

    for _ in range(M):
        r, c = map(int, input().split())
        bomb_row[r - 1] += 1
        bomb_column[c - 1] += 1
        bomb_map.add((r - 1, c - 1))

    max_row = max(bomb_row)  
    max_column = max(bomb_column)  
    ans = max_row + max_column

    bomb_max_row = []  
    bomb_max_column = []  

    for i in range(H):
        if bomb_row[i] == max_row:
            bomb_max_row.append(i)
    for i in range(W):
        if bomb_column[i] == max_column:
            bomb_max_column.append(i)

    for r in bomb_max_row:
        for c in bomb_max_column:
            if (r, c) not in bomb_map:  
                print(ans)
                return

    print(ans - 1)  


if __name__ == "__main__":
    main()

Solution (Python):
from collections import defaultdict
from collections import deque
from collections import Counter
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

h,w,m = readInts()
countx = defaultdict(set)
county = defaultdict(set)
cx,cy = defaultdict(int),defaultdict(int)
for i in range(m):
	x,y = readInts()
	countx[x].add(y)
	county[y].add(x)
	cx[x]+=1
	cy[y]+=1

maxxv = max(cx.values())
maxyv = max(cy.values())
maxxi = [i for i in countx if len(countx[i])==maxxv]
maxyi = [i for i in county if len(county[i])==maxyv]

ans = maxyv+maxxv

for i in maxxi:
	for j in maxyi:
		if j not in countx[i]:
			print(ans)
			exit()

print(ans-1)

Solution (Python):
H, W, M = map(int, input().split())
hw = set()
sum_h = [0] * W
sum_w = [0] * H
for _ in range(M):
    h, w = map(int, input().split())
    hw.add((h, w))
    h -= 1
    w -= 1
    sum_h[w] += 1
    sum_w[h] += 1


max_h = max(sum_h)
max_w = max(sum_w)

new_H = [h for h in range(H) if sum_w[h] == max_w]
new_W = [w for w in range(W) if sum_h[w] == max_h]

max_num = max_h + max_w - 1
for i in new_H:
    for j in new_W:
        if (i + 1, j + 1) not in hw:
            print(max_h + max_w)
            exit()
        else:
            continue
        break

print(max_num)

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

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

basyo = []

for i in range(m):
    hh,ww = map(int,input().split())
    basyo.append((hh-1,ww-1))
    
    tate[hh-1] += 1
    yoko[ww-1] += 1
    
tt,yy = 0,0
x = max(tate)

for i in tate:
    if i == x:
        tt += 1
        
y = max(yoko)
for i in yoko:
    if i == y:
        yy += 1
        
ans = x+y
pl = tt * yy

cur = 0
for i,j in basyo:
    if tate[i] + yoko[j] == ans:
        cur += 1
        
if pl > cur:
    print(ans)
else:
    print(ans-1)

Solution (Python):
h , w , m = map(int,input().split())
retu = [0 for i in range(w)]
gyo = [0 for i in range(h)]
basyo = set()
for i in range(m):
    a , b = map(lambda x:int(x)-1,input().split())
    retu[b] += 1
    gyo[a] += 1
    basyo.add((a,b))
t = max(retu)
mare = [i for i, v in enumerate(retu) if v == t]
s = max(gyo)
magy = [i for i, v in enumerate(gyo) if v == s]

for i in magy:
    for j in mare:
        if not (i,j) in basyo:
            print(s+t)
            exit()
print(s+t-1)

Solution (Python):






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





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)
    ans = 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:
        ans -= 1
    
    return ans





ans = solve(h,w,m,hw_list)
print(ans)

Solution (Python):


def f(h,w,m,ins):
    yp = [0]*h
    xp = [0]*w
    s = set()
    for hi,wi in ins:
        s.add((hi-1,wi-1))
        yp[hi-1] += 1
        xp[wi-1] += 1
    ypm = max(yp)
    xpm = max(xp)
    yps = [i for i in range(h) if yp[i] == ypm]
    xps = [i for i in range(w) if xp[i] == xpm]
    ans = ypm+xpm
    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):
def solve():
    H,W,M = map(int,input().split())
    h_counter = [0] * H
    w_counter = [0] * W

    opponents = []
    for _ in range(M):
        h, w = map(lambda x:int(x)-1, input().split())
        h_counter[h] += 1
        w_counter[w] += 1
        opponents.append((h,w))
    
    h_max = max(h_counter)
    w_max = max(w_counter)

    h_max_area_cnt = 0
    h_max_area_nums = set()
    for h in range(H):
        if h_counter[h] == h_max:
            h_max_area_nums.add(h)
            h_max_area_cnt += 1

    w_max_area_cnt = 0
    w_max_area_nums = set()
    for w in range(W):
        if w_counter[w] == w_max:
            w_max_area_nums.add(w)
            w_max_area_cnt += 1

    max_area_cnt = h_max_area_cnt * w_max_area_cnt

    opponent_in_max_area = 0
    for h,w in opponents:
        if h in h_max_area_nums and w in w_max_area_nums:
            opponent_in_max_area += 1

    if max_area_cnt > opponent_in_max_area:
        print(h_max + w_max)
    else:
        print(h_max + w_max - 1)

if __name__ == '__main__':
    solve()