結果

問題 No.1488 Max Score of the Tree
ユーザー sgswsgsw
提出日時 2021-04-30 02:33:17
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,775 bytes
コンパイル時間 108 ms
コンパイル使用メモリ 10,880 KB
実行使用メモリ 120,204 KB
最終ジャッジ日時 2023-09-25 02:28:42
合計ジャッジ時間 7,547 ms
ジャッジサーバーID
(参考情報)
judge14 / 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 #

import sys


def input():
    return sys.stdin.readline().rstrip()


"""
Do not get stuck on a problem for more than 20 minutes
Just check the editorial :)
There should be something else to do insead
"""

INF = 1 << 128


def slv():
    n, k = map(int, input().split())
    g = [[]for i in range(n)]
    edge = []
    for i in range(n - 1):
        u, v, c = map(int, input().split())
        u -= 1
        v -= 1
        g[u].append((v,c))
        g[v].append((u,c))
        edge.append((u, v, c))

    visited = [INF]*(n)
    cnt = [0]*(n)
    par = [-1]*(n)

    ans = 0

    def dfs(S):
        if visited[S] == INF:
            visited[S] = 0
        for adj,c in g[S]:
            if visited[adj] == INF:
                par[adj] = S
                visited[adj] = visited[S] + c          
                dfs(adj)

        if all(visited[u] < visited[S] for u,c in g[S]):
            cnt[S] = 1
        if par[S] >= 0:
            cnt[par[S]] += cnt[S]


    dfs(0)

    for i in range(n):
        if all(visited[u] < visited[i] for u,c in g[i]):
            ans += visited[i]

    data = []

    for u, v, c in edge:
        if visited[u] < visited[v]:
            data.append((cnt[v], c))
        else:
            data.append((cnt[u], c))


    dp = [[-INF]*(k + 1) for i in range(n)]
    dp[0][0] = 0

    for i in range(1,n):
        cnt,cost = data[i - 1]
        V,cost = cnt * cost,cost
        for j in range(k + 1):
            if j + cost <= k:
                dp[i][j + cost] = max(dp[i][j + cost],dp[i - 1][j] + V)
            dp[i][j] = max(dp[i][j],dp[i - 1][j])
    ans += max(dp[n - 1][c] for c in range(k + 1))
    print(ans)
    return


def main():
    t = 1
    for i in range(t):
        slv()
    return


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