結果

問題 No.1637 Easy Tree Query
ユーザー harurunharurun
提出日時 2021-07-02 18:55:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 298 ms / 2,000 ms
コード長 2,056 bytes
コンパイル時間 271 ms
コンパイル使用メモリ 81,716 KB
実行使用メモリ 186,988 KB
最終ジャッジ日時 2023-10-17 04:34:23
合計ジャッジ時間 9,289 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,476 KB
testcase_01 AC 42 ms
55,476 KB
testcase_02 AC 230 ms
125,628 KB
testcase_03 AC 43 ms
55,476 KB
testcase_04 AC 171 ms
93,188 KB
testcase_05 AC 201 ms
111,092 KB
testcase_06 AC 162 ms
99,544 KB
testcase_07 AC 110 ms
85,580 KB
testcase_08 AC 149 ms
89,440 KB
testcase_09 AC 237 ms
111,728 KB
testcase_10 AC 145 ms
92,304 KB
testcase_11 AC 276 ms
120,596 KB
testcase_12 AC 249 ms
118,720 KB
testcase_13 AC 124 ms
84,704 KB
testcase_14 AC 225 ms
104,036 KB
testcase_15 AC 277 ms
119,956 KB
testcase_16 AC 273 ms
112,976 KB
testcase_17 AC 130 ms
87,604 KB
testcase_18 AC 193 ms
109,800 KB
testcase_19 AC 204 ms
110,476 KB
testcase_20 AC 246 ms
119,148 KB
testcase_21 AC 189 ms
100,916 KB
testcase_22 AC 298 ms
123,976 KB
testcase_23 AC 141 ms
88,804 KB
testcase_24 AC 185 ms
97,988 KB
testcase_25 AC 198 ms
110,796 KB
testcase_26 AC 258 ms
119,976 KB
testcase_27 AC 298 ms
127,660 KB
testcase_28 AC 170 ms
102,580 KB
testcase_29 AC 222 ms
111,584 KB
testcase_30 AC 176 ms
93,900 KB
testcase_31 AC 126 ms
99,436 KB
testcase_32 AC 162 ms
94,756 KB
testcase_33 AC 182 ms
108,620 KB
testcase_34 AC 289 ms
186,988 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