結果

問題 No.1637 Easy Tree Query
ユーザー harurun
提出日時 2021-06-19 00:49:40
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 1,099 ms / 2,000 ms
コード長 2,280 bytes
コンパイル時間 84 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 49,704 KB
最終ジャッジ日時 2024-09-17 03:17:47
合計ジャッジ時間 22,574 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

# 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にすること。
# """
from sys import setrecursionlimit
from collections import deque
setrecursionlimit(10000000)
def main():
  #N,Q=pin(2)
  N,Q=map(int,input().split())
  assert(2<=N<=10**5)
  assert(1<=Q<=10**5)
  d=[[]for _ in[0]*N]
  for _ in[0]*(N-1):
    #a,b=pin(2)
    a,b=map(int,input().split())
    assert(1<=a<=N)
    assert(1<=b<=N)
    d[a-1].append(b-1)
    d[b-1].append(a-1)
  cnt=[1]*N
  B=[0]*N
  def f(i):
    B[i]=1
    for j in d[i]:
      if B[j]==0:
        f(j)
        cnt[i]+=cnt[j]
    return 
  f(0)
  ans=0
  for _ in[0]*Q:
    #p,x=pin(2)
    p,x=map(int,input().split())
    assert(1<=p<=N)
    assert(1<=x<=10**8)
    ans+=cnt[p-1]*x
    print(ans)
  # is_input=False
  # try:
  #   input()
  #   is_input=True
  # except:
  #   pass
  # if is_input:
  #   raise Exception
  # return

main()
0