結果

問題 No.1488 Max Score of the Tree
ユーザー c-yanc-yan
提出日時 2021-04-24 01:49:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 156 ms / 2,000 ms
コード長 948 bytes
コンパイル時間 266 ms
コンパイル使用メモリ 87,112 KB
実行使用メモリ 77,132 KB
最終ジャッジ日時 2023-09-17 13:19:39
合計ジャッジ時間 4,863 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
77,108 KB
testcase_01 AC 148 ms
77,132 KB
testcase_02 AC 151 ms
76,912 KB
testcase_03 AC 139 ms
76,996 KB
testcase_04 AC 156 ms
76,992 KB
testcase_05 AC 71 ms
71,436 KB
testcase_06 AC 89 ms
77,040 KB
testcase_07 AC 113 ms
76,924 KB
testcase_08 AC 96 ms
76,824 KB
testcase_09 AC 110 ms
76,760 KB
testcase_10 AC 105 ms
76,976 KB
testcase_11 AC 142 ms
76,884 KB
testcase_12 AC 73 ms
75,976 KB
testcase_13 AC 96 ms
76,116 KB
testcase_14 AC 103 ms
76,968 KB
testcase_15 AC 95 ms
76,820 KB
testcase_16 AC 80 ms
76,264 KB
testcase_17 AC 84 ms
76,696 KB
testcase_18 AC 117 ms
76,864 KB
testcase_19 AC 94 ms
76,824 KB
testcase_20 AC 87 ms
76,608 KB
testcase_21 AC 77 ms
76,352 KB
testcase_22 AC 93 ms
76,448 KB
testcase_23 AC 70 ms
71,388 KB
testcase_24 AC 72 ms
71,312 KB
testcase_25 AC 71 ms
71,368 KB
testcase_26 AC 85 ms
76,820 KB
testcase_27 AC 81 ms
76,360 KB
testcase_28 AC 86 ms
76,432 KB
testcase_29 AC 88 ms
76,568 KB
testcase_30 AC 112 ms
77,116 KB
testcase_31 AC 145 ms
77,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stdin

readline = stdin.readline

N, K = map(int, readline().split())
links = [{} for _ in range(N)]
for _ in range(N - 1):
    a, b, c = map(int, readline().split())
    a, b = a - 1, b - 1
    links[a][b] = c
    links[b][a] = c


def dfs(parent):
    l = links[parent]
    if len(l) == 0:
        return (0, 1)

    total_score = 0
    total_leaves = 0
    for child in l:
        del links[child][parent]
        score, leaves = dfs(child)
        total_score += score
        total_leaves += leaves
        t.append((l[child], leaves))
        total_score += l[child] * leaves
    return total_score, total_leaves


t = []
result, _ = dfs(0)

dp = [-1] * (K + 1)
dp[0] = 0
for i in range(len(t)):
    for j in range(K - 1,  -1, -1):
        if dp[j] == -1:
            continue
        a = j + t[i][0]
        if a > K:
            continue
        dp[a] = max(dp[a], dp[j] + t[i][0] * t[i][1])
result += max(dp)
print(result)
0