結果

問題 No.1637 Easy Tree Query
ユーザー 河野竜也
提出日時 2021-09-12 20:50:35
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 1,146 ms / 2,000 ms
コード長 431 bytes
コンパイル時間 119 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 46,976 KB
最終ジャッジ日時 2024-06-24 12:35:19
合計ジャッジ時間 23,924 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10 ** 9)
n,q=map(int,input().split())
g=[[] for i in range(n)]
for i in range(n-1):
  a,b=map(int,input().split())
  a-=1
  b-=1
  g[a].append(b)
  g[b].append(a)
s=[0]*n
def dfs(now,before):
  s[now]=1
  for next in g[now]:
    if next==before:
      continue
    dfs(next,now)
    s[now]+=s[next]
dfs(0,-1)
ans=0
for _ in range(q):
  p,x=map(int,input().split())
  p-=1
  ans+=s[p]*x
  print(ans)
0