結果

問題 No.1637 Easy Tree Query
ユーザー puznekopuzneko
提出日時 2021-12-02 23:22:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 313 ms / 2,000 ms
コード長 789 bytes
コンパイル時間 2,202 ms
コンパイル使用メモリ 86,516 KB
実行使用メモリ 150,540 KB
最終ジャッジ日時 2023-09-18 11:05:28
合計ジャッジ時間 15,621 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,076 KB
testcase_01 AC 73 ms
71,412 KB
testcase_02 AC 271 ms
150,540 KB
testcase_03 AC 79 ms
71,272 KB
testcase_04 AC 170 ms
103,108 KB
testcase_05 AC 219 ms
128,200 KB
testcase_06 AC 179 ms
110,736 KB
testcase_07 AC 131 ms
90,444 KB
testcase_08 AC 159 ms
98,720 KB
testcase_09 AC 229 ms
123,464 KB
testcase_10 AC 159 ms
101,760 KB
testcase_11 AC 277 ms
130,528 KB
testcase_12 AC 251 ms
127,764 KB
testcase_13 AC 139 ms
89,760 KB
testcase_14 AC 210 ms
116,004 KB
testcase_15 AC 277 ms
127,732 KB
testcase_16 AC 265 ms
129,732 KB
testcase_17 AC 149 ms
94,764 KB
testcase_18 AC 204 ms
123,336 KB
testcase_19 AC 227 ms
129,880 KB
testcase_20 AC 273 ms
126,900 KB
testcase_21 AC 195 ms
111,468 KB
testcase_22 AC 313 ms
142,240 KB
testcase_23 AC 154 ms
97,520 KB
testcase_24 AC 187 ms
108,456 KB
testcase_25 AC 217 ms
128,244 KB
testcase_26 AC 267 ms
128,892 KB
testcase_27 AC 312 ms
148,320 KB
testcase_28 AC 181 ms
114,780 KB
testcase_29 AC 233 ms
127,880 KB
testcase_30 AC 178 ms
103,140 KB
testcase_31 AC 145 ms
108,608 KB
testcase_32 AC 167 ms
105,480 KB
testcase_33 AC 195 ms
123,148 KB
testcase_34 AC 169 ms
116,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stdin
n, q, *indata = map(int, stdin.read().split())
offset = 0
g = [[] for i in range(n+1)]
for i in range(n-1):
    s, t = indata[offset + 2*i],indata[offset + 2*i+1]
    g[s].append(t)
    g[t].append(s)

dp = [0 for i in range(n+1)]
check = [0 for i in range(n+1)]
que = [(1,0)]
while que:
    now, inout = que.pop()
    if inout == 0:
        check[now] = 1
        que.append((now,1))
        for i in g[now]:
            if check[i] == 0:
                que.append((i,0))
    else:
        check[now] = 2
        dp[now] = 1
        for i in g[now]:
            if check[i] == 2:
                dp[now] += dp[i]

offset = (n-1) * 2
ans = 0
for i in range(q):
    p, x = indata[offset + 2*i],indata[offset + 2*i+1]
    ans += x * dp[p]
    print("{}".format(ans))

0