結果

問題 No.1928 Make a Binary Tree
ユーザー harurunharurun
提出日時 2021-12-09 10:27:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,754 ms / 3,000 ms
コード長 3,110 bytes
コンパイル時間 278 ms
コンパイル使用メモリ 82,700 KB
実行使用メモリ 142,932 KB
最終ジャッジ日時 2024-04-23 22:07:04
合計ジャッジ時間 56,282 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
55,552 KB
testcase_01 AC 43 ms
55,272 KB
testcase_02 AC 44 ms
56,284 KB
testcase_03 AC 46 ms
54,856 KB
testcase_04 AC 44 ms
54,812 KB
testcase_05 AC 43 ms
55,188 KB
testcase_06 AC 45 ms
55,992 KB
testcase_07 AC 45 ms
55,372 KB
testcase_08 AC 51 ms
62,804 KB
testcase_09 AC 52 ms
63,816 KB
testcase_10 AC 45 ms
57,000 KB
testcase_11 AC 44 ms
55,336 KB
testcase_12 AC 43 ms
54,800 KB
testcase_13 AC 45 ms
55,868 KB
testcase_14 AC 45 ms
55,720 KB
testcase_15 AC 270 ms
81,224 KB
testcase_16 AC 249 ms
81,180 KB
testcase_17 AC 217 ms
80,360 KB
testcase_18 AC 263 ms
81,468 KB
testcase_19 AC 119 ms
77,888 KB
testcase_20 AC 230 ms
80,292 KB
testcase_21 AC 168 ms
78,960 KB
testcase_22 AC 238 ms
81,204 KB
testcase_23 AC 209 ms
79,636 KB
testcase_24 AC 284 ms
81,672 KB
testcase_25 AC 276 ms
81,316 KB
testcase_26 AC 1,525 ms
122,676 KB
testcase_27 AC 989 ms
105,192 KB
testcase_28 AC 1,101 ms
108,640 KB
testcase_29 AC 1,668 ms
129,276 KB
testcase_30 AC 927 ms
102,884 KB
testcase_31 AC 1,299 ms
114,784 KB
testcase_32 AC 1,568 ms
127,600 KB
testcase_33 AC 998 ms
105,712 KB
testcase_34 AC 1,210 ms
114,064 KB
testcase_35 AC 1,535 ms
128,316 KB
testcase_36 AC 1,452 ms
142,932 KB
testcase_37 AC 1,208 ms
126,340 KB
testcase_38 AC 44 ms
55,260 KB
testcase_39 AC 1,704 ms
132,428 KB
testcase_40 AC 1,716 ms
132,704 KB
testcase_41 AC 1,740 ms
133,000 KB
testcase_42 AC 1,727 ms
132,996 KB
testcase_43 AC 1,754 ms
132,556 KB
testcase_44 AC 1,730 ms
132,024 KB
testcase_45 AC 1,665 ms
131,688 KB
testcase_46 AC 1,746 ms
133,344 KB
testcase_47 AC 1,725 ms
132,928 KB
testcase_48 AC 1,710 ms
133,012 KB
testcase_49 AC 1,445 ms
131,120 KB
testcase_50 AC 1,643 ms
126,580 KB
testcase_51 AC 1,639 ms
126,548 KB
testcase_52 AC 1,651 ms
126,448 KB
testcase_53 AC 1,651 ms
126,520 KB
testcase_54 AC 1,657 ms
127,016 KB
testcase_55 AC 1,344 ms
132,860 KB
testcase_56 AC 1,719 ms
129,904 KB
testcase_57 AC 1,711 ms
129,172 KB
testcase_58 AC 1,711 ms
129,564 KB
testcase_59 AC 1,062 ms
141,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

from collections import deque
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)
  que=deque()
  que.append(N)
  que.append(0)
  checked=[False]*N
  pre=[0]*N
  cnt=[1]*N
  searched=0
  while que:
    q=que.pop()
    if q<N:
      pre[q]=searched
      checked[q]=True
      for to in Graph[q]:
        if not checked[to]:
          que.append(to+N)
          que.append(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