結果
問題 | 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 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 43 ms
53,760 KB |
testcase_01 | AC | 43 ms
54,144 KB |
testcase_02 | AC | 44 ms
54,272 KB |
testcase_03 | AC | 47 ms
60,288 KB |
testcase_04 | AC | 44 ms
54,528 KB |
testcase_05 | AC | 46 ms
60,288 KB |
testcase_06 | AC | 47 ms
60,544 KB |
testcase_07 | AC | 58 ms
68,480 KB |
testcase_08 | AC | 59 ms
68,736 KB |
testcase_09 | AC | 61 ms
69,888 KB |
testcase_10 | AC | 61 ms
70,144 KB |
testcase_11 | AC | 61 ms
69,632 KB |
testcase_12 | AC | 63 ms
70,912 KB |
testcase_13 | AC | 64 ms
71,424 KB |
testcase_14 | AC | 63 ms
71,168 KB |
testcase_15 | AC | 64 ms
73,992 KB |
testcase_16 | AC | 65 ms
72,064 KB |
testcase_17 | AC | 219 ms
100,360 KB |
testcase_18 | AC | 211 ms
100,152 KB |
testcase_19 | AC | 213 ms
100,608 KB |
testcase_20 | AC | 215 ms
100,020 KB |
testcase_21 | AC | 212 ms
97,024 KB |
testcase_22 | AC | 214 ms
97,052 KB |
testcase_23 | AC | 216 ms
97,152 KB |
testcase_24 | AC | 212 ms
96,896 KB |
testcase_25 | AC | 215 ms
96,896 KB |
testcase_26 | AC | 219 ms
99,968 KB |
testcase_27 | AC | 220 ms
98,944 KB |
testcase_28 | AC | 213 ms
98,912 KB |
testcase_29 | AC | 213 ms
98,180 KB |
testcase_30 | AC | 218 ms
99,328 KB |
testcase_31 | AC | 219 ms
100,000 KB |
ソースコード
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)