結果

問題 No.1488 Max Score of the Tree
ユーザー NoneNone
提出日時 2021-05-04 21:13:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 1,876 bytes
コンパイル時間 1,166 ms
コンパイル使用メモリ 87,088 KB
実行使用メモリ 76,732 KB
最終ジャッジ日時 2023-09-30 22:51:11
合計ジャッジ時間 5,522 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
76,568 KB
testcase_01 AC 127 ms
76,576 KB
testcase_02 AC 133 ms
76,508 KB
testcase_03 AC 130 ms
76,732 KB
testcase_04 AC 132 ms
76,676 KB
testcase_05 AC 74 ms
71,560 KB
testcase_06 AC 92 ms
76,272 KB
testcase_07 AC 108 ms
76,404 KB
testcase_08 AC 95 ms
76,380 KB
testcase_09 AC 95 ms
76,028 KB
testcase_10 AC 106 ms
76,680 KB
testcase_11 AC 133 ms
76,556 KB
testcase_12 AC 76 ms
76,000 KB
testcase_13 AC 89 ms
75,816 KB
testcase_14 AC 105 ms
76,340 KB
testcase_15 AC 94 ms
75,992 KB
testcase_16 AC 78 ms
76,236 KB
testcase_17 AC 86 ms
76,396 KB
testcase_18 AC 112 ms
76,440 KB
testcase_19 AC 97 ms
76,348 KB
testcase_20 AC 86 ms
75,892 KB
testcase_21 AC 80 ms
76,244 KB
testcase_22 AC 93 ms
76,108 KB
testcase_23 AC 72 ms
71,344 KB
testcase_24 AC 73 ms
71,396 KB
testcase_25 AC 73 ms
71,380 KB
testcase_26 AC 88 ms
76,476 KB
testcase_27 AC 79 ms
75,820 KB
testcase_28 AC 83 ms
75,868 KB
testcase_29 AC 84 ms
75,924 KB
testcase_30 AC 117 ms
76,528 KB
testcase_31 AC 140 ms
76,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def dfs(start=0,goal=None):
    parents={}
    p,t=start,0
    parents[p]=-2
    next_set=[(~p,t),(p,t)]
    if p==goal:
        return t
    while next_set:
        p,t=next_set.pop()
        if p>=0:
            for q,d in edges[p]:
                if q in parents:
                    continue
                if q==goal:
                    return t+1
                parents[q]=p
                next_set.append((~q,t+1))
                next_set.append((q,t+1))
        else: # 帰りがけ処理
            p=~p
            if len(edges[p])==1 and p!=start:
                cnt[p]=1
            else:
                for q,d in edges[p]:
                    if q==parents[p]:
                        continue
                    cnt[p]+=cnt[q]
                    dp[q]=cnt[q]*d
                    cost_list.append(dp[q])
                    weight_list.append(d)

    return -1

def knapsack_weight(single=True):
    """
    重さが小さい時のナップサックDP
    :param single: True = 重複なし
    """
    """ dp[weight <= W] = 重さ上限を固定した時の最大価値 """
    N=len(weight_list)
    dp_min = 0  # 総和価値の最小値
    dp = [dp_min] * (W + 1)

    for item in range(N):
        if single:
            S = range(W, weight_list[item] - 1, -1)
        else:
            S = range(weight_list[item], W + 1)
        for weight in S:
            if dp[weight] < dp[weight - weight_list[item]] + cost_list[item]:
                dp[weight] = dp[weight - weight_list[item]] + cost_list[item]
    return dp[W]

N,K=map(int, input().split())
edges=[[] for _ in range(N)]
dp=[0]*N
cnt=[0]*N
for _ in range(N-1):
    a,b,c=map(int, input().split())
    a-=1
    b-=1
    edges[a].append((b,c))
    edges[b].append((a,c))

W = K
cost_list = []
weight_list = []

dfs(start=0,goal=None)
S=sum(dp)

print(S+knapsack_weight(single=True))

0