結果
問題 |
No.1637 Easy Tree Query
|
ユーザー |
![]() |
提出日時 | 2021-08-18 23:45:25 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 353 ms / 2,000 ms |
コード長 | 508 bytes |
コンパイル時間 | 166 ms |
コンパイル使用メモリ | 82,348 KB |
実行使用メモリ | 177,024 KB |
最終ジャッジ日時 | 2024-10-11 22:26:33 |
合計ジャッジ時間 | 10,877 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 33 |
ソースコード
import sys sys.setrecursionlimit(100000) N,Q = map(int,input().split()) AB = [list(map(int,input().split())) for _ in range(N-1)] PX = [list(map(int,input().split())) for _ in range(Q)] E = [[] for _ in range(N)] for ab in AB: a,b = ab E[a-1].append(b-1) E[b-1].append(a-1) dp = [0] * N def dfs(x,p): cnt = 1 for i in E[x]: if i != p: cnt += dfs(i,x) dp[x] = cnt return cnt dfs(0,-1) ans = 0 for px in PX: p,x = px ans += dp[p-1] * x print(ans)