結果

問題 No.1637 Easy Tree Query
コンテスト
ユーザー wgrape
提出日時 2023-11-01 15:07:00
言語 PyPy3
(7.3.23 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 364 ms / 2,000 ms
+ 719µs
コード長 538 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 261 ms
コンパイル使用メモリ 95,824 KB
実行使用メモリ 178,048 KB
最終ジャッジ日時 2026-09-04 06:43:13
合計ジャッジ時間 12,898 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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)
    
cnt = [0] * N # 自分を含む子の数
def dfs(v, p):
    c = 1
    for child in G[v]:
        if child == p:
            continue
        c += dfs(child, v)
    cnt[v] = c
    return c
    
dfs(0, -1)
        
ans = 0
for _ in range(Q):
    p,x = map(int,input().split())
    ans += cnt[p - 1] * x
    print(ans)
    
0