結果

問題 No.1488 Max Score of the Tree
ユーザー tamatotamato
提出日時 2021-04-23 21:43:45
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,403 bytes
コンパイル時間 756 ms
コンパイル使用メモリ 87,312 KB
実行使用メモリ 156,672 KB
最終ジャッジ日時 2023-09-17 11:56:56
合計ジャッジ時間 8,876 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 321 ms
149,880 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 102 ms
71,776 KB
testcase_06 AC 185 ms
96,900 KB
testcase_07 AC 283 ms
122,684 KB
testcase_08 AC 224 ms
108,796 KB
testcase_09 AC 207 ms
98,964 KB
testcase_10 WA -
testcase_11 AC 396 ms
156,536 KB
testcase_12 AC 110 ms
77,772 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 127 ms
81,596 KB
testcase_17 WA -
testcase_18 AC 266 ms
130,296 KB
testcase_19 AC 200 ms
106,564 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 183 ms
100,008 KB
testcase_23 AC 96 ms
72,016 KB
testcase_24 AC 100 ms
71,888 KB
testcase_25 AC 96 ms
72,124 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

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+1)]
    L = [0] * (N+1)
    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]
            for j in range(K+1):
                dp[p][j] = max(dp[p][j], dp[v][j])
                if j + c <= K:
                    dp[p][j+c] = max(dp[p][j+c], dp[v][j] + c * L[v])
    print(max(dp[1]))


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