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:
import collections

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

h_counter = collections.Counter(h)
w_counter = collections.Counter(w)
h_common0 = h_counter.most_common()[0][0]
w_common0 = w_counter.most_common()[0][0]
h_common1 = h_counter.most_common()[-1][1]
w_common1 = w_counter.most_common()[-1][1]

ans=0
for i in range(M):
    if h[i] == h_common0 or w[i] == w_common0:
        ans = ans +1

if h_common1 == M/2 and w_common1 == M/2:
    ans = int(M/2)


print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import bisect
import itertools
import collections
import fractions
import heapq
import math
from operator import mul
from functools import reduce
from functools import lru_cache


def solve():
    readline = sys.stdin.buffer.readline
    input = readline
    mod = 10 ** 9 + 7
    H, W, M = map(int, input().split())
    Hlist = [0] * H
    Wlist = [0] * W
    hw = []

    for _ in range(M):
        h, w = map(int, input().split())
        Hlist[h-1] += 1
        Wlist[w-1] += 1
        hw.append([h, w])


    hmax = [i for i, x in enumerate(Hlist) if x == max(Hlist)]
    wmax = [i for i, x in enumerate(Wlist) if x == max(Wlist)]
    hmaxset = set(hmax)
    wmaxset = set(wmax)

    flag = False
    for hh in hw:
        h = hh[0] - 1
        w = hh[1] - 1
        if not (h in hmaxset and w in wmaxset):
            flag = True
            break


    if flag:
        print(Hlist[h] + Wlist[w])
    else:
        print(Hlist[h] + Wlist[w] - 1)





if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
H,W,M = map(int,input().split())
Hs = []
Ws = []
Hs_count = []
Ws_count = []
zahyo = []
for i in range(M):
    h,w = map(int,input().split())
    zahyo.append(f"{h} {w}")
    Hs.append(h)
    Ws.append(w)
for i in range(1,H+1):
    Hs_count.append(Hs.count(i))
for i in range(1,W+1):
    Ws_count.append(Ws.count(i))
maxIndex_H = [i for i, x in enumerate(Hs_count) if x == max(Hs_count)]
maxIndex_W = [i for i, x in enumerate(Ws_count) if x == max(Ws_count)]

n = len(maxIndex_H)*len(maxIndex_W)


if n <= 0:
    print(max(Hs_count)+max(Ws_count)-1)
else:
    print(max(Hs_count)+max(Ws_count))

Submission status: Wrong Answer

Solution code:
import numpy as np
h, w, m = map(int, input().split())
M = [list(map(int, input().split())) for i in range(m)]

H=[0]*h
W=[0]*w
for mine in M:
    a=mine[0]-1
    b=mine[1]-1
    H[a]+=1
    W[b]+=1

    print(mine)

maxh=max(H)
print('maxh ok')
maxw=max(W)
print('maxw ok')

maxhindex = []
maxwindex = []

for i in range(h):
    if H[i]==maxh:
        maxhindex.append(i)
print('maxhindex ok')
for i in range(w):
    if W[i]==maxw:
        maxwindex.append(i)
print('maxwindex ok')

end=0

for mine in M:
    if mine[0] in maxhindex and mine[1] in maxwindex:
        end=1

ans=maxh+maxw
if end==0:
    ans-=1
print(ans)

Submission status: Wrong Answer

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

bomb_list = [list(map(int, input().split())) for i in range(m)]

for o in range(m):
    for p in range(2):
        bomb_list[o][p] -= 1

h_count_list = [0 for j in range(h)]
w_count_list = [0 for k in range(w)]

max_h_count = 0
max_w_count = 0

for l in range(m):
    a = bomb_list[l][0]
    h_count_list[a] += 1

max_h_count = max(h_count_list)
x = h_count_list.index(max_h_count)
    
for n in range(m):
    b = bomb_list[n][1]
    w_count_list[b] += 1

max_w_count = max(w_count_list)
y = w_count_list.index(max_w_count)

if [x, y] in bomb_list: 
    print(max_w_count + max_h_count - 1)
else:
    print(max_w_count + max_h_count)

