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



Sample input:
2 3 3
2 2
1 1
1 3


Sample output:
3

Submission status: Accepted

Solution code:
def main():
    import sys
    from collections import defaultdict
    readline = sys.stdin.readline

    H, W, M = map(int, readline().rstrip().split())
    HB = [0] * H
    WB = [0] * W
    HW = defaultdict(lambda : False)
    for _ in range(M):
        h, w = map(int, readline().rstrip().split())
        h -= 1
        w -= 1
        HW[(h,w)] = True
        HB[h] += 1
        WB[w] += 1
    hb = max(HB)
    max_h = [i for i, x in enumerate(HB) if x == hb]
    wb = max(WB)
    max_w = [i for i, x in enumerate(WB) if x == wb]
    
    ans = hb + wb
    for mh in max_h:
        for mw in max_w:
            if HW[(mh, mw)] == False:
                print(ans)
                sys.exit(0)
    print(ans - 1)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
H,W,M=map(int,input().split())
L=[0 for _ in range(H)]
C=[0 for _ in range(W)]
B={}
for _ in range(M):
    a,b=map(int,input().split())
    B[str(a)+","+str(b)]=0
    L[a-1]+=1
    C[b-1]+=1

LM=max(L)
CM=max(C)

l=[]
c=[]
for j in range(H):
    if L[j]==LM:
        l.append(j)

for k in range(W):
    if C[k]==CM:
        c.append(k)

ans=LM+CM-1

for a in l:
    for b in c:
        if str(a+1)+","+str(b+1) not in B:
            ans=LM+CM
            break
    if ans==LM+CM:
        break
print(ans)

Submission status: Accepted

Solution code:
H,W,M = map(int,input().split())
Bomb = []
H_num = [0 for _ in range(H)]
W_num = [0 for _ in range(W)]
for i in range(M):
  a,b = map(int,input().split())
  a-=1;b-=1
  Bomb.append([a,b])
  H_num[a] += 1
  W_num[b] += 1

HMAX = max(H_num);WMAX = max(W_num)
ans = HMAX+WMAX
HMAX_list = set([]); WMAX_list = set([])
for i in range(H):
  if H_num[i] == HMAX:
    HMAX_list.add(i)
for i in range(W):
  if W_num[i] == WMAX:
    WMAX_list.add(i)
kosu = len(HMAX_list)*len(WMAX_list)
cnt = 0
for z in Bomb:
  x = z[0];y = z[1]
  if x in HMAX_list and y in WMAX_list:
    cnt += 1
#print(kosu,cnt)
if cnt < kosu:
  print(ans)
else:
  print(ans-1)

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import collections
from collections import deque
from collections import defaultdict

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()

NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()

def main():
    N, W, M = NMI()
    
    BOMB_HW = [NLI() for _ in range(M)]
    
    bomb_h,bomb_w = zip(*BOMB_HW)
    
    dd = defaultdict(bool)
    for m in range(M):
        dd[str(bomb_h[m]) + "-" + str(bomb_w[m])] = True
    
    c_h = collections.Counter(bomb_h)
    c_w = collections.Counter(bomb_w)
    
    bomb_most_h = c_h.most_common()[0]
    bomb_most_w = c_w.most_common()[0]

    bh_destroy = bomb_most_h[1]
    bw_destroy = bomb_most_w[1]
 
    candidate_h = [i[0] for i in c_h.items() if i[1] == bh_destroy]
    candidate_w = [i[0] for i in c_w.items() if i[1] == bw_destroy]

    ans = bh_destroy+bw_destroy
    

    for bh in candidate_h:
        for bw in candidate_w:   
            if not(dd[str(bh) + "-" + str(bw)]):
                print(ans)
                sys.exit()
    print(ans-1)
    


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
H,W,M = map(int,input().split())
lsx = [0] + [0]*H
lsy = [0] + [0]*W
lsxy = []
for i in range(M):
    x,y = map(int,input().split())
    lsx[x] += 1
    lsy[y] += 1
    lsxy.append([x,y])
