結果

問題 No.1488 Max Score of the Tree
ユーザー 小野寺健小野寺健
提出日時 2021-04-24 15:06:56
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 874 bytes
コンパイル時間 121 ms
コンパイル使用メモリ 11,100 KB
実行使用メモリ 329,600 KB
最終ジャッジ日時 2023-09-17 13:36:53
合計ジャッジ時間 6,568 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

N, K = map(int, input().split())

v = [[[], []] for _ in range(N)]
weight = []
for i in range(N-1):
    a, b, c = map(int, input().split())
    v[a-1][1].append((b-1, i))
    v[b-1][1].append((a-1, i))
    weight.append(c)
    
q = [(0, [])]
e = [0] * (N-1)
while len(q) > 0:
    i, c = q.pop(0)
    v[i][0] = c
    cnt = True
    for j, k in v[i][1]:
        if j == 0 or len(v[j][0]) > 0:
            continue
        cnt = False
        q.append((j, c+[k]))
    if cnt:
        for j in c:
            e[j] += 1

d = dict()

def dfs(i, val, k):
    if (i, val, k) in d:
        return d[(i, val, k)]
    if i == N-1:
        return val
    if weight[i] > k:
        ret = dfs(i+1, val+weight[i]*e[i], k)
    else:
        ret = max(dfs(i+1, val+weight[i]*e[i], k), dfs(i+1,  val+weight[i]*e[i]*2, k-weight[i]))
    d[(i, val, k)] = ret
    return ret

print(dfs(0, 0, K))
0