Submission status: Wrong Answer

Solution code:
import math
import numpy as np

h,w,m = map(int,input().split())
target = [[None]*2 for i in range(m)]

for i in range(m):
    target[i][0],target[i][1] =map(int,input().split())

target = np.array(target)
target-=1

tate = [0]*h
tate = np.array(tate)
yoko = [0]*w
yoko = np.array(yoko)

for i in range(m):
    tate[target[i][0]]+=1
    yoko[target[i][1]]+=1
ans = max(yoko)+max(tate)

print(ans-1)
"""
print(np.where (yoko == max(yoko)))
for i in np.where(yoko == max(yoko)):
    for j in np.where(tate == max(tate)):
        print([j,i])
        if [j,i] in target:
            print('yahho')

"""

Submission status: Wrong Answer

Solution code:
h,w,m = map(int,input().split())
H = [0]*(h+1)
W = [0]*(w+1)
s = set([])
for i in range(m):
    a,b = map(int,input().split())
    s.add((a,b))

    H[a] += 1
    W[b] += 1

x = [0]*(h+1)
y = [0]*(w+1)
    
for i in range(h+1):
    x[i] = [i,H[i]]
for i in range(w+1):
    y[i] = [i,W[i]]

x.sort(key= lambda val : val[1],reverse=True)
y.sort(key= lambda val : val[1],reverse=True)

ma = x[0][1]
mb = y[0][1]
i,j = 0,0
sw = 0
while 1:
    if (x[i][0],y[j][0]) in s:
        pass
    else:
        sw = 1
        break
    if x[i][1] + y[j][1] != ma+mb:
        break
    
    if x[i+1][1]!= ma:
        j += 1
    else:
        i += 1
print(ma+mb+sw-1)

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 wi in dw1 and hi in dh1:
            dw1[wi] -= 1
    if maxw == max(dw1.values()):
        print(maxh + maxw)
        return
    for hi, wi in hw:
        if wi in dw1 and hi in dh1:
            dh1[hi] -= 1
    if maxh == max(dh1.values()):
        print(maxh + maxw)
        return
    
    print(maxh + maxw - 1)
    return


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

Submission status: Wrong Answer

Solution code:
import collections

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

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

#print(h)
#print(w)


# 重複要素のmaxを求める
hmax = collections.Counter(h).most_common()[0]

# 重複要素のmaxを求める
wmax = collections.Counter(w).most_common()[0]

#print(hraw)
#print(wraw)

bomb = False
for i in range(len(h)):
    if h[i]==hmax[0] and w[i]==wmax[0]:
        bomb = True
        break

if bomb:
    if len(collections.Counter(h).most_common())>1:
        hmax = collections.Counter(h).most_common()[1]
    elif len(collections.Counter(w).most_common())>1:
        wmax = collections.Counter(w).most_common()[1]
    bomb = False
    for i in range(len(h)):
        if h[i]==hmax[0] and w[i]==wmax[0]:
            bomb = True
            break

        
cnt = hmax[1] + wmax[1]
if bomb:
    cnt -= 1

print(cnt)

Submission status: Wrong Answer

Solution code:
class SegmentTree:
    def __init__(self, init_val, segfunc, ide_ele):
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.tree = [ide_ele] * 2 * self.num
        # 配列の値を葉にセット
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        k += self.num
        self.tree[k] = x
        while k > 1:
            self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
            k >>= 1

    def query(self, l, r):
        res = self.ide_ele

        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.tree[r - 1])
            l >>= 1
            r >>= 1
        return res

import sys,random

input=sys.stdin.readline

H,W,M=map(int,input().split())
bomb=[[] for i in range(H)]
row=[0 for i in range(H)]
column=[0 for j in range(W)]

for _ in range(M):
    h,w=map(int,input().split())
    #h,w=random.randint(1,H),random.randint(1,W)
    row[h-1]+=1
    column[w-1]+=1
    bomb[h-1].append(w-1)

for i in range(H):
    bomb[i].sort()

Seg=SegmentTree(column,segfunc=max,ide_ele=-1)

