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: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    h,w,m = LI()
    hw = LIR(m)
    # h, w, m = [3 * 10 ** 5] * 3
    # hw = [(i, i) for i in range(3 * 10 ** 5)]
    dh = defaultdict(int)
    dh1 = defaultdict(int)
    dw = defaultdict(int)
    dw1 = defaultdict(int)
    ma = defaultdict(int)
    for hi, wi in hw:
        dh[hi] += 1
        dw[wi] += 1
        ma[(hi, wi)] = 1
    maxh = max(dh.values())
    maxw = max(dw.values())
    for key, value in dh.items():
        if value == maxh:
            dh1[key] = maxh
    for key, value in dw.items():
        if value == maxw:
            dw1[key] = maxw
    for hi, wi in hw:
        if hi in dh1:
            dh1[hi] -= 1
        if wi in dw1:
            dw1[wi] -= 1
    if maxh == max(dh1.values()):
        print(maxh + maxw)
    else:
        if maxw == max(dw1.values()):
            print(maxh + maxw)
        else:
            print(maxh + maxw - 1)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
from collections import deque
H , W , M = map(int,input().split())
G1 = [0]*H
G2 = [0]*W
h = [0]*M
w = [0]*M
for i in range(M):
  h[i] , w[i] = map(int,input().split())
  temp = h[i] - 1
  G1[temp] += 1
  temp = w[i] - 1
  G2[temp] += 1
G3 = [[h[i] - 1 , w[i] - 1] for i in range(M)]
M1 = max(G1)
M2 = max(G2)
M1L = deque()
M2L = deque()
for i in range(H):
  if G1[i] == M1:
    M1L.appendleft(i)
for j in range(W):
  if G2[j] == M2:
    M2L.appendleft(j)
c = 0
f = len(M1L) * len(M2L)
for X in G3:
  if X[0] in M1L and X[1] in M2L:
    c += 1
for i in M1L:
  for j in M2L:
    if [i,j] not in G3:
      c = 1
      break
  if c ==1:
    break
if c == 2:
  print(M1 + M2)
else:
  print(M1 + M2 - 1)

Submission status: Wrong Answer

Solution code:
H,W,M = list(map(int, input().split()))
s = []
Hlist = [0 for i in range(H)]
Wlist = [0 for i in range(W)]
for i in range(M):
  s.append(list(map(int, input().split())))
  Hlist[s[i][0]-1] += 1
  Wlist[s[i][1]-1] += 1
  
Hmax = max(Hlist)
Wmax = max(Wlist)
Hmaxindex = [i for i, v in enumerate(Hlist) if v == Hmax]
Wmaxindex = [i for i, v in enumerate(Wlist) if v == Wmax]
print(Hmax + Wmax)

Submission status: Wrong Answer

Solution code:
import numpy as np

H, W, M = [int(n) for n in input().split()]
row_sums = np.zeros(H)
col_sums = np.zeros(W)
coords = []
for i in range(M):
    h, w = [int(n) for n in input().split()]
    row_sums[h-1] += 1
    col_sums[w-1] += 1
    coords.append((h-1, w-1))
coords = set(coords)

col_indexes = np.argwhere(col_sums==col_sums.max()).flatten().tolist()
row_indexes = np.argwhere(row_sums==row_sums.max()).flatten().tolist()

print('col indexes:', col_indexes)
print('row_indexes:', row_indexes)
# print('col_sums:', col_sums)
# print('row_sums:', row_sums)

# intersection_without_bomb = False
# for row_idx in row_indexes:
#     for col_idx in col_indexes:
#         if (row_idx+1, col_idx+1) not in coords:
#             # print('ri, ci : ', row_idx, col_idx)
#             intersection_without_bomb = True
#             break
#     if intersection_without_bomb:
#         break

from itertools import product
# print('coords:', coords)
indexes = set(list(product(row_indexes, col_indexes)))
# print('indexes: ', indexes)
# print('intersection: ', coords.intersection(set(list(product(row_indexes, col_indexes)))))
# print('dif: ', indexes - coords)
if len(indexes-coords) > 0:
    print(int(col_sums.max() + row_sums.max()) - 1)
else:
    print(int(col_sums.max() + row_sums.max()))

# if intersection_without_bomb:
#     print(int(col_sums.max() + row_sums.max()))
# else:
#     print(int(col_sums.max() + row_sums.max()) - 1)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
from operator import itemgetter

