結果

問題 No.1637 Easy Tree Query
ユーザー tobusakana
提出日時 2022-11-27 14:13:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 266 ms / 2,000 ms
コード長 641 bytes
コンパイル時間 446 ms
コンパイル使用メモリ 82,244 KB
実行使用メモリ 163,464 KB
最終ジャッジ日時 2024-10-04 02:17:43
合計ジャッジ時間 8,499 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline = sys.stdin.readline

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

N,Q = map(int,readline().split())
G = [[] for i in range(N)]
for _ in range(N - 1):
    a,b = map(int,readline().split())
    G[a - 1].append(b - 1)
    G[b - 1].append(a - 1)

cnt = [0] * N
def dfs(x, p): # 自分含む子の数を返す
    res = 1
    for child in G[x]:
        if child == p:
            continue
        res += dfs(child, x)
    cnt[x] = res
    return res
    
dfs(0, -1)

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