ans=0
for i in range(H):
    for j in range(len(bomb[i])-1):
        L,R=bomb[i][j],bomb[i][j+1]
        tmp=row[i]+max(column[L],column[R])-1
        ans=max(ans,tmp)
        L+=1
        if L==R:
            continue
        tmp=row[i]+Seg.query(L,R)
        ans=max(ans,tmp)
    if bomb[i] and bomb[i][0]!=0:
        L,R=0,bomb[i][0]
        tmp=row[i]+Seg.query(L,R)
        ans=max(ans,tmp)
    if bomb[i] and bomb[i][-1]!=W-1:
        L,R=bomb[i][-1]+1,W
        tmp=row[i]+Seg.query(L,R)
        ans=max(ans,tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
import sys
 
h, w, m = map(int, sys.stdin.readline().split())
hl = []
wl = []
 
for _ in range(m):
    hi, wi = map(int, sys.stdin.readline().split())
    hl.append(hi)
    wl.append(wi)
 
hc = Counter(hl).most_common()
wc = Counter(wl).most_common()
 
seth = []
setw = []

for s in range(len(hc)):
    if hc[0][1] == hc[s][1]:
        seth.append(hc[s][0])
    else:
        break

for s in range(len(wc)):
    if wc[0][1] == wc[s][1]:
        setw.append(wc[s][0])
    else:
        break

 
ans = 0

zp = list(zip(hl, wl))
for hei in seth:
    for wei in setw:
        print(hei, wei)
        if not (hei, wei) in zp:
            print(hc[0][1] + wc[0][1])
            exit()


print(hc[0][1] + wc[0][1]-1)

Submission status: Wrong Answer

Solution code:
from collections import Counter

H,W,M = map(int,input().split())
h = []
w = []

for _ in range(M):
    tmp_h,tmp_w = map(int,input().split())
    h.append(tmp_h)
    w.append(tmp_w)

H_count = Counter(h).most_common()
W_count = Counter(w).most_common()

print(H_count[0][1] + W_count[0][1])

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)>5 and len(Widx) >5:
    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 = map(int,input().split())
h = list()
w = list()
for i in range(M):
    hh,ww = map(int,input().split())
    h.append(hh)
    w.append(ww)
ch = 0
hh = list()
for i in range(H):
    a = h.count(i+1)
    if ch < a:
        hh = list()
        hh.append(i+1)
        ch = a
    elif ch == a:
        hh.append(i+1)
cw = 0
ww = list()
for i in range(W):
    a = w.count(i+1)
    if cw < a:
        ww = list()
        ww.append(i+1)
        cw = a
    elif cw == a:
        ww.append(i+1)
ans = 0
for i in hh:
    www = list()
    cnt = 0
    for ii in range(M):
        if i == h[ii]:
            www.append(w[ii])
    for ii in www:
        if (ii+1) in ww:
            cnt = -1
        cnt += w.count(ii+1)
        #print('{}:{}:{}:{}'.format(cnt,i,ww,ii))
        ans = max(ans,cnt)
        cnt = 0
print(ans+ch)

Submission status: Wrong Answer

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

X = [0] * H
Y = [0] * W

B = []
h = H
while h:
    h -= 1
    B.append([])
    
A = []
C = {}
m = M
while m:
    m -= 1
    x, y = map(int,input().split())
    x, y = x -1, y -1
    A.append((x, y))
    B[x].append(y)
    C[(x, y)] = True
    X[x] += 1
    Y[y] += 1

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

xm = max(X)
ym = max(Y)
xms = my_index_multi(X, xm)
yms = my_index_multi(Y, ym)

ans = False
for a, b in A:
    if not C[(a, b)]: ans = True

cnt = xm + ym         
print(cnt if ans else cnt - 1)

Submission status: Wrong Answer

Solution code:
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))

h,w,m=nii()

h_b=[[0,i] for i in range(h)]
w_b=[[0,i] for i in range(w)]

l=[]
for i in range(m):
  h,w=nii()
  h-=1
  w-=1
  h_b[h][0]+=1
  w_b[w][0]+=1
  l.append([h,w])

