結果

問題 No.1637 Easy Tree Query
ユーザー player
提出日時 2023-12-20 00:48:22
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 540 ms / 2,000 ms
コード長 547 bytes
コンパイル時間 150 ms
コンパイル使用メモリ 82,104 KB
実行使用メモリ 179,884 KB
最終ジャッジ日時 2024-09-27 09:04:54
合計ジャッジ時間 13,368 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

import pypyjit
import sys
sys.setrecursionlimit(10**6)
pypyjit.set_param('max_unroll_recursion=-1')



n,q = map(int,input().split())

edge = [[] for _ in range(n)]
for _ in range(n-1):
    a,b = map(int,input().split())
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)
    
cnt = [0]*n

def dfs(x,p):
    ret = 1
    for c in edge[x]:
        if c == p:
            continue
        ret += dfs(c,x)
    cnt[x] += ret
    return ret

dfs(0,-1)

ans = 0
for _ in range(q):
    p,x = map(int,input().split())
    ans += cnt[p-1]*x
    print(ans)
0