結果

問題 No.1227 I hate ThREE
ユーザー WSKRWSKR
提出日時 2020-09-22 00:20:58
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,461 bytes
コンパイル時間 285 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 124,544 KB
最終ジャッジ日時 2024-06-24 23:44:40
合計ジャッジ時間 11,883 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
54,272 KB
testcase_01 AC 48 ms
54,272 KB
testcase_02 AC 49 ms
54,400 KB
testcase_03 AC 112 ms
78,080 KB
testcase_04 AC 119 ms
77,696 KB
testcase_05 AC 123 ms
79,616 KB
testcase_06 AC 48 ms
54,272 KB
testcase_07 WA -
testcase_08 AC 143 ms
82,432 KB
testcase_09 AC 131 ms
81,024 KB
testcase_10 AC 114 ms
79,104 KB
testcase_11 AC 507 ms
119,680 KB
testcase_12 AC 566 ms
124,032 KB
testcase_13 AC 91 ms
76,672 KB
testcase_14 AC 127 ms
80,256 KB
testcase_15 AC 149 ms
83,584 KB
testcase_16 AC 254 ms
94,336 KB
testcase_17 AC 179 ms
86,784 KB
testcase_18 AC 305 ms
102,016 KB
testcase_19 AC 255 ms
96,256 KB
testcase_20 AC 324 ms
104,320 KB
testcase_21 AC 72 ms
68,224 KB
testcase_22 AC 267 ms
96,640 KB
testcase_23 WA -
testcase_24 AC 569 ms
124,416 KB
testcase_25 WA -
testcase_26 AC 451 ms
119,808 KB
testcase_27 AC 485 ms
122,624 KB
testcase_28 AC 504 ms
124,160 KB
testcase_29 AC 482 ms
124,032 KB
testcase_30 AC 461 ms
120,832 KB
testcase_31 AC 471 ms
120,704 KB
testcase_32 AC 504 ms
123,392 KB
testcase_33 AC 457 ms
120,192 KB
testcase_34 AC 481 ms
121,856 KB
testcase_35 AC 453 ms
119,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#i hate three
from collections import deque

mod = 10**9 + 7


def main():
    N, C = map(int, input().split())
    G = [[] for i in range(N+1)]
    for i in range(N-1):
        a, b = map(int, input().split())
        G[a].append(b)
        G[b].append(a)

    visited = [False for i in range(N+1)]
    depth = [0 for i in range(N+1)]
    child_cnt = [1 for i in range(N+1)]

    que = deque([])

    def dfs(s_node):
        visited[s_node] = True
        for child in G[s_node]:
            if visited[child] == False:
                depth[child] = depth[s_node] + 1
                dfs(child)
        for child in G[s_node]:
            if depth[child] > depth[s_node]:
                child_cnt[s_node] += child_cnt[child]
        que.append(s_node)

    dfs(1)

    if C > 9*N:
        dp = [[0 for i in range(6*N-5)] for i in range(N+1)]
        while que:
            node = que.popleft()
            for i in range(3*N-4):
                has_child = False
                v = 1
                for child in G[node]:
                    if depth[child] > depth[node]:
                        tmp_u = 0
                        has_child = True
                        if i >= 3:
                            tmp_u += dp[child][i-3]

                        if i+3 <= 3*N-5:
                            tmp_u += dp[child][i+3]

                        elif i+3 > 3*N-5:
                            tmp_u += pow(2, child_cnt[child]-1, mod)

                        v *= tmp_u
                        v %= mod
                dp[node][i] = v
                if has_child == False:
                    dp[node][i] = 1

            for i in range(3*N-3, 6*N-5):
                has_child = False
                v = 1
                for child in G[node]:
                    if depth[child] > depth[node]:

                        tmp_u = 0
                        has_child = True
                        if i+3 <= 6*N-6:
                            tmp_u += dp[child][i+3]
                        if i-3 >= 3*N-3:
                            tmp_u += dp[child][i-3]
                        else:
                            tmp_u += pow(2, child_cnt[child]-1, mod)

                        v *= tmp_u
                        v %= mod
                dp[node][i] = v
                if has_child == False:
                    dp[node][i] = 1

        ans = sum(dp[1][i] for i in range(3*N-4)) + sum(dp[1][i]
                                                        for i in range(3*N-3, 6*N-5)) + (C-6*N + 6)*pow(2, N-1, mod)
        print(ans % mod)
        return

    elif C <= 9*N:
        dp = [[0 for i in range(C+1)] for j in range(N+1)]
        while que:
            node = que.popleft()
            for i in range(1, C+1):
                has_child = False
                v = 1
                for child in G[node]:
                    if depth[child] > depth[node]:
                        has_child = True
                        tmp_u = 0
                        if 1 <= i-3 <= C:
                            tmp_u += dp[child][i-3]
                        if 1 <= i+3 <= C:
                            tmp_u += dp[child][i+3]
                        v *= (tmp_u)
                        v %= mod
                dp[node][i] = v % mod
                if has_child == False:
                    dp[node][i] = 1

        ans = sum(dp[1][i] for i in range(1, C+1)) % mod
        print(ans)
        return


if __name__ == "__main__":
    main()

0