h_s=sorted(h_b,key=lambda x:x[0],reverse=True)
w_s=sorted(w_b,key=lambda x:x[0],reverse=True)

ans=0
for i in range(min(10,len(h_s))):
  for j in range(min(10,len(w_s))):
    t_ans=h_b[i][0]+w_b[j][0]
    num=l.count([i,j])
    t_ans-=num
    ans=max(ans,t_ans)

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(hli[i])
for i in range(w):
    if wli[i] == b:
        wli2.append(wli[i])
for i in range(len(hli2)):
    for j in range(len(wli2)):
        if [hli2[i]+1,wli2[j]+1] not in li:
            print(a + b)
            exit()
print(a + b - 1)

Submission status: Wrong Answer

Solution code:
H, W, M = map(int, input().split())
bomb = list()
for i in range(M):
    hh, ww = map(int, input().split())
    bomb.append((hh - 1, ww - 1))

row = dict()
col = dict()

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

for b in bomb:
    xx, yy = b
    row[xx] = row[xx] + 1
    col[yy] = col[yy] + 1

row = sorted(row.items(), key=lambda x: x[1], reverse=True)
col = sorted(col.items(), key=lambda x: x[1], reverse=True)


hx, hy = row[0][0], col[0][0]
cx, cy = row[0][1], col[0][1]
nx, ny = 0, 0

while (hx, hy) in bomb:
    if nx + 1 < H and row[nx + 1][1] == row[nx][1]:
        nx += 1
    elif nx + 1 >= H and ny + 1 < W:
        nx = 0
        ny += 1
    else:
        break
    hx, hy = row[nx][0], col[ny][0]

if (hx, hy) in bomb:
    print(row[0][1] + col[0][1] - 1)
else:
    print(row[0][1] + col[0][1])

Submission status: Wrong Answer

Solution code:
def E():
    h,w,m = list(map(int, input().split()))
    h_l = []
    w_l = []
    bomb = []
    for _ in range(m):
        h_t,w_t = list(map(int, input().split()))
        h_l.append(h_t)
        w_l.append(w_t)
        bomb.append(str(h_t)+":"+str(w_t))
    import collections
    c_h = collections.Counter(h_l).most_common()
    c_w = collections.Counter(w_l).most_common()
    max_h = c_h[0][1]
    max_w = c_w[0][1]
    flg = -1
    for i in range(len(c_h)):
        if c_h[i][1] != max_h:
            break
        for j in range(len(c_w)):
            if c_w[j][1] != max_w:
                break
            if str(c_h[i][0]+1)+":"+str(c_w[j][0]+1) in bomb:
                flg = 0
                break
    print(max_h + max_w + flg)
E()

Submission status: Wrong Answer

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

maxr, maxc = Counter(), Counter()

mass = set()

for i in range(M):
    h, w = map(int, input().split())
    mass.add((h-1, w-1))
    maxr[h-1]+=1
    maxc[w-1]+=1

res = 0

r = maxr.most_common()[0][0]
c = maxc.most_common()[0][0]

res = max(res, maxr[r]+maxc[c] - int((r, c) in mass))
print(res)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# Input#
# n = int(input())
h, w, m = map(int, input().split())
tar_list = []

h_count = [0] * (h + 1)
w_count = [0] * (w + 1)

h_max = 0
w_max = 0

h_maxlist = []
w_maxlist = []

ans = 0
ans_tmp = 0

for i in range(m):
    h_in, w_in = map(int, input().split())

    h_count[h_in] += 1
    if h_count[h_in] > h_max:
        h_max = h_count[h_in]
        h_maxlist = []
    if h_count[h_in] == h_max:
        h_maxlist.append(h_in)
        tar_list.append((h_in, w_in))

    w_count[w_in] += 1
    if w_count[w_in] > w_max:
        w_max = w_count[w_in]
        w_maxlist = []
    if w_count[w_in] == w_max:
        w_maxlist.append(w_in)
        tar_list.append((h_in, w_in))


for i in range(len(h_maxlist)):
    tar_h = h_maxlist[i]
    for j in range(len(w_maxlist)):
        tar_w = w_maxlist[j]
        if (tar_h, tar_w) in tar_list:
            ans = h_max + w_max - 1
        else:
            ans = h_max + w_max
            break


