結果
| 問題 |
No.1637 Easy Tree Query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-08-17 10:45:09 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 501 ms / 2,000 ms |
| コード長 | 495 bytes |
| コンパイル時間 | 291 ms |
| コンパイル使用メモリ | 82,780 KB |
| 実行使用メモリ | 212,260 KB |
| 最終ジャッジ日時 | 2024-10-10 07:27:06 |
| 合計ジャッジ時間 | 13,789 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 33 |
ソースコード
import sys
sys.setrecursionlimit(10**9)
N,Q=map(int,input().split())
G=[[] for _ in range(N)]
for i in range(N-1):
a,b=map(lambda x:int(x)-1,input().split())
G[a].append(b)
G[b].append(a)
seen=[False]*N
subtree_size=[1]*N
def dfs(G,v,p=-1):
seen[v]=True
for nextv in G[v]:
if nextv!=p:
dfs(G,nextv,v)
for c in G[v]:
if c!=p:
subtree_size[v]+=subtree_size[c]
dfs(G,0)
ans=0
for i in range(Q):
p,x=map(int,input().split())
ans+=x*subtree_size[p-1]
print(ans)