maxx = max(lsx)
maxy = max(lsy)
cx = lsx.count(maxx)
cy = lsy.count(maxy)
al = cx*cy
for i in range(M):
    if lsx[lsxy[i][0]] == maxx and lsy[lsxy[i][1]] == maxy:
        al -= 1
if al == 0:
    factor = 1
else:
    factor = 0        
print(maxx+maxy-factor)

Submission status: Accepted

Solution code:
h, w, m = map(int, input().split())
boms = set()
Hs = [0] * h
Ws = [0] * w
for i in range(m):
  _h, _w = map(int, input().split())
  boms.add((_h - 1, _w - 1))
  Hs[_h - 1] += 1
  Ws[_w - 1] += 1
Hmax = max(Hs)
Hcount = Hs.count(Hmax)
Wmax = max(Ws)
Wcount = Ws.count(Wmax)
Hindexs = [0] * Hcount
Windexs = [0] * Wcount
hcount = 0
wcount = 0
for i in range(h):
  if Hs[i] == Hmax:
    Hindexs[hcount] = i
    hcount += 1
for i in range(w):
  if Ws[i] == Wmax:
    Windexs[wcount] = i
    wcount += 1
if m < len(Hindexs) * len(Windexs):
  print(Hmax + Wmax)
  exit()
for hindex in Hindexs:
  for windex in Windexs:
    if (hindex,windex) not in boms: 
      print(Hmax + Wmax)
      exit()
print(Hmax + Wmax -1)

Submission status: Accepted

Solution code:
H, W, M = map(int, input().split())
lh = [0]*(H+1)
lw = [0]*(W+1)
l = []
for _ in range(M):
  h, w = map(int, input().split())
  lh[h] += 1
  lw[w] += 1
  l += [[h, w]]

maxh = max(lh)
maxw = max(lw)
cnt = 0

ch = lh.count(maxh)
cw = lw.count(maxw)
for y, x in l:
  if lh[y] == maxh and lw[x] == maxw:
    cnt += 1

ans = maxh+maxw
if cnt == ch*cw:
  ans -= 1

print(ans)

Submission status: Accepted

Solution code:
h, w, m =  map(int, input().split())
bom_list = []
h_list = [0] * h
w_list = [0] * w

for i in range(m):
    a, b =  map(int, input().split())
    bom_list.append([a - 1, b - 1])
    h_list[a - 1] += 1
    w_list[b - 1] += 1

max_h = max(h_list)
max_w = max(w_list)
ans = max_h + max_w
count = 0

for bom in bom_list:
    if h_list[bom[0]] == max_h and w_list[bom[1]] == max_w:
        count += 1

if h_list.count(max_h) * w_list.count(max_w) == count:
    ans -= 1

print(ans)

Submission status: Accepted

Solution code:
ans=0

h,w,m=map(int,input().split())
d=list()
from collections import defaultdict
r=defaultdict(int)
c=defaultdict(int)
r=[0]*(h+1)
c=[0]*(w+1)
for i in range(m):
  a,b=map(int,input().split())
  d+=(a,b),
  r[a]+=1
  c[b]+=1
R=max(r)
C=max(c)

x=0
nr=r.count(R)
nc=c.count(C)
for i in range(m):
    a,b=d[i]
    if r[a]==R and c[b]==C:
        x+=1

ans=R+C
if x==nr*nc:
    ans-=1
print(ans)

Submission status: Accepted

Solution code:
import itertools
H,W,m = map(int,input().split())
hs = [0 for _ in range(H)]
ws = [0 for _ in range(W)]
data = set()

for j in range(m):
	h,w = map(int,input().split())
	data.add((h-1, w-1))
	hs[h-1] += 1
	ws[w-1] += 1

#print(hs, ws)

