結果

問題 No.1928 Make a Binary Tree
ユーザー harurunharurun
提出日時 2021-12-15 21:32:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,237 ms / 3,000 ms
コード長 3,019 bytes
コンパイル時間 388 ms
コンパイル使用メモリ 82,428 KB
実行使用メモリ 417,552 KB
最終ジャッジ日時 2024-04-23 22:16:59
合計ジャッジ時間 53,427 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
52,736 KB
testcase_01 AC 34 ms
52,992 KB
testcase_02 AC 34 ms
53,120 KB
testcase_03 AC 33 ms
52,736 KB
testcase_04 AC 33 ms
52,992 KB
testcase_05 AC 35 ms
52,864 KB
testcase_06 AC 34 ms
53,376 KB
testcase_07 AC 35 ms
53,376 KB
testcase_08 AC 39 ms
60,032 KB
testcase_09 AC 40 ms
60,160 KB
testcase_10 AC 35 ms
53,248 KB
testcase_11 AC 36 ms
52,992 KB
testcase_12 AC 35 ms
52,992 KB
testcase_13 AC 37 ms
53,760 KB
testcase_14 AC 35 ms
53,248 KB
testcase_15 AC 187 ms
79,920 KB
testcase_16 AC 164 ms
79,488 KB
testcase_17 AC 152 ms
79,104 KB
testcase_18 AC 190 ms
79,728 KB
testcase_19 AC 83 ms
77,056 KB
testcase_20 AC 165 ms
79,576 KB
testcase_21 AC 131 ms
78,560 KB
testcase_22 AC 157 ms
79,360 KB
testcase_23 AC 138 ms
78,744 KB
testcase_24 AC 177 ms
80,036 KB
testcase_25 AC 187 ms
80,480 KB
testcase_26 AC 1,118 ms
119,476 KB
testcase_27 AC 709 ms
103,248 KB
testcase_28 AC 761 ms
104,704 KB
testcase_29 AC 1,284 ms
124,736 KB
testcase_30 AC 665 ms
101,356 KB
testcase_31 AC 872 ms
109,844 KB
testcase_32 AC 1,223 ms
123,128 KB
testcase_33 AC 762 ms
103,776 KB
testcase_34 AC 900 ms
112,428 KB
testcase_35 AC 1,308 ms
181,488 KB
testcase_36 AC 1,851 ms
300,480 KB
testcase_37 AC 998 ms
123,260 KB
testcase_38 AC 34 ms
53,916 KB
testcase_39 AC 1,976 ms
294,088 KB
testcase_40 AC 1,968 ms
294,004 KB
testcase_41 AC 2,008 ms
293,420 KB
testcase_42 AC 1,986 ms
295,008 KB
testcase_43 AC 1,965 ms
294,896 KB
testcase_44 AC 1,953 ms
294,768 KB
testcase_45 AC 1,991 ms
295,236 KB
testcase_46 AC 1,986 ms
295,424 KB
testcase_47 AC 1,967 ms
294,512 KB
testcase_48 AC 1,964 ms
293,840 KB
testcase_49 AC 2,237 ms
417,552 KB
testcase_50 AC 1,448 ms
126,192 KB
testcase_51 AC 1,414 ms
125,240 KB
testcase_52 AC 1,408 ms
125,284 KB
testcase_53 AC 1,391 ms
124,980 KB
testcase_54 AC 1,395 ms
125,820 KB
testcase_55 AC 1,777 ms
291,988 KB
testcase_56 AC 1,489 ms
127,492 KB
testcase_57 AC 1,470 ms
126,308 KB
testcase_58 AC 1,435 ms
127,276 KB
testcase_59 AC 768 ms
126,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(400000)
class SegTree:
  def __init__(self,op,e,v):
    self._op=op
    self._e=e
    
    if isinstance(v,int):
      v=[e]*v
    
    self._n=len(v)
    self._log=self.ceil2()
    self._size=1<<self._log
    self._d=[e]*(2*self._size)
    
    for i in range(self._n):
      self._d[self._size+i]=v[i]
    for i in range(self._size-1,0,-1):
      self._update(i)
    return
  
  def ceil2(self):
    x=0
    while (1<<x)<self._n:
      x+=1
    return x
    
  def set(self,p,x):
    p+=self._size
    self._d[p]=x
    for i in range(1,self._log+1):
      self._update(p>>i)
    return
    
  def get(self,p):
    return self._d[p+self._size]
    
  def prod(self,left,right):
    sml=self._e
    smr=self._e
    left+=self._size
    right+=self._size
    while left<right:
      if left&1:
        sml=self._op(sml,self._d[left])
        left+=1
      if right&1:
        right-=1
        smr=self._op(self._d[right],smr)
      left>>=1
      right>>=1
    return self._op(sml,smr)
    
  def all_prod(self):
    return self._d[1]
  
  def max_right(self,left,target):
    self.target=target
    if left==self._n:
      return self._n
    left+=self._size
    sm=self._e
    
    first=True
    while first or (left & -left)!=left:
      first=False
      while left%2==0:
        left>>=1
      if not self._f(self._op(sm,self._d[left])):
        while left<self._size:
          left*=2
          if self._f(self._op(sm,self._d[left])):
            sm=self._op(sm,self._d[left])
            left+=1
        return left-self._size
      sm=self._op(sm,self._d[left])
      left+=1
    return self._n
    
  def min_left(self,right,target):
    self.target=target
    if right==0:
      return 0
    
    right+=self._size
    sm=self._e
    
    first=True
    while first or (right&-right)!=right:
      first=False
      right-=1
      while right>1 and right%2:
        right>>=1
      if not self._f(self._op(self._d[right],sm)):
        while right<self._size:
          right=2*right+1
          if self._f(self._op(self._d[right],sm)):
            sm=self._op(self._d[right],sm)
            right-=1
        return right+1-self._size
      sm=self._op(self._d[right],sm)
    return 0
  
  def _update(self,k):
    self._d[k]=self._op(self._d[2*k],self._d[2*k+1])
    return

  def _f(self,u):
    return u<self.target


pin=sys.stdin.readline
def main():
  N=int(pin())
  Graph=[[]for i in range(N)]
  checked=[False]*N
  for i in range(N-1):
    x,y=map(int,pin().split())
    Graph[x-1].append(y-1)
    Graph[y-1].append(x-1)
  seg=SegTree(max,[-1,-1],N)
  searched=[0]
  def dfs(i):
    pre=searched[0]
    checked[i]=True
    for to in Graph[i]:
      if not checked[to]:
        cnt=dfs(to)
        seg.set(searched[0],[cnt,searched[0]])
        searched[0]+=1
    res=1
    for j in range(2):
      tmp=seg.prod(pre,searched[0])
      if tmp[0]==-1:
        break
      res+=tmp[0]
      seg.set(tmp[1],[-1,-1])
    return res
  ans=dfs(0)
  print(ans)
  return

main()
0