結果

問題 No.1637 Easy Tree Query
ユーザー harurunharurun
提出日時 2021-06-19 00:49:40
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 963 ms / 2,000 ms
コード長 2,280 bytes
コンパイル時間 512 ms
コンパイル使用メモリ 11,916 KB
実行使用メモリ 49,380 KB
最終ジャッジ日時 2023-10-17 04:33:53
合計ジャッジ時間 20,592 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
10,228 KB
testcase_01 AC 28 ms
10,228 KB
testcase_02 AC 902 ms
28,992 KB
testcase_03 AC 27 ms
10,228 KB
testcase_04 AC 266 ms
19,488 KB
testcase_05 AC 687 ms
17,416 KB
testcase_06 AC 467 ms
15,420 KB
testcase_07 AC 246 ms
10,780 KB
testcase_08 AC 217 ms
17,392 KB
testcase_09 AC 598 ms
24,080 KB
testcase_10 AC 330 ms
14,236 KB
testcase_11 AC 812 ms
25,968 KB
testcase_12 AC 782 ms
23,044 KB
testcase_13 AC 159 ms
14,328 KB
testcase_14 AC 433 ms
24,204 KB
testcase_15 AC 779 ms
26,484 KB
testcase_16 AC 624 ms
27,936 KB
testcase_17 AC 236 ms
14,328 KB
testcase_18 AC 643 ms
17,408 KB
testcase_19 AC 649 ms
18,732 KB
testcase_20 AC 804 ms
22,120 KB
testcase_21 AC 441 ms
19,844 KB
testcase_22 AC 886 ms
28,864 KB
testcase_23 AC 240 ms
15,488 KB
testcase_24 AC 381 ms
19,868 KB
testcase_25 AC 697 ms
16,940 KB
testcase_26 AC 827 ms
23,296 KB
testcase_27 AC 963 ms
28,036 KB
testcase_28 AC 529 ms
16,512 KB
testcase_29 AC 642 ms
21,376 KB
testcase_30 AC 266 ms
20,808 KB
testcase_31 AC 533 ms
10,956 KB
testcase_32 AC 342 ms
16,764 KB
testcase_33 AC 642 ms
15,068 KB
testcase_34 AC 377 ms
49,380 KB
権限があれば一括ダウンロードができます

ソースコード

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