結果

問題 No.1637 Easy Tree Query
ユーザー irumo8202irumo8202
提出日時 2022-02-07 11:42:17
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,097 ms / 2,000 ms
コード長 651 bytes
コンパイル時間 132 ms
コンパイル使用メモリ 10,936 KB
実行使用メモリ 77,224 KB
最終ジャッジ日時 2023-09-02 20:37:30
合計ジャッジ時間 25,398 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
7,832 KB
testcase_01 AC 17 ms
7,796 KB
testcase_02 AC 937 ms
77,224 KB
testcase_03 AC 16 ms
7,872 KB
testcase_04 AC 339 ms
33,808 KB
testcase_05 AC 629 ms
50,708 KB
testcase_06 AC 406 ms
37,044 KB
testcase_07 AC 160 ms
19,544 KB
testcase_08 AC 263 ms
28,440 KB
testcase_09 AC 721 ms
55,680 KB
testcase_10 AC 288 ms
29,156 KB
testcase_11 AC 886 ms
67,108 KB
testcase_12 AC 813 ms
60,956 KB
testcase_13 AC 165 ms
20,880 KB
testcase_14 AC 575 ms
48,048 KB
testcase_15 AC 894 ms
65,996 KB
testcase_16 AC 813 ms
61,256 KB
testcase_17 AC 248 ms
24,716 KB
testcase_18 AC 612 ms
48,308 KB
testcase_19 AC 628 ms
50,072 KB
testcase_20 AC 859 ms
62,104 KB
testcase_21 AC 494 ms
42,216 KB
testcase_22 AC 1,055 ms
74,192 KB
testcase_23 AC 238 ms
26,208 KB
testcase_24 AC 454 ms
39,064 KB
testcase_25 AC 646 ms
50,044 KB
testcase_26 AC 887 ms
64,440 KB
testcase_27 AC 1,097 ms
76,716 KB
testcase_28 AC 470 ms
40,480 KB
testcase_29 AC 700 ms
53,844 KB
testcase_30 AC 381 ms
35,620 KB
testcase_31 AC 355 ms
30,968 KB
testcase_32 AC 366 ms
33,244 KB
testcase_33 AC 543 ms
44,548 KB
testcase_34 AC 533 ms
67,932 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10000000)

N, Q = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N - 1)]
PX = [list(map(int, input().split())) for _ in range(Q)]

G = [[] for _ in range(N + 1)]
order = [[-1, -1] for _ in range(N + 1)]

for a, b in AB:
    G[a].append(b)
    G[b].append(a)

p = -1


def dfs(v, pre=-1):
    global p
    p += 1
    order[v][0] = p
    for nx in G[v]:
        if nx == pre:
            continue
        dfs(nx, v)
    p += 1
    order[v][1] = p


dfs(1)
ans = []
tot = 0

for p, x in PX:
    tot += ((order[p][1] - order[p][0]) // 2 + 1) * x
    ans.append(tot)

print(*ans, sep="\n")
0