h, w, m = (int(x) for x in input().split())
H = [0] * h
W = [0] * w
HW = []
for _ in range(m):
    h_, w_ = (int(x) - 1 for x in input().split())
    HW.append((h_, w_))
    H[h_] += 1
    W[w_] += 1
hmax = max(H)
wmax = max(W)
hindex = []
windex = []
ans = hmax + wmax - 1

HW.sort(key=itemgetter(0, 1))
for y in range(h):
    if H[y] == hmax:
        hindex.append(y)
for x in range(w):
    if W[x] == wmax:
        windex.append(x)

for y in hindex:
    for x in windex:
        index = bisect_left(HW, (y, x))
        if index < len(HW) and HW[index] != (y, x):
            ans = hmax + wmax

print(ans)

Submission status: Wrong Answer

Solution code:
H,W,M = [int(x) for x in input().split()]
hw = []
hNum = [0]*H
wNum = [0]*W

for i in range(M):
    h,w = [int(x) for x in input().split()]
    hw.append([h,w])
    hNum[h-1]+=1
    wNum[w-1]+=1

hmax = max(hNum)
hMaxList = []
wmax = max(wNum)
wMaxList = []

for i in range(H):
    if(hmax == hNum[i]):
        hMaxList.append(i)
for i in range(W):
    if(wmax == wNum[i]):
        wMaxList.append(i)

ans = wmax + hmax -1
if((M <= H and hmax ==  1) or (M <= W and wmax ==  1 )):
    ans += 1
flag = False
for i in hMaxList:
    if(not i in wMaxList):
        flag = True
        

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

Submission status: Wrong Answer

Solution code:
H,W,M=map(int,input().split())
Hs=[0]*H
Ws=[0]*W

B=[]
for i in range(M):
    h,w=map(int,input().split())
    B+=[[h-1,w-1]]
    Hs[h-1]+=1
    Ws[w-1]+=1

Hm=max(Hs)
Wm=max(Ws)
Hi=[]
for i in range(H):
    if Hs[i]==Hm:
        Hi+=[i]

Wi=[]
for i in range(W):
    if Ws[i]==Wm:
        Wi+=[i]

result=Hm+Wm-1
for i in Hi:
    for j in Wi:
        if not([i,j] in B):
            result+=1
            break
print(result)

Submission status: Wrong Answer

Solution code:
H, W, M = list(map(int, input().split()))
HW = set()
hd = {}
wd = {}
for i in range(M):
	h, w = tuple(map(int, input().split()))
	HW.add((h, w))
	if h in hd:
		hd[h] += 1
	else:
		hd[h] = 1
	if w in wd:
		wd[w] += 1
	else:
		wd[w] = 1

hm = [kv[0] for kv in hd.items() if kv[1] == max(hd.values())]
wm = [kv[0] for kv in wd.items() if kv[1] == max(wd.values())]
ans = 0
for h in hm:
	for w in wm:
		if (h, w) in HW:
			ans = max(ans, (hd[h] + wd[w] + 1))
		else:
			ans = max(ans, (hd[h] + wd[w]))
print(ans)

Submission status: Wrong Answer

Solution code:
H, W, M = map(int, input().split())
Hdst = [0]*H
Wdst = [0]*W
DST = [[0] * 2 for i in range(M)]
for i in range(M):
    hi, wi = map(int, input().split())
    Hdst[hi-1] += 1
    Wdst[wi-1] += 1
    DST[i] = [hi-1, wi-1]

Hdstmax = max(Hdst)
Wdstmax = max(Wdst)
Hbomb = [i for i, x in enumerate(Hdst) if x == Hdstmax]
Wbomb = [i for i, x in enumerate(Wdst) if x == Wdstmax]

tmp = M
for i in range(M):
    if (DST[i][0] not in Hbomb) or (DST[i][1] not in Wbomb):
        tmp -= 1

if len(Hbomb)*len(Wbomb) == tmp:
    print(Hdstmax + Wdstmax)
else:
    print(Hdstmax + Wdstmax - 1)

Submission status: Wrong Answer

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

from collections import defaultdict
counter_h = defaultdict(int)
counter_w = defaultdict(int)

for i in range(M):
    h[i],w[i] = map(int, input().split())
    counter_h[h[i]] += 1
    counter_w[w[i]] += 1
    
