結果

問題 No.1637 Easy Tree Query
ユーザー ああいいああいい
提出日時 2021-12-04 12:40:34
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 902 ms / 2,000 ms
コード長 1,060 bytes
コンパイル時間 330 ms
コンパイル使用メモリ 10,892 KB
実行使用メモリ 34,992 KB
最終ジャッジ日時 2023-09-20 23:09:45
合計ジャッジ時間 21,740 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
8,196 KB
testcase_01 AC 15 ms
8,180 KB
testcase_02 AC 877 ms
32,680 KB
testcase_03 AC 16 ms
8,108 KB
testcase_04 AC 320 ms
19,176 KB
testcase_05 AC 550 ms
23,008 KB
testcase_06 AC 390 ms
18,448 KB
testcase_07 AC 165 ms
11,352 KB
testcase_08 AC 252 ms
16,940 KB
testcase_09 AC 621 ms
27,104 KB
testcase_10 AC 273 ms
15,660 KB
testcase_11 AC 793 ms
30,852 KB
testcase_12 AC 699 ms
27,792 KB
testcase_13 AC 167 ms
13,636 KB
testcase_14 AC 512 ms
25,076 KB
testcase_15 AC 760 ms
30,668 KB
testcase_16 AC 694 ms
30,332 KB
testcase_17 AC 218 ms
14,700 KB
testcase_18 AC 535 ms
22,220 KB
testcase_19 AC 541 ms
23,320 KB
testcase_20 AC 725 ms
28,156 KB
testcase_21 AC 433 ms
21,588 KB
testcase_22 AC 895 ms
33,840 KB
testcase_23 AC 235 ms
15,328 KB
testcase_24 AC 398 ms
20,760 KB
testcase_25 AC 560 ms
22,764 KB
testcase_26 AC 748 ms
29,076 KB
testcase_27 AC 902 ms
34,564 KB
testcase_28 AC 452 ms
19,712 KB
testcase_29 AC 611 ms
25,448 KB
testcase_30 AC 350 ms
20,248 KB
testcase_31 AC 346 ms
13,156 KB
testcase_32 AC 332 ms
17,764 KB
testcase_33 AC 481 ms
20,160 KB
testcase_34 AC 515 ms
34,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,Q = list(map(int,input().split()))
a = [0] * (N-1)
b = [0] * (N-1)
for i in range(N - 1):
    a[i],b[i] = list(map(int,input().split()))
p = [0] * Q
x = [0] * Q
for i in range(Q):
    p[i],x[i] = list(map(int,input().split()))
tree = [[] for _ in range(N+1)]
for i in range(N-1):
    tree[a[i]].append(b[i])
    tree[b[i]].append(a[i])
sub = [1] * (N+1)
"""
def tansaku(now = 1,parent = 0):
    if len(tree[now]) == 1:
        return 1
    else:
        for i in tree[now]:
            if i != parent:
                sub[now] += tansaku(i,now)
        return sub[now]
tansaku()
"""
#再帰1000回しかしてくれない
q = []
flags = [False] * (N+1)
q.append((1,0))
while len(q):
    now,parent = q.pop()
    if flags[now]:
        for i in tree[now]:
            if i != parent:
                sub[now] += sub[i]
    else:
        flags[now] = True
        q.append((now,parent))
        for i in tree[now]:
            if i != parent:
                q.append((i,now))
    
_sum = 0
for i in range(Q):
    _sum += sub[p[i]] * x[i]
    print(_sum)
    
0