結果

問題 No.1637 Easy Tree Query
ユーザー qibqib
提出日時 2022-11-17 00:00:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 476 ms / 2,000 ms
コード長 636 bytes
コンパイル時間 596 ms
コンパイル使用メモリ 81,720 KB
実行使用メモリ 95,476 KB
最終ジャッジ日時 2023-10-17 21:15:38
合計ジャッジ時間 15,537 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,476 KB
testcase_01 AC 37 ms
53,476 KB
testcase_02 AC 407 ms
90,936 KB
testcase_03 AC 37 ms
53,476 KB
testcase_04 AC 203 ms
84,020 KB
testcase_05 AC 376 ms
82,248 KB
testcase_06 AC 282 ms
80,480 KB
testcase_07 AC 191 ms
76,636 KB
testcase_08 AC 177 ms
82,536 KB
testcase_09 AC 341 ms
87,864 KB
testcase_10 AC 230 ms
79,840 KB
testcase_11 AC 426 ms
88,688 KB
testcase_12 AC 394 ms
85,852 KB
testcase_13 AC 163 ms
79,752 KB
testcase_14 AC 263 ms
87,880 KB
testcase_15 AC 404 ms
89,220 KB
testcase_16 AC 345 ms
90,612 KB
testcase_17 AC 198 ms
79,884 KB
testcase_18 AC 350 ms
81,796 KB
testcase_19 AC 356 ms
83,508 KB
testcase_20 AC 417 ms
85,612 KB
testcase_21 AC 271 ms
84,320 KB
testcase_22 AC 456 ms
91,184 KB
testcase_23 AC 193 ms
80,584 KB
testcase_24 AC 244 ms
84,200 KB
testcase_25 AC 371 ms
81,376 KB
testcase_26 AC 422 ms
86,724 KB
testcase_27 AC 476 ms
90,236 KB
testcase_28 AC 298 ms
81,040 KB
testcase_29 AC 358 ms
85,156 KB
testcase_30 AC 196 ms
84,960 KB
testcase_31 AC 273 ms
75,944 KB
testcase_32 AC 236 ms
81,588 KB
testcase_33 AC 351 ms
79,848 KB
testcase_34 AC 158 ms
95,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

src = 0
st = [~ src, src]
par = [None for _ in range(n)]
dp = [0 for _ in range(n)]
while len(st) > 0:
  cur = st.pop()
  if cur >= 0:
    for nxt in g[cur]:
      if nxt == par[cur]:
        continue
      par[nxt] = cur
      st.append(~ nxt)
      st.append(nxt)
  
  else:
    dp[~ cur] += 1
    if not par[~ cur] is None:
      dp[par[~ cur]] += dp[~ cur]

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