結果
問題 | No.1075 木の上の山 |
ユーザー | Kiri8128 |
提出日時 | 2020-05-13 02:44:27 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 220 ms / 2,000 ms |
コード長 | 1,624 bytes |
コンパイル時間 | 797 ms |
コンパイル使用メモリ | 82,216 KB |
実行使用メモリ | 100,608 KB |
最終ジャッジ日時 | 2024-09-14 06:50:14 |
合計ジャッジ時間 | 6,193 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 30 |
ソースコード
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)