maxhs = 0
maxws = 0
maxhsindex = []
maxwsindex = []

for i in range(H):
	targ = hs[i]
	if targ > maxhs:
		maxhs = targ
		maxhsindex = [i]
	elif targ == maxhs:
		maxhsindex.append(i)

for i in range(W):
	targ = ws[i]
	if targ > maxws:
		maxws = targ
		maxwsindex = [i]
	elif targ == maxws:
		maxwsindex.append(i)


for i, j in itertools.product(maxhsindex, maxwsindex):
	#print(i, j)
	if (i, j) not in data:
		print(max(hs)+max(ws))
		exit()
print(max(hs)+max(ws)-1)

Submission status: Accepted

Solution code:
from collections import defaultdict


def main():
    height, width, target_count = [int(x) for x in input().split()]
    count_by_height = defaultdict(int)
    count_by_width = defaultdict(int)
    bomb_is = set()
    for _ in range(target_count):
        bom_h, bom_w = [int(x) - 1 for x in input().split()]
        count_by_height[bom_h] += 1
        count_by_width[bom_w] += 1
        bomb_is.add((bom_h, bom_w))
    max_h = max(v for v in count_by_height.values())
    max_w = max(v for v in count_by_width.values())
    max_h_index = [i for i, x in count_by_height.items() if x == max_h]
    max_w_index = [i for i, x in count_by_width.items() if x == max_w]
    no_cross = any(
        (hh, ww) not in bomb_is for hh in max_h_index for ww in max_w_index)
    return max_h + max_w - (not no_cross)


if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
h, w, m = map(int, input().split())
r = [0] * (h + 1)
c = [0] * (w + 1)
s = []
for _ in range(m):
    x, y = map(int, input().split())
    r[x] += 1
    c[y] += 1
    s.append([x, y])
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 = 0
for x, y in s:
    count += x in nr and y in nc
print(R + C - (len(nr) * len(nc) == count))

Submission status: Accepted

Solution code:
import sys
H, W, M = map(int, sys.stdin.readline().split())
hlst = {}
wlst = {}
hwlst = []

for i in range(M):
    h, w = sys.stdin.readline().split()
    if h in hlst:
        hlst[h] += 1
    else:
        hlst[h] = 1

    if w in wlst:
        wlst[w] += 1
    else:
        wlst[w] = 1

    hwlst.append([h, w])

h = max(hlst.values())
w = max(wlst.values())
hmax = [i for i in hlst if hlst[i] == h]
wmax = [i for i in wlst if wlst[i] == w]
num = len(hmax)*len(wmax)
ans = h + w -1


for i in hwlst:
    if hlst[i[0]] == h and wlst[i[1]] == w:
        num -= 1

if num == 0:
    print(h+w-1)
else:
    print(h+w)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import collections

H, W, M = map(int, input().split())
tate = []
yoko = []
S = set()

for i in range(M):
    h, w = map(int, input().split())
    tate.append(h-1)
    yoko.append(w-1)
    S.add((h-1, w-1))

tate = collections.Counter(tate)
yoko = collections.Counter(yoko)
yoko_max = yoko.most_common()[0][1]
yoko_index = []
tate_max = tate.most_common()[0][1]
tate_index = []

for k, v in yoko.most_common():
    if v == yoko_max: yoko_index.append(k)
    else: break

for k, v in tate.most_common():
    if v == tate_max: tate_index.append(k)
    else: break

for i in yoko_index:
    for j in tate_index:
        if (j, i) in S:
            continue
        else:
            print(tate_max + yoko_max)
            exit()


