結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
77,012 KB
testcase_01 AC 147 ms
77,020 KB
testcase_02 AC 154 ms
76,996 KB
testcase_03 AC 140 ms
77,060 KB
testcase_04 AC 155 ms
76,884 KB
testcase_05 AC 73 ms
71,156 KB
testcase_06 AC 88 ms
76,648 KB
testcase_07 AC 113 ms
76,844 KB
testcase_08 AC 95 ms
76,792 KB
testcase_09 AC 111 ms
76,348 KB
testcase_10 AC 104 ms
76,772 KB
testcase_11 AC 141 ms
76,876 KB
testcase_12 AC 77 ms
75,904 KB
testcase_13 AC 99 ms
75,920 KB
testcase_14 AC 103 ms
76,792 KB
testcase_15 AC 96 ms
76,824 KB
testcase_16 AC 82 ms
75,988 KB
testcase_17 AC 91 ms
76,524 KB
testcase_18 AC 121 ms
76,700 KB
testcase_19 AC 97 ms
76,684 KB
testcase_20 AC 92 ms
76,536 KB
testcase_21 AC 81 ms
76,508 KB
testcase_22 AC 97 ms
76,156 KB
testcase_23 AC 72 ms
71,340 KB
testcase_24 AC 71 ms
71,340 KB
testcase_25 AC 74 ms
71,320 KB
testcase_26 AC 87 ms
76,804 KB
testcase_27 AC 80 ms
76,356 KB
testcase_28 AC 86 ms
76,216 KB
testcase_29 AC 88 ms
76,280 KB
testcase_30 AC 110 ms
76,860 KB
testcase_31 AC 143 ms
77,112 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