結果

問題 No.1637 Easy Tree Query
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2021-08-06 23:31:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 566 ms / 2,000 ms
コード長 644 bytes
コンパイル時間 999 ms
コンパイル使用メモリ 81,460 KB
実行使用メモリ 96,836 KB
最終ジャッジ日時 2023-10-17 05:25:09
合計ジャッジ時間 13,756 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,372 KB
testcase_01 AC 41 ms
55,372 KB
testcase_02 AC 469 ms
96,552 KB
testcase_03 AC 42 ms
55,396 KB
testcase_04 AC 227 ms
87,496 KB
testcase_05 AC 391 ms
83,936 KB
testcase_06 AC 293 ms
81,240 KB
testcase_07 AC 204 ms
77,324 KB
testcase_08 AC 202 ms
84,304 KB
testcase_09 AC 364 ms
91,100 KB
testcase_10 AC 242 ms
80,648 KB
testcase_11 AC 461 ms
93,728 KB
testcase_12 AC 426 ms
89,180 KB
testcase_13 AC 169 ms
80,556 KB
testcase_14 AC 301 ms
91,380 KB
testcase_15 AC 455 ms
93,568 KB
testcase_16 AC 404 ms
96,824 KB
testcase_17 AC 204 ms
80,584 KB
testcase_18 AC 377 ms
83,936 KB
testcase_19 AC 376 ms
84,628 KB
testcase_20 AC 433 ms
88,092 KB
testcase_21 AC 275 ms
87,240 KB
testcase_22 AC 472 ms
96,836 KB
testcase_23 AC 196 ms
81,504 KB
testcase_24 AC 257 ms
86,716 KB
testcase_25 AC 387 ms
83,120 KB
testcase_26 AC 443 ms
90,000 KB
testcase_27 AC 566 ms
93,668 KB
testcase_28 AC 313 ms
82,188 KB
testcase_29 AC 398 ms
87,544 KB
testcase_30 AC 213 ms
87,540 KB
testcase_31 AC 292 ms
76,864 KB
testcase_32 AC 242 ms
83,128 KB
testcase_33 AC 366 ms
80,712 KB
testcase_34 AC 169 ms
95,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
n,Q = map(int,input().split())
e = [[] for i in range(n)]
for _ in range(n-1):
    a,b = [int(x)-1 for x in input().split()]
    e[a].append(b)
    e[b].append(a)

size = [1]*n

q = deque([0])
dis = [n]*n
dis[0] = 0
topo = []
while q:
    now = q.pop()
    topo.append(now)
    for nex in e[now]:
        if dis[nex] > dis[now]+1:
            q.append(nex)
            dis[nex] = dis[now]+1

for now in topo[::-1]:
    for nex in e[now]:
        if dis[nex] > dis[now]:
            size[now] += size[nex]

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