結果

問題 No.1075 木の上の山
ユーザー Kiri8128Kiri8128
提出日時 2020-05-13 02:44:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 279 ms / 2,000 ms
コード長 1,624 bytes
コンパイル時間 1,235 ms
コンパイル使用メモリ 87,064 KB
実行使用メモリ 102,216 KB
最終ジャッジ日時 2023-10-12 07:40:39
合計ジャッジ時間 9,903 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
71,804 KB
testcase_01 AC 91 ms
71,664 KB
testcase_02 AC 90 ms
71,652 KB
testcase_03 AC 96 ms
76,892 KB
testcase_04 AC 92 ms
71,876 KB
testcase_05 AC 97 ms
76,768 KB
testcase_06 AC 97 ms
76,876 KB
testcase_07 AC 108 ms
77,228 KB
testcase_08 AC 110 ms
77,528 KB
testcase_09 AC 109 ms
77,384 KB
testcase_10 AC 109 ms
77,456 KB
testcase_11 AC 108 ms
77,204 KB
testcase_12 AC 108 ms
77,332 KB
testcase_13 AC 110 ms
77,300 KB
testcase_14 AC 112 ms
77,412 KB
testcase_15 AC 114 ms
77,140 KB
testcase_16 AC 112 ms
76,896 KB
testcase_17 AC 276 ms
102,128 KB
testcase_18 AC 266 ms
102,216 KB
testcase_19 AC 272 ms
102,196 KB
testcase_20 AC 270 ms
101,968 KB
testcase_21 AC 279 ms
98,548 KB
testcase_22 AC 274 ms
98,520 KB
testcase_23 AC 273 ms
98,644 KB
testcase_24 AC 264 ms
98,608 KB
testcase_25 AC 267 ms
98,736 KB
testcase_26 AC 273 ms
100,832 KB
testcase_27 AC 271 ms
100,088 KB
testcase_28 AC 268 ms
100,272 KB
testcase_29 AC 274 ms
100,928 KB
testcase_30 AC 275 ms
101,456 KB
testcase_31 AC 272 ms
101,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import deque

mod = 10 ** 9 + 7
N, K = map(int, input().split())
X = [[] for i in range(N)]
for i in range(N-1):
    x, y = map(int, input().split())
    X[x-1].append(y-1)
    X[y-1].append(x-1)

P = [-1] * N
Q = deque([0])
R = []
while Q:
    i = deque.popleft(Q)
    R.append(i)
    for a in X[i]:
        if a != P[i]:
            P[a] = i
            X[a].remove(i)
            deque.append(Q, a)

# Settings Start
unit = [1 for i in range(K)]
merge = lambda a, b: [aa * bb % mod for aa, bb in zip(a, b)]
def adj_bu(a, i):
    b = a[:]
    for j in range(1, K):
        b[j] += b[j-1]
        b[j] %= mod
    return b
def adj_td(a, i, p):
    b = a[:]
    for j in range(1, K):
        b[j] += b[j-1]
        b[j] %= mod
    return b
def adj_td_unique(a, i, p):
    b = [0] + a[:-1]
    for j in range(1, K):
        b[j] += b[j-1]
        b[j] %= mod
    return b
def adj_fin(a, i):
    b = a[:]
    for j in range(1, K):
        b[j] += b[j-1]
        b[j] %= mod
    return b[-1]
# Settings End

ME = [unit] * N
XX = [0] * N
TD = [unit] * N
ANS = [0] * N
for i in R[1:][::-1]:
    XX[i] = adj_bu(ME[i], i)
    p = P[i]
    ME[p] = merge(ME[p], XX[i])
XX[R[0]] = adj_fin(ME[R[0]], R[0])
ANS[R[0]] = adj_fin(ME[R[0]], R[0])

for i in R:
    ac = TD[i]
    for j in X[i]:
        TD[j] = ac
        ac = merge(ac, XX[j])
    ac = unit
    for j in X[i][::-1]:
        TD[j] = merge(TD[j], ac)
        ANS[j] = adj_fin(merge(ME[j], adj_td_unique(TD[j], j, i)), j)
        TD[j] = adj_td(TD[j], j, i)
        ac = merge(ac, XX[j])

print(sum([a for a in ANS]) % mod)
0