結果

問題 No.1637 Easy Tree Query
ユーザー nephrologistnephrologist
提出日時 2021-08-06 21:45:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 494 ms / 2,000 ms
コード長 1,017 bytes
コンパイル時間 1,539 ms
コンパイル使用メモリ 81,720 KB
実行使用メモリ 95,964 KB
最終ジャッジ日時 2023-10-17 05:06:53
合計ジャッジ時間 14,548 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,408 KB
testcase_01 AC 38 ms
53,408 KB
testcase_02 AC 432 ms
89,976 KB
testcase_03 AC 38 ms
53,432 KB
testcase_04 AC 217 ms
82,804 KB
testcase_05 AC 386 ms
82,152 KB
testcase_06 AC 292 ms
80,132 KB
testcase_07 AC 201 ms
76,616 KB
testcase_08 AC 187 ms
82,084 KB
testcase_09 AC 346 ms
86,156 KB
testcase_10 AC 237 ms
79,444 KB
testcase_11 AC 444 ms
87,912 KB
testcase_12 AC 420 ms
85,268 KB
testcase_13 AC 165 ms
79,456 KB
testcase_14 AC 287 ms
86,700 KB
testcase_15 AC 417 ms
88,436 KB
testcase_16 AC 366 ms
89,832 KB
testcase_17 AC 197 ms
79,528 KB
testcase_18 AC 368 ms
81,624 KB
testcase_19 AC 375 ms
82,584 KB
testcase_20 AC 439 ms
85,608 KB
testcase_21 AC 277 ms
83,076 KB
testcase_22 AC 469 ms
90,148 KB
testcase_23 AC 196 ms
80,248 KB
testcase_24 AC 249 ms
83,080 KB
testcase_25 AC 384 ms
81,044 KB
testcase_26 AC 442 ms
86,244 KB
testcase_27 AC 494 ms
89,428 KB
testcase_28 AC 310 ms
80,712 KB
testcase_29 AC 368 ms
84,588 KB
testcase_30 AC 207 ms
84,404 KB
testcase_31 AC 289 ms
75,836 KB
testcase_32 AC 238 ms
81,344 KB
testcase_33 AC 361 ms
79,444 KB
testcase_34 AC 161 ms
95,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n, q = map(int, input().split())

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


# graph and n is necessary
def dfs(start):
    par = [-1] * n
    depth = [-1] * n
    size = [0] * n
    stack = []
    stack.append(~start)
    stack.append(start)
    depth[start] = 0
    while stack:
        v = stack.pop()
        if v >= 0:
            d = depth[v]
            for u in graph[v]:
                if par[v] == u:
                    continue
                par[u] = v
                depth[u] = d + 1
                stack.append(~u)
                stack.append(u)
        else:
            a = ~v
            size[a] = 1
            for u in graph[a]:
                if u == par[a]:
                    continue
                size[a] += size[u]
    return size


size = dfs(0)

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

0