結果

問題 No.1488 Max Score of the Tree
ユーザー ああいいああいい
提出日時 2021-12-28 21:07:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 230 ms / 2,000 ms
コード長 920 bytes
コンパイル時間 324 ms
コンパイル使用メモリ 82,164 KB
実行使用メモリ 145,316 KB
最終ジャッジ日時 2024-04-10 13:15:47
合計ジャッジ時間 5,416 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 202 ms
140,672 KB
testcase_01 AC 210 ms
137,796 KB
testcase_02 AC 218 ms
142,964 KB
testcase_03 AC 216 ms
143,032 KB
testcase_04 AC 226 ms
145,316 KB
testcase_05 AC 32 ms
53,668 KB
testcase_06 AC 82 ms
82,748 KB
testcase_07 AC 138 ms
109,628 KB
testcase_08 AC 105 ms
94,460 KB
testcase_09 AC 93 ms
86,596 KB
testcase_10 AC 138 ms
109,880 KB
testcase_11 AC 214 ms
143,240 KB
testcase_12 AC 34 ms
57,652 KB
testcase_13 AC 80 ms
87,028 KB
testcase_14 AC 128 ms
104,108 KB
testcase_15 AC 92 ms
87,160 KB
testcase_16 AC 54 ms
69,980 KB
testcase_17 AC 77 ms
81,024 KB
testcase_18 AC 158 ms
118,000 KB
testcase_19 AC 109 ms
95,868 KB
testcase_20 AC 75 ms
77,744 KB
testcase_21 AC 52 ms
67,808 KB
testcase_22 AC 99 ms
89,404 KB
testcase_23 AC 34 ms
53,576 KB
testcase_24 AC 33 ms
52,524 KB
testcase_25 AC 34 ms
53,628 KB
testcase_26 AC 79 ms
82,336 KB
testcase_27 AC 55 ms
67,964 KB
testcase_28 AC 67 ms
73,352 KB
testcase_29 AC 76 ms
77,644 KB
testcase_30 AC 185 ms
127,684 KB
testcase_31 AC 230 ms
145,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,K = map(int,input().split())
G = [[] for _ in range(N + 1)]
for _ in range(N - 1):
    a,b,c = map(int,input().split())
    G[a].append((b,c))
    G[b].append((a,c))

node = [[0] * 2 for _ in range(N + 1)]
edge = []
score = 0
def tansaku(now = 1,parent = 0):
    global score
    node[now][0] = parent
    if now != 1 and len(G[now]) == 1:
        node[now][1] = 1
        return 1
    for v,c in G[now]:
        if v != parent:
            t = tansaku(v,now)
            node[now][1] += t
            edge.append((c,t))
            score += c * t
    return node[now][1]
tansaku()
dp = [[0] * (K + 1) for _ in range(N-1)]
dp[0][edge[0][0]] = edge[0][1] * edge[0][0]
for i in range(1,N-1):
    c,t = edge[i]
    for j in range(K + 1):
        dp[i][j] = dp[i-1][j]
        if j - c >= 0:
            if dp[i][j] < dp[i-1][j-c] + c * t:
                dp[i][j] = dp[i-1][j-c] + c * t
m = max(dp[N-2])
print(m + score)
0