MAX_h = max(counter_h.values())
h_list = [k for k, v in counter_h.items() if v == MAX_h]
MAX_w = max(counter_w.values())


import itertools

ans = MAX_h+MAX_w

if len(h_list)<=3*10**5:
    X = h_list


    check = 0


    for i in range(M):
            if h[i] in X:
                check += 1

    if len(X)==check:
        ans -= 1
    
print(ans)

Submission status: Wrong Answer

Solution code:
h,w,m = map(int,input().split())
hli = [0]*h
wli = [0]*w
li = []
for i in range(m):
    a,b = map(int,input().split())
    li.append([a,b])
    hli[a-1] += 1
    wli[b-1] += 1
a = max(hli)
b = max(wli)
ans1 = hli.index(a) + 1
ans2 = wli.index(b) + 1
hli2 = []
wli2 = []
if m == 1:
    print(1)
    exit()
for i in range(h):
    if hli[i] == a:
        hli2.append(i)
for i in range(w):
    if wli[i] == b:
        wli2.append(i)
for i in range(len(hli2)):
    for j in range(len(wli2)):
        if [hli2[i],wli2[j]] not in li:
            print(a + b)
            exit()
print(a + b - 1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
# 幅優先探索（行きがけ）
import collections
import sys
import copy
import re
import math
import itertools


def I(): return int(sys.stdin.readline().rstrip())
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())


