結果

問題 No.1637 Easy Tree Query
ユーザー hir355hir355
提出日時 2021-08-06 21:25:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 517 ms / 2,000 ms
コード長 648 bytes
コンパイル時間 262 ms
コンパイル使用メモリ 81,632 KB
実行使用メモリ 96,724 KB
最終ジャッジ日時 2023-10-17 04:48:54
合計ジャッジ時間 13,420 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,444 KB
testcase_01 AC 38 ms
53,444 KB
testcase_02 AC 425 ms
96,204 KB
testcase_03 AC 37 ms
53,448 KB
testcase_04 AC 203 ms
86,072 KB
testcase_05 AC 378 ms
83,576 KB
testcase_06 AC 290 ms
80,840 KB
testcase_07 AC 197 ms
76,672 KB
testcase_08 AC 188 ms
84,392 KB
testcase_09 AC 346 ms
91,276 KB
testcase_10 AC 244 ms
79,984 KB
testcase_11 AC 445 ms
92,800 KB
testcase_12 AC 426 ms
88,280 KB
testcase_13 AC 165 ms
80,156 KB
testcase_14 AC 277 ms
91,556 KB
testcase_15 AC 425 ms
93,212 KB
testcase_16 AC 365 ms
96,724 KB
testcase_17 AC 199 ms
80,392 KB
testcase_18 AC 369 ms
83,312 KB
testcase_19 AC 365 ms
84,944 KB
testcase_20 AC 435 ms
87,940 KB
testcase_21 AC 277 ms
86,084 KB
testcase_22 AC 477 ms
96,356 KB
testcase_23 AC 197 ms
81,556 KB
testcase_24 AC 259 ms
86,084 KB
testcase_25 AC 389 ms
81,704 KB
testcase_26 AC 441 ms
90,160 KB
testcase_27 AC 517 ms
94,216 KB
testcase_28 AC 307 ms
81,764 KB
testcase_29 AC 374 ms
87,452 KB
testcase_30 AC 229 ms
87,340 KB
testcase_31 AC 291 ms
75,924 KB
testcase_32 AC 245 ms
83,032 KB
testcase_33 AC 371 ms
80,364 KB
testcase_34 AC 157 ms
95,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def tree_dfs(g, root=0):
    s = [root]
    d = [1] * len(g)
    order = []
    while s:
        p = s.pop()
        d[p] = 0
        order.append(p)
        for node in g[p]:
            if d[node]:
                s.append(node)
    return order

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

order = tree_dfs(g)
d = [0] * n
d[0] = 0
for v in order[::-1]:
    d[v] = 1
    for node in g[v]:
        d[v] += d[node]
ans = 0
for i in range(q):
    p, x = map(int, input().split())
    ans += d[p - 1] * x
    print(ans)
0