結果

問題 No.1488 Max Score of the Tree
ユーザー 👑 tamatotamato
提出日時 2021-04-23 21:48:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 371 ms / 2,000 ms
コード長 1,480 bytes
コンパイル時間 561 ms
コンパイル使用メモリ 87,456 KB
実行使用メモリ 155,700 KB
最終ジャッジ日時 2023-09-17 12:03:59
合計ジャッジ時間 9,008 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 352 ms
152,044 KB
testcase_01 AC 340 ms
148,880 KB
testcase_02 AC 354 ms
153,104 KB
testcase_03 AC 363 ms
155,624 KB
testcase_04 AC 368 ms
155,628 KB
testcase_05 AC 99 ms
71,736 KB
testcase_06 AC 172 ms
96,292 KB
testcase_07 AC 258 ms
122,032 KB
testcase_08 AC 204 ms
108,264 KB
testcase_09 AC 183 ms
99,068 KB
testcase_10 AC 266 ms
123,460 KB
testcase_11 AC 369 ms
155,392 KB
testcase_12 AC 108 ms
77,296 KB
testcase_13 AC 147 ms
77,800 KB
testcase_14 AC 234 ms
114,432 KB
testcase_15 AC 186 ms
100,192 KB
testcase_16 AC 122 ms
81,056 KB
testcase_17 AC 155 ms
91,584 KB
testcase_18 AC 285 ms
129,424 KB
testcase_19 AC 204 ms
105,996 KB
testcase_20 AC 148 ms
88,640 KB
testcase_21 AC 123 ms
82,728 KB
testcase_22 AC 183 ms
99,364 KB
testcase_23 AC 99 ms
71,860 KB
testcase_24 AC 97 ms
72,028 KB
testcase_25 AC 98 ms
72,104 KB
testcase_26 AC 165 ms
94,732 KB
testcase_27 AC 119 ms
77,848 KB
testcase_28 AC 138 ms
85,376 KB
testcase_29 AC 147 ms
87,828 KB
testcase_30 AC 315 ms
139,928 KB
testcase_31 AC 371 ms
155,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    from collections import deque
    input = sys.stdin.readline

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

    que = deque()
    que.append(1)
    seen = [-1] * (N+1)
    seen[1] = 0
    par = [None] * (N+1)
    child = [[] for _ in range(N+1)]
    seq = []
    depth = [0] * (N+1)
    while que:
        v = que.popleft()
        seq.append(v)
        for u, c in adj[v]:
            if seen[u] == -1:
                seen[u] = seen[v] + 1
                par[u] = (v, c)
                child[v].append(u)
                que.append(u)
                depth[u] = depth[v] + c
    seq.reverse()

    D = 0
    for v in seq:
        if child[v]:
            continue
        D += depth[v]

    dp = [[D] * (K+1) for _ in range(N)]
    L = [0] * (N+1)
    E = []
    for v in seq:
        if not child[v]:
            L[v] = 1
        else:
            for u in child[v]:
                L[v] += L[u]
        if v != 1:
            p, c = par[v]
            E.append((c, c * L[v]))

    for i in range(N-1):
        w, v = E[i]
        for j in range(K+1):
            dp[i+1][j] = max(dp[i+1][j], dp[i][j])
            if j + w <= K:
                dp[i+1][j+w] = max(dp[i+1][j+w], dp[i][j] + v)
    print(max(dp[-1]))


if __name__ == '__main__':
    main()
0