def main():
    H, W, M = LI()

    HList = [0]*H
    WList = [0]*W
    graph1 = [0]*H
    graph2 = [0]*W
    for m in range(M):
        h, w = LI()
        HList[h-1] += 1
        WList[w-1] += 1
        graph1[h-1] += 1
        graph2[w-1] += 1

    Hmax = max(HList)
    Wmax = max(WList)

    HIndex = [i for i, v in enumerate(HList) if v == Hmax]
    WIndex = [i for i, v in enumerate(WList) if v == Wmax]

    ans = Hmax + Wmax
    for h in HIndex:
        for w in WIndex:
            if graph1[h] == 0 and graph2[w]:
                print(ans)
                return

    print(ans-1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import bisect
from collections import defaultdict

H, W, M = map(int, input().split())
horizon_set = defaultdict(set)
vertical_set = defaultdict(set)
horizon = defaultdict(int)
vertical = defaultdict(int)
bombs = set()
for _ in range(M):
    y, x = map(int, input().split())
    y -= 1
    x -= 1
    horizon_set[y].add(x)
    vertical_set[x].add(y)
    horizon[y] += 1
    vertical[x] += 1
    bombs.add((x, y))

yn = sorted(horizon.items(), key=lambda yn: yn[::-1], reverse=True)
xn = sorted(vertical.items(), key=lambda xn: xn[::-1], reverse=False)
xnn = [n for x, n in xn]
ans = 0
y, ny = yn[0]
for j in range(len(xn)):
    x, nx = xn[j]
    n = nx + ny
    if (x, y) in bombs:
        n -= 1
    ans = max(ans, n)

for i in range(1, len(yn)):
    y, ny = yn[i]
    if ny + xnn[0] <= ans:
        break

    jm = bisect.bisect_left(xnn, ans - ny)
    for j in range(jm, len(xn)):
        x, nx = xn[j]
        n = nx + ny
        if (x, y) in bombs:
            n -= 1
        ans = max(ans, n)
print(ans)

Submission status: Wrong Answer

Solution code:
H,W,M=map(int,input().split())
mine=[list(map(int,input().split()))for i in range(M)]
mine_h=[0]*H
mine_w=[0]*W
ans=0
for i in range(M):
    mine_h[mine[i][0]-1]+=1
max_h=max(mine_h)
for i in mine_h:
    tmp=0
    if i ==max_h:
        mine_w=[0]*W
        for j in range(M):
            if mine[j][0]-1!=i:
                mine_w[mine[j][1]-1]+=1
        tmp=max_h+max(mine_w)
        if tmp>ans:
            ans=tmp
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy
h,w,m=map(int,input().split())
h_arr=[0]*h
w_arr=[0]*w
record = set()
for i in range(m):
    tmp_h, tmp_w = map(int,input().split())
    record.add((tmp_h,tmp_w))
    h_arr[tmp_h-1] += 1
    w_arr[tmp_w-1] += 1
#最大の列と行の交差点に爆破対象があるかどうかがわからない?
max_h = max(h_arr)
max_w = max(w_arr)
max_h_indexes = [i for i, x in enumerate(h_arr) if x == max_h]
max_w_indexes = [i for i, x in enumerate(w_arr) if x == max_w]
ans = max_h + max_w

print(ans)

Submission status: Wrong Answer

Solution code:
import random
import numpy as np
H,W,M=map(int,input().split())
hw=[]
xcount=dict()
xmax=0
ans=0
for i in range(M):
    hw.append(list(map(int,input().split())))
    xcount.setdefault(hw[i][0],0)
    xcount[hw[i][0]]+=1
    if xcount[hw[i][0]]>xmax:
        xmax=xcount[hw[i][0]]
        xmaxindex=hw[i][0]
ans+=xcount[xmaxindex]
ycount=dict()
ycount_copy=dict()
ymax=0
for i in range(M):
    if hw[i][0]==xmaxindex:
        ycount_copy.setdefault(hw[i][1],0)
        ycount_copy[hw[i][1]]+=1
        continue
    ycount_copy.setdefault(hw[i][1],0)
    ycount_copy[hw[i][1]]+=1
    ycount.setdefault(hw[i][1],0)
    ycount[hw[i][1]]+=1
    if ycount[hw[i][1]]>ymax:
        ymax=ycount[hw[i][1]]
        ymaxindex=hw[i][1]
try:
    if ([xmaxindex,ymaxindex] in hw):
        if random.randint(1,4)==1:
            ans+=1
    ans+=ycount[ymaxindex]
except:
    pass
print(ans)

Submission status: Wrong Answer

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

h_list = [0]*h
w_list = [0]*w
baku = []

for i in range(m):
  h1,w1 = map(int, input().split())
  h_list[h1-1] += 1 
  w_list[w1-1] += 1 
  baku.append([])
  baku[i].append(h1)
  baku[i].append(w1)

hh = max(h_list)
ww = max(w_list)
hh_ind = h_list.index(hh) + 1
ww_ind = w_list.index(ww) + 1
ma = hh + ww
for i in range(m):
  if baku[i][0] == hh_ind and  baku[i][1] == ww_ind:
    ma -= 1 
  
  
print(ma)

Submission status: Wrong Answer

Solution code:
import collections
h,w,m=map(int,input().split())
H=[]
W=[]
S=[]
for _ in range(m):
  a,b=map(int,input().split())
  H.append(a)
  S.append([a,b])
  
HC=collections.Counter(H)
commonHC=HC.most_common()[0][0]
nHC=HC.most_common()[0][1]
for i in range(m):
  if S[i][0]!=commonHC:
    W.append(S[i][1])
if W!=[]:
  WC=collections.Counter(W)
  nWC=WC.most_common()[0][1]
else:
  nWC=0
print(nHC+nWC)

Submission status: Wrong Answer

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

hw = np.zeros(
  (M, 2),np.uint8
)
for _ in range(M):
  h, w = map(int, input().split())
  hw[_][0] = h
  hw[_][1] = w
  
#print(hw, hw[:, 0])
c_h = Counter(hw[:, 0]).most_common()
c_w = Counter(hw[:, 1]).most_common()

max_ch = c_h[0][1]
max_ch_lst = []
for _c_h in c_h:
  if _c_h[1] == max_ch:
    max_ch_lst.append(_c_h[0])
  else:
    break
    
max_cw = c_w[0][1]
max_cw_lst = []
flg = 1
for _c_w in c_w:
  if _c_w[1] == max_cw:
    #max_cw_lst.append(_c_w[0])
    for k in max_ch_lst:
      if [k, _c_w[0]] not in hw:
        flg = 0
  else:
    break
    
print(max_cw + max_ch - flg)

Submission status: Wrong Answer

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

H, W, count = map(int, input().split())
'''h = []
w = []
ans = 0

for i in range(count):
  h_n, w_n = map(int, input().split())
  h.append(h_n)
  w.append(w_n)
  
h_count = collections.Counter(h)
w_count = collections.Counter(w)

h_bomb, h_most_count = zip(*h_count.most_common())
w_bomb, w_most_count = zip(*w_count.most_common()[0][1])
'''
def index_Multi(List,liter):
    #Listはリスト本体・literは検索したい文字
    index_L = []
    for val in range(0,len(List)):
        if liter == List[val]:
            index_L.append(val)
    return index_L

def serch(count, H, W):
  h = []
  w = []
  num = []
  for i in range(count):
    h_n, w_n = map(int, input().split())
    h.append(h_n)
    w.append(w_n)
  for j in range(H):
    line_count = h.count(j + 1)
    print("line_count:"+str(line_count))
    h_index = index_Multi(h, j+1)
    h_num = len(h_index)
    for k in range(W):
      row_count = w.count(k + 1)
      print("row_count:"+str(row_count))
      w_index = index_Multi(w, k+1)
      w_num = len(w_index)
      sum_ans = line_count + row_count
      if h_index == w_index:
        sum_ans -= 1
      num.append(sum_ans)
  print(max(num))
serch(count, H, W)

Submission status: Wrong Answer

Solution code:
H, W, M = map(int, input().split())
hlst = [0]*(H+10)
wlst = [0]*(W+10)
hwlst = []

for i in range(M):
    h, w = map(int, input().split())
    hlst[h] += 1
    wlst[w] += 1
    hwlst.append([h, w])

h = max(hlst)
w = max(wlst)
hmax = [i for i in range(H+1) if hlst[i] == h]
wmax = [i for i in range(W+1) if wlst[i] == w]
ans = h + w -1


for i in hmax:
    for j in wmax:
        if [i, j]  not in hwlst:
            ans += 1
            break

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
H, W, M = map(int, input().split())
lis =[]
Hlis = [0]*H
Wlis = [0]*W
for _ in range(M):
    Ch, Cm = map(int, input().split())
    Hlis[Ch-1] += 1
    Wlis[Cm-1] += 1
    lis.append([Ch, Cm])

Hmax = max(Hlis)
Hidx = [i+1 for i, v in enumerate(Hlis) if v == Hmax]
Wmax = max(Wlis)
Widx = [i+1 for i, v in enumerate(Wlis) if v == Wmax]

if len(Hidx)>10 and len(Widx) >10:
    print(Hmax + Wmax)
    sys.exit()


for i in range(len(Hidx)):
    for j in range(len(Widx)):
        if [Hidx[i], Widx[j]] not in lis:
            print(Hmax+Wmax)
            sys.exit()
else:
    print(Hmax + Wmax -1)

Submission status: Wrong Answer

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

cnt_dict_h = {}
cnt_dict_w = {}
bomb_list = []

for i in range(M):
    h, w = [int(i)-1 for i in input().split()]
    bomb_list.append((h,w))
    cnt_dict_h.setdefault(h, 0)
    cnt_dict_h[h] += 1
    cnt_dict_w.setdefault(w, 0)
    cnt_dict_w[w] += 1

max_h_kv_list = [kv for kv in cnt_dict_h.items() if kv[1] == max(cnt_dict_h.values())]
max_w_kv_list = [kv for kv in cnt_dict_w.items() if kv[1] == max(cnt_dict_w.values())]

bomb_num = max_h_kv_list[0][1] + max_w_kv_list[0][1]


flag = False
for bomb in bomb_list:
    if bomb[0] not in max_h_kv_list and bomb[1] not in max_w_kv_list:
        flag = True
        break

if flag:
    bomb_num = bomb_num - 1

print(bomb_num)

Submission status: Wrong Answer

Solution code:
# for #!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase


# region fastio

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")

# endregion
class union_find:
    def __init__(self, n):
        self.n = n
        self.rank = [0]*n
        self.parent = [int(j) for j in range(n)]
    
    def union(self,i,j):
        i = self.find(i)
        j = self.find(j)
        if self.rank[i] == self.rank[j]:
            self.parent[i] = j
            self.rank[j] += 1

        elif self.rank[i] > self.rank[j]:
            self.parent[j] = i
        else:
            self.parent[i] = j

    def find(self, i):
        temp = i
        if self.parent[temp] != temp:
            self.parent[temp] = self.find(self.parent[temp])
        return self.parent[temp]
from math import log2, ceil
from collections import deque, Counter as CC
def main():
# Enter your code here. Read input from STDIN. Print output to STDOUT
    # for t in range(int(input())):
        
        # n = int(input())
        # n,x,t = [int(j) for j in input().split()]
# )
        h,w,m = [int(j) for j in input().split()]
        r = [0 for i in range(h)]
        c =  [0 for i in range(w)]
        d = dict()
        for i in range(m):
            u,v = [int(j) for j in input().split()]
            r[u-1]+=1
            c[v-1]+=1
            d[(u,v)]=1
        r.sort(reverse=True)
        c.sort(reverse=True)
        ans = 0
        for i in range(h):
            for j in range(w):
                if(r[i]!=r[0]):
                    break
                if(c[j]!=c[0]):
                    break
                if((r[i],c[i]) not in d):
                    ans = r[0]+c[0]
                    break
                else:
                    ans = r[0]+c[0]-1
        print(ans)



        # b = [int(j) for j in input().split()]

            # s = n-1

        # s = input()




        # a = list(a)
        # print(a)
        # for i in a:



        



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import collections
h,w ,m = map(int, raw_input().split())
cx, cy  = collections.Counter(), collections.Counter()
for _ in range(m):
	x,y = map(int, raw_input().split())
	cx[x] += 1
	cy[y] += 1
q= max(cx.values()) + max(cy.values())
print q - 1

Submission status: Wrong Answer

Solution code:
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007

#+++++

def main():
	#a = int(input())
	h, w, m = tin()
	#s = input()
	hl=[0]*h
	wl=[0]*w
	for _ in range(m):
		hi,wi=tin()
		hl[hi-1]+=1
		wl[wi-1]+=1
		
	return max(hl)+max(wl)
		
		
	
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Wrong Answer

Solution code:

import collections
a = input().split()
M = int(a[2])
hidari = []
migi = []
c = []

for i in range(M):
  b = input().split()
  hidari.append(b[0])
  migi.append(b[1])
  c.append(b)

countleft = collections.Counter(hidari)
countright = collections.Counter(migi)
leftmax = countleft.most_common()[0][1]
rightmax = countright.most_common()[0][1]
tempmax = int(leftmax + rightmax)

#leftkeys = [k for k, v in countleft.items() if v == leftmax]
#rightkeys = [k for k, v in countright.items() if v == rightmax]
leftkeys = (list(countleft.keys())[list(countleft.values()).index(leftmax)])
rightkeys = (list(countright.keys())[list(countright.values()).index(rightmax)])

leftkeyN = int(len(leftkeys))
rightkeyN = int(len(rightkeys))
flag = int(0)
cnt = int(0)
#これで左右の最頻回数はOK
#左右の最頻奴も探した
#ここから最頻同志になってるやつを探す

for T in range(M):
    if c[T][0] in leftkeys and c[T][1] in rightkeys:
        cnt = cnt + 1
    else:
        pass

if cnt == int(leftkeyN * rightkeyN):
    print(int(tempmax - 1))
else:
    print(int(tempmax))

Submission status: Wrong Answer

Solution code:
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 list(hmaps):
        for w in list(wmaps):
            if (h, w) not in maps:
                aru = False
                break
    if aru:
        print(ans - 2)
    else:
        print(ans + 1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import random
h,w,m = map(int,input().split())
lis = [list(map(int,input().split())) for i in range(m)]
exist = set()
tate = [[0,i] for i in range(h)]
yoko = [[0,i] for i in range(w)]

for y,x in lis:
    exist.add(format(y).zfill(6)+format(x).zfill(6))
    tate[y-1][0] += 1
    yoko[x-1][0] += 1

tate.sort(key=lambda x:x[0],reverse=True)
yoko.sort(key=lambda x:x[0],reverse=True)

ans = tate[0][0]+yoko[0][0]-1
if h == 1 or w == 1:
    print(m)
    exit()

  # print(tate)
  # print(yoko)

a = [tate[0]]
b = [yoko[0]]
for i in range(h-1):
    if tate[i][0] <= tate[i+1][0]+1:
        a.append(tate[i+1])
    else:
        break
for i in range(w-1):
    if yoko[i][0] <= yoko[i+1][0]+1:
        b.append(yoko[i+1])
    else:
        break
a = random.sample(a,len(a))
b = random.sample(b,len(b))

i = 0
while i < len(a) and i <= 1800:
    j = 0
    while j < len(b) and j <= 1800:
        ans = max(ans,a[i][0]+b[j][0]-bool(format(a[i][1]+1).zfill(6)+format(b[j][1]+1).zfill(6) in exist))
        j += 1
    i += 1
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

dh=defaultdict(int)
dw=defaultdict(int)
nomap=[]

for _ in range(M):
    h,w=map(int,input().split())
    dh[h]+=1
    dw[w]+=1
    nomap.append([h,w])

goal=0

i=max(dh, key=dh.get)
j=max(dw, key=dw.get)
if [i,j] not in nomap:
    if dh[i]+dw[j]>goal:
        goal=dh[i]+dw[j]
else:
    if dh[i]+dw[j]-1>goal:
        goal=dh[i]+dw[j]-1

print(goal)

Submission status: Wrong Answer

Solution code:
import numpy as np

# print('input >>')
H, W, M = map(int,(input().split()))
bombs = []
for _ in range(M):
    bombs.append(tuple(map(int,(input().split()))))

grid = [[0] * W for _ in range(H)]

for bomb in bombs:
    h = bomb[0] - 1
    w = bomb[1] - 1
    grid[h][w] = 1

grid = np.array(grid)

hsum = np.sum(grid, axis=0)
wsum = np.sum(grid, axis=1)

hmaxi = np.argmax(hsum)
hmax = np.argmax(hsum)

# print(hsum)
# print(wsum)

# print('-----output-----')
print(max(hsum) + max(wsum) - 1)

Submission status: Wrong Answer

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


def main():
    H,W,M = list(map(int, sys.stdin.readline().split()))
    #hw_list = [ list(map(int, sys.stdin.readline().split())) for _ in range(M) ]

    val_h = [None]*M
    val_w = [None]*M
    #print("val_h",val_h)
    #print("val_w",val_w)
    bomb_addr = set()

    for i in range(M):
        val_h[i], val_w[i] = list(map(int, sys.stdin.readline().split()))
        bomb_addr.add( (val_h[i], val_w[i]) )
    
    #print("val_h",val_h)
    #print("val_w",val_w)
    #print("bomb_addr",bomb_addr)

    cnt_h = Counter(val_h)
    cnt_w = Counter(val_w)
    #print("cnt_h",cnt_h)
    #print("cnt_w",cnt_w)

    max_h, _ = cnt_h.most_common()[0]
    max_w, _ = cnt_w.most_common()[0]
    #print("max_h",max_h)
    #print("max_w",max_w)

    h_arr = [ k for k,v in cnt_h.most_common() if k == max_h ]
    w_arr = [ k for k,v in cnt_w.most_common() if k == max_w ]
    #print("h_arr",h_arr)
    #print("w_arr",w_arr)


    blast = 0

    for r in h_arr:
        for c in w_arr:
            tmp_blast = cnt_h[r] + cnt_w[c]
            #print("r",r,"c",c,"tmp_blast",tmp_blast)

            if (r,c) in bomb_addr:
                tmp_blast -= 1
                #print("r",r,"c",c,"tmp_blast minus",tmp_blast)
            
            blast = max(blast, tmp_blast)


    #print("blast",blast)
    print(blast)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
h,w,m=map(int,input().split())
nh=[[_] for _ in range(w)] #各wに対するhの値
nw=[[_] for _ in range(h)]
num=[]
for i in range(m):
    h2,w2=map(int,input().split())
    nh[w2-1].append(i)
    nw[h2-1].append(i)
    num.append([h2,w2])
for i in range(w):
    nh[i].append(len(nh[i])-1)
for i in range(h):
    nw[i].append(len(nw[i])-1)
nh.sort(reverse=True,key=lambda x: x[-1])
nw.sort(reverse=True,key=lambda x: x[-1])
ans=0
yn2=0
for i in range(w):
    yn=nh[0][-1]+nw[0][-1]
    for j in range(h):
        num10=nh[i][-1]+nw[j][-1]
        if num10+1<yn:
            yn2=1
            break
        if [nw[j][0]+1,nh[i][0]+1] in num:
            num10-=1
        ans=max(ans,num10)
    if yn2==1:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
import collections

h,w,m=map(int,input().split())
mat=[map(int, input().split()) for _ in range(m)]
x,y=[list(i) for i in zip(*mat)]

xc = collections.Counter(x)
yc = collections.Counter(y)

xhigh = xc.most_common()[0]
yhigh = yc.most_common()[0]

ans = xhigh[1]+yhigh[1]

for j in range(len(x)):

    if x[j] == xhigh[0] and y[j] == yhigh[0]:
    
        ans-=1
        break

print(ans)