print("{}".format(ans))

Submission status: Wrong Answer

Solution code:
#n=int(input())
#n,m=map(int,input().split())
#a=list(map(int,input().split()))

h,w,m=map(int,input().split())
tate=[0]*(h+1)
yoko=[0]*(w+1)
#box=[[0]*(w+1) for i in range(h+1)]

for i in range(m):
    a, b = map(int, input().split())
    tate[a] += 1
    yoko[b] += 1
   # box[a][b] += 1
tmax = max(tate)
ymax = max(yoko)
tbox = [i for i, v in enumerate(tate) if v == tmax]
ybox = [i for i, v in enumerate(yoko) if v == ymax]

flag = True
#for i in tbox:
 #   for j in ybox:
  #      if box[i][j] == 0:
#            flag = False
#print(tmax,ymax,tbox,ybox,flag,box)
if flag:
    print(tmax + ymax - 1)
else:
    print(tmax + ymax)

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] == 0:
                print(ans)
                return

    print(ans-1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

cnt_h = [0] * h
cnt_w = [0] * w

for h1,w1 in hw:
    cnt_h[h1-1] += 1
    cnt_w[w1-1] += 1

lh = []
lw = []

max1 = 0

for i,e in enumerate(cnt_h):
    max1 = max(max1, e)
    if e == max1:
        lh.append((e,i))

max1 = 0
for i,e in enumerate(cnt_w):
    max1 = max(max1, e)
    if e == max1:
        lw.append((e,i))

lh.sort(reverse = True)
lw.sort(reverse = True)

mh = lh[0][0]
mw = lw[0][0]

idx_h = 0
idx_w = 0

cnt = 0
for i,j in lh:
    if i == mh:
        cnt += 1
    else:
        break

idx_h = cnt

cnt = 0
for i,j in lw:
    if i == mw:
        cnt += 1
    else:
        break

idx_w = cnt

ans = mh + mw


if idx_h * idx_w > m:
    print(ans)
    exit()

for i in range(idx_h):
    for j in range(idx_w):
        if (lh[i][1],lw[j][1]) in hw:
            print(ans)
            exit()

print(ans - 1)

"""
h_w = [[] for _ in range(h)]
w_h = [[] for _ in range(w)]

for h1,w1 in hw:
    h_w[h1-1].append(w1-1)
    w_h[w1-1].append(h1-1)

lh = deque([])
lw = deque([])


vh = [False] * h
vw = [False] * w

for i in range(h):
    if not vh[i]:
        lh.append(i)
        tmp = 0
        while True:
            while lh:
                l = lh.popleft()
                if not vh[l]:
                    vh[l] = True
                    for j in h_w[l]:
                        if not vw[j]:
                            lw.append(j)
                            tmp += 1
            print(lw)
            while lw:
                l = lw.popleft()
                if not vw[l]:
                    vw[l] = True
                    for j in w_h[l]:
                        if not vh[j]:
                            lh.append(j)
                            tmp += 1
            print(lh)
            if lh or lw:
                pass
            else:
                print(tmp)
                print(vh)
                print(vw)
                break
"""

Submission status: Wrong Answer

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

H = [0] * m
W = [0] * m
idx = 0
for h, w in HW:
    H[idx], W[idx] = h, w
    idx += 1
#print(H, w)

set_H = set(H)
set_W = set(W)
ans = max(len(set_H), len(set_W))
print(ans)

Submission status: Wrong Answer

Solution code:
H, W, M = map(int, input().split())
# 行を固定して考える
row = [0]*H
column = [0]*W
happend = set()
for _ in range(M):
    h, w = map(lambda x: int(x)-1, input().split())
    row[h] += 1
    column[w] += 1
    happend.add((h, w))
ans = 0
max_column_index = column.index(max(column))
max_column = max(column)

max_row_index = row.index(max(row))
max_row = max(row)
for i in range(H):
    if (i, max_column_index) in happend:
        ans = max(ans, row[i]+max_column)-1
    else:
        ans = max(ans, row[i]+max_column)


for j in range(W):
    # その行に爆弾を置くことを考えた時
    if (max_row_index, j) in happend:
        ans = max(ans, column[j]+max_row)-1
    else:
        ans = max(ans, column[j]+max_row)
print(ans)

Submission status: Wrong Answer

Solution code:
#import random

def putmx(lis):
  ret=[]
  mx=max(lis)
  for i in range(len(lis)):
    if mx==lis[i]:
      ret.append(i)
  return ret
  

h,w,m=map(int,input().split())
#inp=[(random.randrange(1,h+1),random.randrange(1,w+1)) for i in range(m)]
hb=[0]*h
wb=[0]*w
bmb=[]
for i in range(h):
  bmb.append({-1})
for i in range(m):
  hh,ww=map(int,input().split())
  hh-=1
  ww-=1
  bmb[hh].add(ww)
  hb[hh]+=1
  wb[ww]+=1

#hmx=[i for i, x in enumerate(hb) if x == max(hb)]
#wmx=[i for i, x in enumerate(wb) if x == max(wb)]
hmx=putmx(hb)
wmx=set(putmx(wb))
mhb=max(hb)
mwb=max(wb)
#print(hb,wb,hmx,wmx)
fl=False
for i in hmx:
  if len(bmb[i]&wmx)>1:
      print(mhb+mwb-1)
      fl=True
      break
  if fl:
    break
else:
  print(mhb+mwb)

Submission status: Wrong Answer

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

h = {}
w = {}
for i in range(M):
    hi = C[i][0]
    wi = C[i][1]
    h[hi] = h.get(hi, 0) + 1
    w[wi] = w.get(wi, 0) + 1

h = sorted(h.items(), key=lambda x:x[1], reverse=True)
w = sorted(w.items(), key=lambda x:x[1], reverse=True)


count = h[0][1] + w[0][1]
if [h[0][0], w[0][0]] in C:
    print(count - 1)
else:
    print(count)

Submission status: Wrong Answer

Solution code:
import collections
import itertools

H,W,M = map(int, input().split())
h=[]
w=[]
S=[]
maxh=[]
maxw=[]


for m in range(M):
    a,b = map(int, input().split())
    h.append(a)
    w.append(b)
    S.append([a,b])


lh=collections.Counter(h)
lw=collections.Counter(w)

maxh.append(lh.most_common()[0][0])
hmaxcount=lh.most_common()[0][1]

for n in range(1,len(lh)):
    if lh.most_common()[n][1]==hmaxcount:
        maxh.append(lh.most_common()[n][1])
    else:
        break


maxw.append(lw.most_common()[0][0])
wmaxcount=lw.most_common()[0][1]

sum= hmaxcount+wmaxcount

for n in range(1,len(lw)):
    if lw.most_common()[n][1]==wmaxcount:
        maxw.append(lw.most_common()[n][1])
    else:
        break

for an in list(itertools.product(maxh, maxw)):
    if list(an) not in S:
        print(sum)
        break
else:
    print(sum-1)

Submission status: Wrong Answer

Solution code:
H, W, M = map(int, input().split())
bomb = list()
for i in range(M):
    hh, ww = map(int, input().split())
    bomb.append((hh - 1, ww - 1))

row = dict()
col = dict()

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

for b in bomb:
    xx, yy = b
    row[xx] = row[xx] + 1
    col[yy] = col[yy] + 1

row = sorted(row.items(), key=lambda x: x[1], reverse=True)
col = sorted(col.items(), key=lambda x: x[1], reverse=True)


hx, hy = row[0][0], col[0][0]
cx, cy = row[0][1], col[0][1]
nx, ny = 0, 0

while (hx, hy) in bomb:
    if nx + 1 < H and row[nx + 1][1] == row[nx][1]:
        nx += 1
    elif nx + 1 >= H and ny + 1 < W:
        nx = 0
        ny += 1
    hx, hy = row[nx][0], col[ny][0]

if (hx, hy) in bomb:
    print(row[0][1] + col[0][1] - 1)
else:
    print(row[0][1] + col[0][1])

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[j]) 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()