結果

問題 No.1637 Easy Tree Query
ユーザー yvay5cqeyvay5cqe
提出日時 2021-08-18 21:28:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 288 ms / 2,000 ms
コード長 539 bytes
コンパイル時間 292 ms
コンパイル使用メモリ 82,248 KB
実行使用メモリ 177,100 KB
最終ジャッジ日時 2024-04-20 02:34:05
合計ジャッジ時間 9,467 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
52,128 KB
testcase_01 AC 33 ms
53,496 KB
testcase_02 AC 269 ms
106,136 KB
testcase_03 AC 33 ms
53,108 KB
testcase_04 AC 158 ms
88,268 KB
testcase_05 AC 217 ms
93,232 KB
testcase_06 AC 174 ms
88,028 KB
testcase_07 AC 120 ms
80,108 KB
testcase_08 AC 138 ms
86,240 KB
testcase_09 AC 218 ms
97,120 KB
testcase_10 AC 142 ms
85,048 KB
testcase_11 AC 257 ms
100,952 KB
testcase_12 AC 228 ms
97,860 KB
testcase_13 AC 122 ms
82,240 KB
testcase_14 AC 195 ms
94,312 KB
testcase_15 AC 245 ms
100,788 KB
testcase_16 AC 237 ms
100,264 KB
testcase_17 AC 135 ms
83,404 KB
testcase_18 AC 233 ms
92,500 KB
testcase_19 AC 209 ms
93,296 KB
testcase_20 AC 239 ms
98,084 KB
testcase_21 AC 183 ms
91,132 KB
testcase_22 AC 277 ms
104,312 KB
testcase_23 AC 137 ms
84,208 KB
testcase_24 AC 177 ms
89,940 KB
testcase_25 AC 201 ms
92,308 KB
testcase_26 AC 246 ms
98,968 KB
testcase_27 AC 288 ms
104,724 KB
testcase_28 AC 173 ms
89,140 KB
testcase_29 AC 216 ms
94,988 KB
testcase_30 AC 155 ms
89,424 KB
testcase_31 AC 138 ms
84,624 KB
testcase_32 AC 151 ms
87,160 KB
testcase_33 AC 181 ms
89,876 KB
testcase_34 AC 251 ms
177,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**7)
N, Q = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(N-1)]
qx = [tuple(map(int, input().split())) for _ in range(Q)]

g = [[] for _ in range(N)]

for a, b in ab:
    g[a-1].append(b-1)
    g[b-1].append(a-1)

cnts = [0] * N


def dfs(now, prev):
    cnt = 1
    for nx in g[now]:
        if nx == prev:
            continue
        cnt += dfs(nx, now)
    cnts[now] = cnt
    return cnt


dfs(0, -1)

ans = 0
for q, x in qx:
    ans += cnts[q-1] * x
    print(ans)
0