結果

問題 No.1488 Max Score of the Tree
ユーザー c-yanc-yan
提出日時 2021-04-24 01:23:14
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 942 bytes
コンパイル時間 594 ms
コンパイル使用メモリ 10,844 KB
実行使用メモリ 11,276 KB
最終ジャッジ日時 2023-09-17 13:19:19
合計ジャッジ時間 25,341 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,336 ms
10,448 KB
testcase_01 TLE -
testcase_02 TLE -
testcase_03 AC 1,693 ms
10,976 KB
testcase_04 TLE -
testcase_05 AC 16 ms
8,028 KB
testcase_06 AC 252 ms
8,780 KB
testcase_07 AC 841 ms
10,068 KB
testcase_08 AC 387 ms
8,700 KB
testcase_09 AC 862 ms
9,256 KB
testcase_10 AC 650 ms
9,496 KB
testcase_11 AC 1,914 ms
11,112 KB
testcase_12 AC 20 ms
8,228 KB
testcase_13 AC 435 ms
8,728 KB
testcase_14 AC 638 ms
9,728 KB
testcase_15 AC 374 ms
9,328 KB
testcase_16 AC 63 ms
8,440 KB
testcase_17 AC 210 ms
9,204 KB
testcase_18 AC 1,078 ms
9,992 KB
testcase_19 AC 398 ms
9,304 KB
testcase_20 AC 183 ms
8,764 KB
testcase_21 AC 79 ms
8,472 KB
testcase_22 AC 421 ms
9,064 KB
testcase_23 AC 15 ms
7,976 KB
testcase_24 AC 15 ms
7,976 KB
testcase_25 AC 17 ms
8,064 KB
testcase_26 AC 230 ms
8,672 KB
testcase_27 AC 52 ms
7,976 KB
testcase_28 AC 134 ms
8,588 KB
testcase_29 AC 207 ms
8,940 KB
testcase_30 AC 901 ms
9,492 KB
testcase_31 TLE -
権限があれば一括ダウンロードができます

ソースコード

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