結果

問題 No.1637 Easy Tree Query
ユーザー NatsubiSogan
提出日時 2021-08-06 21:27:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 477 ms / 2,000 ms
コード長 488 bytes
コンパイル時間 306 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 176,896 KB
最終ジャッジ日時 2024-09-17 03:36:12
合計ジャッジ時間 12,604 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10 ** 6)
n, q = map(int, input().split())
G = [[] for i in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    G[a - 1].append(b - 1)
    G[b - 1].append(a - 1)
dp = [0] * n
def dfs(prev, cur):
    res = 1
    for nex in G[cur]:
        if nex != prev:
            res += dfs(cur, nex)
    dp[cur] = res
    return res
ans = 0
dfs(-1, 0)
for _ in range(q):
    p, x = map(int, input().split())
    ans += x * dp[p - 1]
    print(ans)
0