結果

問題 No.1488 Max Score of the Tree
ユーザー c-yanc-yan
提出日時 2021-04-23 22:25:40
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 884 bytes
コンパイル時間 427 ms
コンパイル使用メモリ 87,076 KB
実行使用メモリ 77,236 KB
最終ジャッジ日時 2023-09-17 12:33:15
合計ジャッジ時間 5,434 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 91 ms
76,700 KB
testcase_07 AC 117 ms
76,548 KB
testcase_08 AC 99 ms
76,816 KB
testcase_09 AC 114 ms
76,212 KB
testcase_10 AC 108 ms
76,804 KB
testcase_11 AC 144 ms
76,944 KB
testcase_12 AC 79 ms
75,796 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 74 ms
71,448 KB
testcase_24 AC 74 ms
71,124 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

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 dfs1(n):
    l = links[n]
    for x in l:
        del links[x][n]
    for x in l:
        dfs1(x)
dfs1(0)

result = 0
t = []
def dfs2(n):
    global result
    l = links[n]
    if len(l) == 0:
        return 1
    c = 0
    for x in l:
        a = dfs2(x)
        c += a
        t.append((l[x], a))
        result += l[x] * a
    return a
dfs2(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