結果

問題 No.1637 Easy Tree Query
ユーザー sangonana
提出日時 2021-08-06 21:31:52
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 1,016 ms / 2,000 ms
コード長 437 bytes
コンパイル時間 218 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 46,848 KB
最終ジャッジ日時 2024-09-17 03:42:42
合計ジャッジ時間 21,712 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**6)

n,q=map(int,input().split())
node=[[]for i in range(n)]
for i in range(n-1):
	a,b=map(int,input().split())
	node[a-1].append(b-1)
	node[b-1].append(a-1)

cnt=[0]*n
visited=[0]*n
visited[0]=1
def dfs(x):
	now=1
	for y in node[x]:
		if visited[y]==0:
			visited[y]=1
			now+=dfs(y)
	cnt[x]=now
	return now

dfs(0)

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