結果

問題 No.1227 I hate ThREE
ユーザー rlangevin
提出日時 2024-01-22 12:18:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,542 ms / 2,000 ms
コード長 1,200 bytes
コンパイル時間 306 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 132,344 KB
最終ジャッジ日時 2024-09-28 06:23:27
合計ジャッジ時間 14,776 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

def non_rec_dfs(G, s):
    stack = [s]
    N = len(G)
    par = [-1] * N
    sz = [1] * N
    dp = [[1] * M for _ in range(N)]
    ans = 0
    while stack:
        u = stack.pop()
        if u >= 0:
            stack.append(~u)
            for v in G[u]:
                if v == par[u]:
                    continue
                par[v] = u
                stack.append(v)
        else:
            u = ~u
            for i in range(M):
                for v in G[u]:
                    temp = 0
                    if v == par[u]:
                        continue
                    sz[u] += sz[v]
                    if i >= 3:
                        temp += dp[v][i - 3]
                    if i < M - 3:
                        temp += dp[v][i + 3]
                    dp[u][i] *= temp
                    dp[u][i] %= mod
                
    return dp[0]


N, C = map(int, input().split())
M = min(7000, C)
mod = 10 ** 9 + 7
G = [[] for i in range(N)]
for i in range(N - 1):
    u, v = map(int, input().split())
    u, v = u - 1, v - 1
    G[u].append(v)
    G[v].append(u)
    
dp = non_rec_dfs(G, 0)
ans = sum(dp) + dp[M//2] * (C - M)
print(ans%mod)
0