結果

問題 No.1703 Much Matching
ユーザー harurunharurun
提出日時 2021-08-26 18:42:12
言語 PyPy3
(7.3.15)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 3,617 bytes
コンパイル時間 264 ms
コンパイル使用メモリ 87,152 KB
実行使用メモリ 324,964 KB
最終ジャッジ日時 2023-09-30 09:23:42
合計ジャッジ時間 13,359 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,336 KB
testcase_01 AC 76 ms
71,428 KB
testcase_02 AC 76 ms
71,416 KB
testcase_03 AC 91 ms
75,936 KB
testcase_04 WA -
testcase_05 AC 98 ms
76,356 KB
testcase_06 AC 360 ms
147,836 KB
testcase_07 AC 111 ms
79,956 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 146 ms
87,704 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 95 ms
76,764 KB
testcase_15 WA -
testcase_16 AC 253 ms
119,204 KB
testcase_17 AC 278 ms
124,320 KB
testcase_18 AC 95 ms
76,652 KB
testcase_19 AC 486 ms
194,492 KB
testcase_20 AC 145 ms
89,636 KB
testcase_21 AC 554 ms
211,716 KB
testcase_22 WA -
testcase_23 AC 237 ms
111,840 KB
testcase_24 AC 106 ms
77,852 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 289 ms
128,152 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 1,022 ms
324,964 KB
testcase_37 AC 126 ms
83,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stderr
perr=lambda x:stderr.write(str(x)+"\n")
class INPUT:
  def __init__(self):
    self._l=open(0).read().split()
    self._length=len(self._l)
    self._index=0
    return

  def stream(self,k=1,f=int,f2=False):
    assert(-1<k)
    if self._length==self._index or self._length-self._index<k:
      raise Exception("There is no input!")
    elif f!=str:
      if k==0:
        ret=list(map(f,self._l[self._index:]))
        self._index=self._length
        return ret
      if k==1 and not f2:
        ret=f(self._l[self._index])
        self._index+=1
        return ret
      if k==1 and f2:
        ret=[f(self._l[self._index])]
        self._index+=1
        return ret
      ret=[]
      for _ in [0]*k:
        ret.append(f(self._l[self._index]))
        self._index+=1
      return ret
    else:
      if k==0:
        ret=list(self._l[self._index:])
        self._index=self._length
        return ret
      if k==1 and not f2:
        ret=self._l[self._index]
        self._index+=1
        return ret
      if k==1 and f2:
        ret=[self._l[self._index]]
        self._index+=1
        return ret
      ret=[]
      for _ in [0]*k:
        ret.append(self._l[self._index])
        self._index+=1
      return ret
pin=INPUT().stream

#pin(number[default:1],f[default:int],f2[default:False])
#if number==0 -> return left all
#listを変数で受け取るとき、必ずlistをTrueにすること。

class dsu:
  def __init__(self,n):
    assert n>0,"n must be Natural"
    self.n=n
    self.parents=[-1]*n
    return
  
  def find(self,x):
    assert x<self.n,"warning!! this is 0 index"
    if self.parents[x]<0:
      return x
    self.parents[x]=self.find(self.parents[x])
    return self.parents[x]
  
  def merge(self,x,y):
    assert x<self.n and y<self.n,"warning!! this is 0 index"
    x=self.find(x)
    y=self.find(y)
    if x==y:
      return
    if self.parents[x]>self.parents[y]:
      x,y=y,x
    self.parents[x]+=self.parents[y]
    self.parents[y]=x
    return
    
  def size(self,x):
    assert x<self.n,"warning!! this is 0 index"
    return -self.parents[self.find(x)]
    
  def same(self,x,y):
    assert x<self.n and y<self.n,"warning!! this is 0 index"
    return self.find(x)==self.find(y)
  
  def leader(self,x):
    assert x<self.n,"warning!! this is 0 index"
    if self.parents[x]<0:
      return x
    self.parents[x]=self.leader(self.parents[x])
    return self.parents[x]
  
  def groups(self):
    ld=[self.leader(i) for i in range(self.n)]
    res=[[] for _ in range(self.n)]
    for i in range(self.n):
      res[ld[i]].append(i)
    return list(filter(lambda a:a !=[],res))
    
def lcs(S,T):
  dp=[[0]*(len(T)+1)for _ in[0]*(len(S)+1)]
  for i in range(1,len(S)+1):
    for j in range(1,len(T)+1):
      if S[i-1]==T[j-1]:
        dp[i][j]=dp[i-1][j-1]+1
      else:
        dp[i][j]=max(dp[i-1][j],dp[i][j-1])
  length=dp[len(S)][len(T)]
  i=len(S)
  j=len(T)
  ans=[]
  while length>0:
    if S[i-1]==T[j-1]:
      ans.append(S[i-1])
      i-=1
      j-=1
      length-=1
    elif dp[i][j]==dp[i-1][j]:
      i-=1
    else:
      j-=1
  return len(ans[::-1])

def main():
  N,M,Q=pin(3)
  assert(1<=N<=1000)
  assert(1<=M<=1000)
  assert(1<=Q<=N*M)
  uf=dsu(N+M)
  validation_set=set()
  for _ in[0]*Q:
    a,b=pin(2)
    assert(1<=a<=N)
    assert(1<=b<=M)
    validation_set.add((a,b))
    uf.merge(a-1,N+b-1)
  assert(len(validation_set)==Q)
  A=list(range(0,N))
  B=list(range(N,N+M))
  g=uf.groups()
  for i in g:
    for j in i:
      if j<N:
        A[j]=i[0]
      else:
        B[j-N]=i[0]
  ans=lcs(A,B)
  print(ans)
  return

main()
0