結果

問題 No.1928 Make a Binary Tree
ユーザー harurunharurun
提出日時 2021-12-15 12:01:22
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 3,386 bytes
コンパイル時間 522 ms
コンパイル使用メモリ 13,184 KB
実行使用メモリ 74,888 KB
最終ジャッジ日時 2024-04-23 22:08:49
合計ジャッジ時間 8,231 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
14,336 KB
testcase_01 AC 31 ms
14,208 KB
testcase_02 AC 29 ms
14,208 KB
testcase_03 AC 30 ms
14,232 KB
testcase_04 AC 29 ms
14,264 KB
testcase_05 AC 30 ms
14,208 KB
testcase_06 AC 28 ms
14,184 KB
testcase_07 AC 30 ms
14,340 KB
testcase_08 AC 29 ms
14,176 KB
testcase_09 AC 30 ms
14,200 KB
testcase_10 AC 30 ms
14,080 KB
testcase_11 AC 30 ms
14,252 KB
testcase_12 AC 30 ms
14,340 KB
testcase_13 AC 31 ms
14,208 KB
testcase_14 AC 29 ms
14,208 KB
testcase_15 AC 210 ms
17,280 KB
testcase_16 AC 155 ms
16,400 KB
testcase_17 AC 123 ms
15,744 KB
testcase_18 AC 202 ms
17,020 KB
testcase_19 AC 48 ms
14,396 KB
testcase_20 AC 130 ms
15,948 KB
testcase_21 AC 77 ms
15,100 KB
testcase_22 AC 147 ms
16,220 KB
testcase_23 AC 115 ms
15,616 KB
testcase_24 AC 189 ms
17,024 KB
testcase_25 AC 218 ms
17,256 KB
testcase_26 TLE -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

class Stack:
  def __init__(self,max_size=400010):
    self._stack=[-1]*max_size
    self._size=0
    return
  def push(self,x):
    self._stack[self._size]=x
    self._size+=1
    return
  def pop(self):
    self._size-=1
    return self._stack[self._size]
  def empty(self):
    return self._size==0
  
    
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

def main():
  N=int(input())
  Graph=[[]for i in range(N)]
  for i in range(N-1):
    x,y=map(int,input().split())
    Graph[x-1].append(y-1)
    Graph[y-1].append(x-1)
  seg=SegTree(max,[-1,-1],N)
  st=Stack()
  st.push(N)
  st.push(0)
  checked=[False]*N
  pre=[0]*N
  cnt=[1]*N
  searched=0
  while not st.empty():
    q=st.pop()
    if q<N:
      pre[q]=searched
      checked[q]=True
      for to in Graph[q]:
        if not checked[to]:
          st.push(to+N)
          st.push(to)
    else:
      for j in range(2):
        tmp=seg.prod(pre[q-N],searched)
        if tmp[0]==-1:
          break
        cnt[q-N]+=tmp[0]
        seg.set(tmp[1],[-1,-1])
      seg.set(searched,[cnt[q-N],searched])
      searched+=1
  print(cnt[0])
  return

main()
0