結果

問題 No.1637 Easy Tree Query
ユーザー prd_xxxprd_xxx
提出日時 2021-08-06 23:12:28
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 488 ms / 2,000 ms
コード長 617 bytes
コンパイル時間 92 ms
コンパイル使用メモリ 12,012 KB
実行使用メモリ 60,340 KB
最終ジャッジ日時 2023-10-17 04:46:35
合計ジャッジ時間 12,282 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,064 KB
testcase_01 AC 29 ms
10,064 KB
testcase_02 AC 448 ms
50,772 KB
testcase_03 AC 29 ms
10,064 KB
testcase_04 AC 185 ms
27,000 KB
testcase_05 AC 318 ms
34,032 KB
testcase_06 AC 222 ms
26,420 KB
testcase_07 AC 110 ms
15,512 KB
testcase_08 AC 151 ms
23,520 KB
testcase_09 AC 356 ms
39,864 KB
testcase_10 AC 165 ms
22,036 KB
testcase_11 AC 443 ms
46,256 KB
testcase_12 AC 394 ms
41,336 KB
testcase_13 AC 101 ms
18,284 KB
testcase_14 AC 288 ms
36,256 KB
testcase_15 AC 442 ms
45,872 KB
testcase_16 AC 389 ms
44,584 KB
testcase_17 AC 131 ms
20,140 KB
testcase_18 AC 301 ms
32,872 KB
testcase_19 AC 316 ms
34,268 KB
testcase_20 AC 401 ms
41,892 KB
testcase_21 AC 244 ms
31,276 KB
testcase_22 AC 469 ms
50,960 KB
testcase_23 AC 133 ms
21,304 KB
testcase_24 AC 214 ms
29,660 KB
testcase_25 AC 311 ms
33,280 KB
testcase_26 AC 405 ms
43,412 KB
testcase_27 AC 488 ms
51,688 KB
testcase_28 AC 248 ms
28,520 KB
testcase_29 AC 337 ms
37,512 KB
testcase_30 AC 217 ms
28,640 KB
testcase_31 AC 210 ms
20,032 KB
testcase_32 AC 186 ms
25,504 KB
testcase_33 AC 278 ms
29,788 KB
testcase_34 AC 273 ms
60,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
N,Q = map(int,input().split())
AB = [tuple(map(int,input().split())) for i in range(N-1)]
PX = [tuple(map(int,input().split())) for i in range(Q)]

es = [[] for _ in range(N)]
for a,b in AB:
    a,b = a-1,b-1
    es[a].append(b)
    es[b].append(a)

cs = [0] * N
def dfs(v,p=-1):
    if len(es[v])==1 and es[v][0]==p:
        cs[v] = 1
        return cs[v]
    ret = 1
    for to in es[v]:
        if to==p: continue
        ret += dfs(to,v)
    cs[v] = ret
    return ret
dfs(0)

ans = 0
for p,x in PX:
    p -= 1
    ans += cs[p] * x
    print(ans)
0