結果
| 問題 | 
                            No.1637 Easy Tree Query
                             | 
                    
| コンテスト | |
| ユーザー | 
                             wgrape
                         | 
                    
| 提出日時 | 2023-11-01 15:07:00 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 486 ms / 2,000 ms | 
| コード長 | 538 bytes | 
| コンパイル時間 | 360 ms | 
| コンパイル使用メモリ | 82,396 KB | 
| 実行使用メモリ | 176,896 KB | 
| 最終ジャッジ日時 | 2024-09-25 17:58:16 | 
| 合計ジャッジ時間 | 13,086 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 33 | 
ソースコード
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)
    
            
            
            
        
            
wgrape