結果

問題 No.1637 Easy Tree Query
ユーザー harurunharurun
提出日時 2021-06-19 00:47:22
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 440 ms / 2,000 ms
コード長 2,057 bytes
コンパイル時間 154 ms
コンパイル使用メモリ 12,256 KB
実行使用メモリ 63,620 KB
最終ジャッジ日時 2023-10-17 04:33:00
合計ジャッジ時間 10,391 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 24 ms
10,400 KB
testcase_01 AC 24 ms
10,404 KB
testcase_02 AC 388 ms
51,060 KB
testcase_03 AC 27 ms
10,400 KB
testcase_04 AC 147 ms
28,164 KB
testcase_05 AC 271 ms
36,488 KB
testcase_06 AC 189 ms
27,976 KB
testcase_07 AC 95 ms
16,476 KB
testcase_08 AC 122 ms
24,504 KB
testcase_09 AC 290 ms
41,952 KB
testcase_10 AC 139 ms
23,408 KB
testcase_11 AC 363 ms
49,028 KB
testcase_12 AC 326 ms
44,084 KB
testcase_13 AC 88 ms
18,884 KB
testcase_14 AC 229 ms
37,768 KB
testcase_15 AC 365 ms
48,536 KB
testcase_16 AC 325 ms
46,888 KB
testcase_17 AC 112 ms
21,020 KB
testcase_18 AC 261 ms
35,256 KB
testcase_19 AC 263 ms
36,532 KB
testcase_20 AC 338 ms
44,696 KB
testcase_21 AC 199 ms
32,816 KB
testcase_22 AC 420 ms
53,952 KB
testcase_23 AC 113 ms
22,312 KB
testcase_24 AC 184 ms
31,228 KB
testcase_25 AC 267 ms
35,916 KB
testcase_26 AC 355 ms
46,112 KB
testcase_27 AC 440 ms
54,764 KB
testcase_28 AC 228 ms
30,232 KB
testcase_29 AC 293 ms
39,812 KB
testcase_30 AC 166 ms
29,828 KB
testcase_31 AC 178 ms
23,808 KB
testcase_32 AC 157 ms
26,820 KB
testcase_33 AC 238 ms
32,268 KB
testcase_34 AC 248 ms
63,620 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)
  assert(2<=N<=10**5)
  assert(1<=Q<=10**5)
  d=[[]for _ in[0]*N]
  for _ in[0]*(N-1):
    a,b=pin(2)
    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)
    assert(1<=p<=N)
    assert(1<=x<=10**8)
    ans+=cnt[p-1]*x
    print(ans)
  is_input=False
  try:
    pin(0)
    is_input=True
  except:
    pass
  if is_input:
    raise Exception
  return

main()
0