結果

問題 No.1637 Easy Tree Query
ユーザー lloyz
提出日時 2021-12-24 23:34:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 423 ms / 2,000 ms
コード長 565 bytes
コンパイル時間 226 ms
コンパイル使用メモリ 82,040 KB
実行使用メモリ 178,428 KB
最終ジャッジ日時 2024-09-19 22:52:04
合計ジャッジ時間 11,999 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import defaultdict
sys.setrecursionlimit(10**6)

n, q = map(int, input().split())
edge = defaultdict(list)
for _ in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    edge[a].append(b)
    edge[b].append(a)

V = [0 for _ in range(n)]
def dfs(curr, prev):
    V[curr] += 1
    for np in edge[curr]:
        if np == prev:
            continue
        dfs(np, curr)
        V[curr] += V[np]
dfs(0, -1)

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