結果

問題 No.1637 Easy Tree Query
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2024-01-10 12:06:06
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,117 ms / 2,000 ms
コード長 882 bytes
コンパイル時間 265 ms
コンパイル使用メモリ 11,904 KB
実行使用メモリ 49,400 KB
最終ジャッジ日時 2024-01-10 12:06:33
合計ジャッジ時間 23,388 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
10,240 KB
testcase_01 AC 28 ms
10,240 KB
testcase_02 AC 1,049 ms
28,288 KB
testcase_03 AC 29 ms
10,240 KB
testcase_04 AC 336 ms
19,712 KB
testcase_05 AC 760 ms
17,024 KB
testcase_06 AC 532 ms
15,104 KB
testcase_07 AC 260 ms
10,624 KB
testcase_08 AC 266 ms
17,664 KB
testcase_09 AC 716 ms
24,064 KB
testcase_10 AC 364 ms
14,208 KB
testcase_11 AC 943 ms
25,472 KB
testcase_12 AC 881 ms
22,016 KB
testcase_13 AC 177 ms
14,336 KB
testcase_14 AC 498 ms
24,320 KB
testcase_15 AC 874 ms
26,112 KB
testcase_16 AC 724 ms
27,904 KB
testcase_17 AC 286 ms
14,336 KB
testcase_18 AC 711 ms
17,024 KB
testcase_19 AC 742 ms
18,432 KB
testcase_20 AC 1,005 ms
21,632 KB
testcase_21 AC 513 ms
19,840 KB
testcase_22 AC 1,081 ms
28,288 KB
testcase_23 AC 264 ms
15,488 KB
testcase_24 AC 456 ms
19,968 KB
testcase_25 AC 875 ms
16,512 KB
testcase_26 AC 989 ms
22,656 KB
testcase_27 AC 1,117 ms
27,392 KB
testcase_28 AC 582 ms
16,000 KB
testcase_29 AC 716 ms
21,120 KB
testcase_30 AC 339 ms
20,992 KB
testcase_31 AC 572 ms
10,240 KB
testcase_32 AC 390 ms
16,768 KB
testcase_33 AC 729 ms
14,208 KB
testcase_34 AC 574 ms
49,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import *
from itertools import *
from functools import *
from heapq import *
import sys,math
# input = sys.stdin.readline
sys.setrecursionlimit(200000)

N,Q = map(int,input().split())
e = [[] for _ in range(N)]
for i in range(N-1):
  a,b = map(int,input().split())
  a -= 1
  b -= 1
  e[a].append(b)
  e[b].append(a)

dist = [-1]*N
def dfs(x):
  if(x!=0):
    if len(e[x]) == 1:
        return
  for ix in e[x]:
    if dist[ix] == -1:
      dist[ix] = dist[x] + 1
      dfs(ix)
  return
dist[0] = 0
dfs(0)
flg = [-1]*N
def dfs_v2(x):
  if(x!=0):
    if len(e[x]) == 1:
        flg[x] = 1
        return 1
  if flg[x] != -1:
    return flg[x]
  tmp = 1
  for ix in e[x]:
    if dist[ix] > dist[x]:
      tmp += dfs_v2(ix)
  flg[x] = tmp
  return tmp
dfs_v2(0)
S = 0
for _ in range(Q):
  p,x = map(int,input().split())
  p -= 1
  S += x*flg[p]
  print(S)
# print(flg)
0