結果

問題 No.1488 Max Score of the Tree
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2022-09-01 21:29:52
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,111 bytes
コンパイル時間 657 ms
コンパイル使用メモリ 82,264 KB
実行使用メモリ 150,444 KB
最終ジャッジ日時 2024-04-26 21:46:17
合計ジャッジ時間 6,378 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 39 ms
53,292 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 41 ms
58,956 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 206 ms
122,416 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 39 ms
54,308 KB
testcase_24 AC 38 ms
53,216 KB
testcase_25 AC 41 ms
54,912 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

N,K = map(int,input().split())
e = [[] for _ in range(N)]
edge = dict()
I = 0
for _ in range(N-1):
    a,b,c = map(int,input().split())
    a -= 1
    b -= 1
    e[a].append(b)
    e[b].append(a)
    edge[(a,b)]=c
    edge[(b,a)]=c
    
depth = [-1]*N
leaves = [-1]*N
depth[0] = 0
for i in range(1,N):
    if len(e[i])==1:
        leaves[i] = 1
def dfs(x):
    
    if leaves[x] !=-1:
        return leaves[x]
    
    tmp = 0
    for ix in e[x]:
        if depth[ix]==-1:
            depth[ix] = depth[x]+1
    
    for ix in e[x]:
        if depth[ix]>depth[x]:
            tmp += dfs(ix)
            
    leaves[x] = tmp
    return tmp
    
dfs(0)
E = []

for i in range(N):
    
    for j in e[i]:
        
        if depth[j]<depth[i]:
            c = edge[(i,j)]
            E.append((c,c*leaves[i]))
            I += c*leaves[i]
            break

N = len(E)
dp = [[0]*(K+2) for _ in range(N)]
a,b = E[0]
dp[0][min(a,K+1)]=b
for i in range(N-1):
    a,b = E[i+1]
    for j in range(K+1):
        
        dp[i+1][min(j+a,K+1)] = max(dp[i][j] + b,dp[i][min(j+a,K+1)])

print(max(dp[-1][:K+1])+I)
    
    
0