結果
問題 | No.196 典型DP (1) |
ユーザー | Mao-beta |
提出日時 | 2024-04-04 02:17:35 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 1,195 ms / 2,000 ms |
コード長 | 1,963 bytes |
コンパイル時間 | 163 ms |
コンパイル使用メモリ | 13,056 KB |
実行使用メモリ | 64,768 KB |
最終ジャッジ日時 | 2024-10-01 00:13:17 |
合計ジャッジ時間 | 24,394 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 28 ms
11,136 KB |
testcase_01 | AC | 28 ms
11,008 KB |
testcase_02 | AC | 27 ms
11,136 KB |
testcase_03 | AC | 27 ms
11,264 KB |
testcase_04 | AC | 27 ms
11,136 KB |
testcase_05 | AC | 28 ms
11,008 KB |
testcase_06 | AC | 27 ms
11,136 KB |
testcase_07 | AC | 27 ms
11,008 KB |
testcase_08 | AC | 26 ms
11,136 KB |
testcase_09 | AC | 27 ms
11,136 KB |
testcase_10 | AC | 28 ms
11,136 KB |
testcase_11 | AC | 29 ms
11,008 KB |
testcase_12 | AC | 31 ms
11,264 KB |
testcase_13 | AC | 28 ms
11,136 KB |
testcase_14 | AC | 29 ms
11,136 KB |
testcase_15 | AC | 49 ms
12,416 KB |
testcase_16 | AC | 116 ms
15,232 KB |
testcase_17 | AC | 235 ms
20,608 KB |
testcase_18 | AC | 415 ms
28,800 KB |
testcase_19 | AC | 544 ms
33,664 KB |
testcase_20 | AC | 809 ms
43,520 KB |
testcase_21 | AC | 805 ms
44,032 KB |
testcase_22 | AC | 827 ms
43,904 KB |
testcase_23 | AC | 978 ms
64,768 KB |
testcase_24 | AC | 927 ms
58,240 KB |
testcase_25 | AC | 880 ms
56,192 KB |
testcase_26 | AC | 1,002 ms
43,648 KB |
testcase_27 | AC | 1,012 ms
43,648 KB |
testcase_28 | AC | 1,004 ms
43,520 KB |
testcase_29 | AC | 1,012 ms
43,776 KB |
testcase_30 | AC | 1,010 ms
43,520 KB |
testcase_31 | AC | 1,132 ms
43,008 KB |
testcase_32 | AC | 1,108 ms
43,008 KB |
testcase_33 | AC | 1,195 ms
43,008 KB |
testcase_34 | AC | 1,104 ms
43,264 KB |
testcase_35 | AC | 1,103 ms
43,136 KB |
testcase_36 | AC | 1,046 ms
43,264 KB |
testcase_37 | AC | 791 ms
43,264 KB |
testcase_38 | AC | 1,066 ms
43,008 KB |
testcase_39 | AC | 1,002 ms
43,136 KB |
testcase_40 | AC | 1,111 ms
43,264 KB |
testcase_41 | AC | 28 ms
11,136 KB |
testcase_42 | AC | 27 ms
11,136 KB |
testcase_43 | AC | 27 ms
11,008 KB |
ソースコード
import sys import math import bisect from heapq import heapify, heappop, heappush from collections import deque, defaultdict, Counter from functools import lru_cache from itertools import accumulate, combinations, permutations, product sys.setrecursionlimit(1000000) MOD = 10 ** 9 + 7 MOD99 = 998244353 input = lambda: sys.stdin.readline().strip() NI = lambda: int(input()) NMI = lambda: map(int, input().split()) NLI = lambda: list(NMI()) SI = lambda: input() SMI = lambda: input().split() SLI = lambda: list(SMI()) EI = lambda m: [NLI() for _ in range(m)] def adjlist(n, edges, directed=False, in_origin=1): if len(edges) == 0: return [[] for _ in range(n)] weighted = True if len(edges[0]) > 2 else False if in_origin == 1: if weighted: edges = [[x-1, y-1, w] for x, y, w in edges] else: edges = [[x-1, y-1] for x, y in edges] res = [[] for _ in range(n)] if weighted: for u, v, c in edges: res[u].append([v, c]) if not directed: res[v].append([u, c]) else: for u, v in edges: res[u].append(v) if not directed: res[v].append(u) return res def main(): N, K = NMI() AB = EI(N-1) # i以下の部分木でj個塗る dp = [[0]*(N+1) for _ in range(N)] P = [N] * N G = adjlist(N, AB, in_origin=0) # i以下の部分木のサイズ SZ = [0] * N def dfs(now): SZ[now] = 1 dp[now][0] = 1 for v in G[now]: if v == P[now]: continue P[v] = now dfs(v) for i in range(SZ[now], -1, -1): for j in range(SZ[v], 0, -1): dp[now][i+j] += dp[now][i] * dp[v][j] % MOD dp[now][i+j] %= MOD SZ[now] += SZ[v] dp[now][SZ[now]] = 1 dfs(0) print(dp[0][K]) if __name__ == "__main__": main()