print(tate_max + yoko_max - 1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

def main():
    H, W, M = map(int, input().split())
    bomb = [list(map(int, input().split()))for i in range(M)]
    H_bomb = [0] * (H + 1)
    W_bomb = [0] * (W + 1)
    for b in bomb:
        H_bomb[b[0]] += 1
        W_bomb[b[1]] += 1
    max_h, max_w = max(H_bomb), max(W_bomb)
    cnt = H_bomb.count(max_h) * W_bomb.count(max_w)
    for b in bomb:
        if H_bomb[b[0]] == max_h and W_bomb[b[1]] == max_w:
            cnt -= 1
    print(max_h + max_w + bool(cnt) - 1)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#coding: utf-8
from collections import defaultdict
import sys

H, W, M = (int(x) for x in input().split())

NH = [[0, i] for i in range(H)]
NW = [[0, i] for i in range(W)]

B = defaultdict(int) 
for i in range(M):
    h, w = (int(x)-1 for x in input().split())
    B[(h, w)] = 0
    NH[h][0] += 1
    NW[w][0] += 1

NH.sort(reverse=True)
NW.sort(reverse=True)
maxH = NH[0][0]
maxW = NW[0][0]

for i in range(H):
    countH, idxH = NH[i] 
    if countH != maxH:
        break

    for j in range(W):
        countW, idxW = NW[j] 
        if countW!= maxW:
            break

        if (idxH, idxW) not in B:
            print(maxH + maxW)
            sys.exit(0)

print(maxH + maxW - 1)

Submission status: Accepted

Solution code:
import numpy as np

[H, W, M] = map(int, input().split())
colsum = np.zeros(W+1)
rowsum = np.zeros(H+1)
bombs = []
rowbombs = [set() for i in range(H+1)]
for i in range(M):
    [h, w] = map(int, input().split())
    rowsum[h] += 1
    colsum[w] += 1
    bombs.append([h, w])
    rowbombs[h].add(w)

maxcol = max(colsum)
maxrow = max(rowsum)

colchk = np.where(colsum==maxcol)
rowchk = np.where(rowsum==maxrow)

for i in range(len(rowchk[0])):
    for j in range(len(colchk[0])):
        if colchk[0][j] not in rowbombs[rowchk[0][i]]:
            print(int(max(colsum)+max(rowsum)))
            quit()

print(int(max(colsum)+max(rowsum))-1)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

ch = [[0, i] for i in range(h)]
cw = [[0, i] for i in range(w)]
ngh = {}
ngw = {}

for h, w in hw:
    ch[h-1][0] += 1
    cw[w-1][0] += 1

ch.sort(key=lambda x: x[0], reverse=True)
cw.sort(key=lambda x: x[0], reverse=True)
mch = ch[0][0]
mcw = cw[0][0]

for cnt, i in ch:
    if cnt == mch:
        ngh[i] = 1
    else:
        break

for cnt, i in cw:
    if cnt == mcw:
        ngw[i] = 1
    else:
        break

cnt = 0
for h, w in hw:
    if h-1 in ngh and w-1 in ngw:
        cnt += 1

lenh = len(list(ngh.keys()))
lenw = len(list(ngw.keys()))

if lenh * lenw == cnt:
    print(mch + mcw -1)
else:
    print(mch + mcw)

Submission status: Accepted

Solution code:
from bisect import bisect_left
import sys
input = sys.stdin.readline
H,W,m = map(int,input().split())
h = [0]*(H+1)
w = [0]*(W+1)
c = [[-1,10**6] for i in range(H+1)]
for i in range(m):
    a,b = map(int,input().split())
    h[a] += 1
    w[b] += 1
    c[a].append(b)
mh = max(h)
mw = max(w)
hkouho = []
wkouho = []
for i in range(H+1):
    if h[i] == mh:
        hkouho.append(i)
for i in range(W+1):
    if w[i] == mw:
        wkouho.append(i)
for i in hkouho:
    c[i].sort()
    for j in wkouho:
        if c[i][bisect_left(c[i],j)] != j:
            print(mh+mw)
            exit()
print(mh+mw-1)

Submission status: Accepted

Solution code:
h, w, m = map(int, input().split())
hw = []
h_cnt = [0] * h
w_cnt = [0] * w
hw = []
hw_set = set()
for _ in range(m):
    r, c = map(int, input().split())
    r -= 1
    c -= 1
    h_cnt[r] += 1
    w_cnt[c] += 1
    hw.append((r, c))
    hw_set.add((r, c))
mh = max(h_cnt)
mw = max(w_cnt)
cand_r = []
for i in range(h):
    if h_cnt[i] == mh:
        cand_r.append(i)
cand_c = []
for j in range(w):
    if w_cnt[j] == mw:
        cand_c.append(j)
ans = mh + mw
for i in cand_r:
    for j in cand_c:
        if not (i, j) in hw_set:
            print(ans)
            exit()
print(ans - 1)

Submission status: Accepted

Solution code:
H, W, M = map(int, input().split())
h = [0] * (H+1)
w = [0] * (W+1)
l = set()
for i in range(M):
  x, y = map(int, input().split())
  h[x] += 1
  w[y] += 1
  l.add((x, y))
  
c = max(h)
d = max(w)
a = [i for i, x in enumerate(h) if x == c]
b = [i for i, x in enumerate(w) if x == d]

flag = False
for s in a:
  for t in b:
    if (s, t) not in l:
      flag = True
      break
  if flag:
    break
ans = c+d
if not flag:
  ans -= 1
print(ans)

Submission status: Accepted

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

bombDict = dict()
bombDictH = dict()
bombDictW = dict()


for i in range(h+1):
    bombDict[i] = set()
    bombDictH[i] = 0

for i in range(w+1):
    bombDictW[i] = 0


for i in range(m):
    bombH,bombW = list(map(int,input().split()))
    bombDictH[bombH] += 1
    bombDictW[bombW] += 1
    bombDict[bombH].add(bombW)

maxH = max(bombDictH,key = bombDictH.get)
maxW = max(bombDictW,key = bombDictW.get)
maxHV = max(bombDictH.values())
maxWV = max(bombDictW.values())

hList = list(bombDictH.values())
wList = list(bombDictW.values())

maxHList = []
maxWList = []

if hList.count(maxHV) > 1:
    for i in range(h+1):
        if hList[i] == maxHV:
            maxHList.append(i)
else:
    maxHList.append(maxH)

if wList.count(maxWV) > 1:
    for i in range(w+1):
        if wList[i] == maxWV:
            maxWList.append(i)
else:
    maxWList.append(maxW)

ans = 0

for i in maxHList:
    for j in maxWList:
        if j in bombDict[i]:
            ans = max(ans,bombDictH[i] + bombDictW[j] - 1)
        else:
            ans = max(ans,bombDictH[i] + bombDictW[j])
            break

print(ans)

Submission status: Accepted

Solution code:

from bisect import bisect_left
import sys
input = sys.stdin.readline
H,W,m = map(int,input().split())
h = [0]*(H+1)
w = [0]*(W+1)
c = [[-1,10**6] for i in range(H+1)]
for i in range(m):
    a,b = map(int,input().split())
    h[a] += 1
    w[b] += 1
    c[a].append(b)
mh = max(h)
mw = max(w)
hkouho = []
wkouho = []
for i in range(H+1):
    if h[i] == mh:
        hkouho.append(i)
for i in range(W+1):
    if w[i] == mw:
        wkouho.append(i)
for i in range(H+1):
    c[i].sort()
# print(hkouho)
# print(wkouho)
for i in hkouho:
    for j in wkouho:
        # print(c[i],i,j,bisect_left(c[i],j))
        if c[i][bisect_left(c[i],j)] != j:
            print(mh+mw)
            exit()
print(mh+mw-1)

Submission status: Accepted

Solution code:
from collections import deque

def argmax_poss(a):
    if len(a) == 0:
        return []
    all_ = [0]
    max_ = a[0]
    for i in range(1, len(a)):
        if a[i] > max_:
            all_ = [i]
            max_ = a[i]
        elif a[i] == max_:
            all_.append(i)
    return all_

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

h_scores = [0 for i in range(h)]
w_scores = [0 for i in range(w)]

record = set()

for i in range(m):
  h,w = map(int,input().split())
  record.add((h-1,w-1))
  h_scores[h-1] += 1
  w_scores[w-1] += 1

max_h_poss = argmax_poss(h_scores)
max_w_poss = argmax_poss(w_scores)

out_pos_count = 0 

is_ok = False

for h in max_h_poss:
    for w in max_w_poss:
        if (h,w) not in record:
            is_ok = True
            break
        
if is_ok:
    print(max(h_scores) + max(w_scores))
else:
    print(max(h_scores) + max(w_scores)-1)

Submission status: Accepted

Solution code:
from collections import Counter
import sys
H, W, M = map(int, input().split())

h = [0] * M
w = [0] * M
for i in range(M):
    h[i], w[i] = map(int, input().split())


S = Counter(h)
T = Counter(w)

if (M == 1):
    print(1)

else:
    if (S.most_common()[0][1] > T.most_common()[0][1]):
        smc = S.most_common()[0][0]
        for i in range(M):
            if (h[i] == smc):
                w[i] = 'boom'

        w1 = [s for s in w if s != 'boom']
        T = Counter(w1)
        if (len(w1) == 0):
            print(M)
            sys.exit()
        tmc = T.most_common()[0][0]
        for i in range(len(w1)):
            if (w1[i] == tmc):
                w1[i] = 'boom' 

        w2 = [s for s in w1 if s != 'boom']
        print(M-len(w2))

    else:
        tmc = T.most_common()[0][0]
        for i in range(M):
            if (w[i] == tmc):
                h[i] = 'boom'

        h1 = [s for s in h if s != 'boom']
        S = Counter(h1)
        if (len(h1) == 0):
            print(M)
            sys.exit()
        smc = S.most_common()[0][0]
        for i in range(len(h1)):
            if (h1[i] == smc):
                h1[i] = 'boom' 

        h2 = [s for s in h1 if s != 'boom']
        print(M-len(h2))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import collections as cl
import sys


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


def main():
    h, w, m = MI()
    targets = [[] for i in range(h)]
    tate = [[] for i in range(w)]
    tate_length = [0] * w
    original = []
    lengths = [0] * h
    for i in range(m):
        target = LI()
        original.append(target)
        targets[target[0] - 1].append(target[1] - 1)
        tate[target[1] - 1].append(target[0] - 1)
        lengths[target[0] - 1] += 1
        tate_length[target[1] - 1] += 1

    max_length = max(lengths)
    max_length_tate = max(tate_length)

    max_w_indexes = [i for i, x in enumerate(
        tate_length) if x == max_length_tate]

    max_h_indexes = [i for i, x in enumerate(
        lengths) if x == max_length]

    max_h_indexes = set(max_h_indexes)
    max_w_indexes = set(max_w_indexes)

    ttl = len(max_h_indexes) * len(max_w_indexes)

    for target in original:
        if target[0] - 1 in max_h_indexes and target[1] - 1 in max_w_indexes:
            ttl -= 1

    if ttl > 0:
        print(max_length + max_length_tate)
    else:
        print(max_length+max_length_tate - 1)


main()

Submission status: Accepted

Solution code:
def main():
    h, w, m = map(int, input().split())
    h_bomb_dict = {}
    w_bomb_dict = {}
    bombs = []
    for i in range(m):
        zh, zw = map(int, input().split())
        bombs.append((zh, zw))
        if zh in h_bomb_dict.keys():
            h_bomb_dict[zh] += 1
        else:
            h_bomb_dict[zh] = 1

        if zw in w_bomb_dict.keys():
            w_bomb_dict[zw] += 1
        else:
            w_bomb_dict[zw] = 1
    bombs = set(bombs)

    hbombs = max(h_bomb_dict.values())
    wbombs = max(w_bomb_dict.values())
    h_maxs = []
    w_maxs = []
    for key, v in h_bomb_dict.items():
        if v == hbombs:
            h_maxs.append(key)

    for key, v in w_bomb_dict.items():
        if v == wbombs:
            w_maxs.append(key)

    for h in h_maxs:
        for w in w_maxs:
            if not (h, w) in bombs:
                print(hbombs + wbombs)
                return
    print(hbombs + wbombs-1)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import itertools

input = sys.stdin.readline

def my_index_multi(l, x):
    return [k for k, _x in enumerate(l) if _x == x]

h,w,m = map(int,input().split())
l_h = [0]*(h+1)
l_w = [0]*(w+1)
l_b = set()

for _ in range(m):
    h,w = map(int,input().split())
    l_h[h] += 1
    l_w[w] += 1
    l_b.add((h,w))

m_h = max(l_h)
m_w = max(l_w)

l_h_2 = []
for i, x in enumerate(l_h):
    if x == m_h:
        l_h_2.append(i)

l_w_2 = []
for i, x in enumerate(l_w):
    if x == m_w:
        l_w_2.append(i)

ans = m_h + m_w

for i,j in itertools.product(l_h_2,l_w_2):
    if (i,j) not in l_b:
        break
else:
    ans -= 1

print(ans)

Submission status: Accepted

Solution code:
import glob
import os
import numpy as np

bn = os.path.basename(__file__).split('.')[0]
dn = os.path.dirname(__file__).split('/')

# 問題ごとのディレクトリのトップからの相対パス
REL_PATH = dn[len(dn)-2] + '\\' + dn[len(dn)-1] + '\\' + bn

# テスト用ファイル置き場のトップ
TOP_PATH = 'C:\\AtCoder'

class Common:
    problem = []
    index = 0

    def __init__(self, rel_path):
        self.rel_path = rel_path

    def initialize(self, path):
        file = open(path)
        self.problem = file.readlines()
        self.index = 0
        return

    def input_data(self):
        try:
            IS_TEST
            self.index += 1
            return self.problem[self.index - 1]

        except NameError:
            return input()

    def resolve(self):
        pass

    def exec_resolve(self):
        try:
            IS_TEST
            for path in glob.glob(TOP_PATH + '\\' + self.rel_path + '/*.txt'):
                print("Test: " + path)
                self.initialize(path)
                self.resolve()
                print("\n\n")
        except NameError:
            self.resolve()


class Solver(Common):

    def resolve(self):

        H, W, M = map(int, self.input_data().split())
        B = [None for i in range(M)]

        ver_num =[0 for i in range(W+1)] # 列ごとの合計(行分ある)
        hol_num = [0 for i in range(H+1)] # 行ごとの合計(列分ある)

        for i in range(M):
            B[i] = list(map(int, self.input_data().split()))
            ver_num[B[i][1]] +=1
            hol_num[B[i][0]] +=1

        ver_max = np.max(np.array(ver_num))
        hol_max = np.max(np.array(hol_num))

        max_columns = []
        max_rows = []

        for i in range(1,W+1):
            if ver_max == ver_num[i]:
                max_columns.append(i)
        for i in range(1,H+1):
            if hol_max == hol_num[i]:
                max_rows.append(i)

        b_cross = 0
        for b in B:
            if ver_num[b[1]] == ver_max and hol_num[b[0]] == hol_max:
                b_cross += 1
        if b_cross == len(max_columns) * len(max_rows):
            result = ver_max + hol_max - 1
        else:
            result = ver_max + hol_max
        print(str(result))

solver = Solver(REL_PATH)
solver.exec_resolve()

Submission status: Accepted

Solution code:
import sys;input=sys.stdin.readline
H, W, M = map(int, input().split())
dx = [0]*H
dy = [0]*W
HH = set()
for _ in range(M):
    x, y = map(int, input().split())
    dx[x-1] += 1
    dy[y-1] += 1
    HH.add((x-1, y-1))
mx=max(dx)
my=max(dy)
mxs = set()
mys = set()
for i in range(H):
    if dx[i] == mx:
        mxs.add(i)
for i in range(W):
    if dy[i] == my:
        mys.add(i)
#print(mxs, mys, mx, my)
for x in mxs:
    for y in mys:
        if (x, y) not in HH:
#            print(x, y)
            print(mx+my)
            sys.exit()
print(mx+my-1)

Submission status: Accepted

Solution code:
from sys import exit, stdin

# import copy
from collections import deque, Counter

# import numpy as np
input = stdin.readline

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

col = Counter()
row = Counter()
D = Counter()
for i in range(M):
    h, w = input().split()
    row.update([h])
    col.update([w])
    D.update([h + w])

rtop = row.most_common(1)[0]
ctop = col.most_common(1)[0]


for c in col.most_common():
    if c[1] < ctop[1]:
        break
    else:
        if D[rtop[0] + c[0]] == 0:
            ans = rtop[1] + c[1]
            break
        else:
            ans = rtop[1] + c[1] - 1

for r in row.most_common():
    if r[1] < rtop[1]:
        break
    else:
        if D[r[0] + ctop[0]] == 0:
            ans2 = r[1] + ctop[1]
            break
        else:
            ans2 = r[1] + c[1] - 1

print(max(ans, ans2))

Submission status: Accepted

Solution code:
import sys
h,w,m = map(int, input().split())
h_t = [0 for _ in range(h)]
w_t = [0 for _ in range(w)]
t = []
for _ in range(m):
  hi,wi = map(int, input().split())
  h_t[hi-1] += 1
  w_t[wi-1] += 1
  t.append([hi-1,wi-1])
hmax = max(h_t)
wmax = max(w_t)
hmaxi = [i1 for i1 in range(h) if h_t[i1] == hmax]
wmaxi = [i2 for i2 in range(w) if w_t[i2] == wmax]
c = len(hmaxi)*len(wmaxi)
d = 0
for ht,wt in t:
  if h_t[ht] == hmax:
    if w_t[wt] == wmax:
      d += 1
if c ==d:
  print(hmax + wmax-1)
else:
  print(hmax + wmax)

Submission status: Accepted

Solution code:
import collections 

h,w,m=map(int,input().split())
x=[]
y=[]
t=[]

for _ in range(m):
  a=list(map(int,input().split()))
  x.append(a[0])
  y.append(a[1])
  t.append(a)
  
X=collections.Counter(x)
Y=collections.Counter(y)

va,co=zip(*Y.most_common())
va1,co1=zip(*X.most_common())
X1=co1[0]
Y1=co[0]
chx=0
chy=0

for i in va:
  if Y[i]==Y1:
    chy+=1
    
for j in va1:
  if X[j]==X1:
    chx+=1


ans=0
for r in t:
  h=r[0]
  g=r[1]
  if X[h]==co1[0] and Y[g]==co[0]:
    ans+=1
    
if ans<(chx*chy):
  print(X1+Y1)
  
else:
  print(X1+Y1-1)

Submission status: Accepted

Solution code:
h,w,m=map(int,input().split())
hh=[[0,i]for i in range(h)]
ww=[[0,i]for i in range(w)]
hw=set()
for _ in range(m):
  a,b=map(int,input().split())
  a-=1
  b-=1
  hh[a][0]+=1
  ww[b][0]+=1
  hw.add((a,b))
ww.sort(reverse=1)
s=ww[0][0]
w=[]
for x,j in ww:
  if x==s:w.append(j)
hh.sort(reverse=1)
ans=0
t=max(hh)[0]
for j in w:
  for x,i in hh:
    if x!=t:break
    if (i,j) in hw:
      ans=s+x-1
    else:
      exit(print(s+x))
print(ans)