結果

問題 No.1488 Max Score of the Tree
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2022-09-01 21:47:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 181 ms / 2,000 ms
コード長 1,036 bytes
コンパイル時間 434 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 72,448 KB
最終ジャッジ日時 2024-04-26 22:00:35
合計ジャッジ時間 5,011 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 172 ms
70,912 KB
testcase_01 AC 164 ms
71,168 KB
testcase_02 AC 171 ms
71,296 KB
testcase_03 AC 171 ms
71,296 KB
testcase_04 AC 172 ms
71,168 KB
testcase_05 AC 43 ms
52,480 KB
testcase_06 AC 90 ms
68,992 KB
testcase_07 AC 129 ms
70,784 KB
testcase_08 AC 106 ms
69,888 KB
testcase_09 AC 94 ms
66,304 KB
testcase_10 AC 129 ms
70,400 KB
testcase_11 AC 179 ms
72,448 KB
testcase_12 AC 52 ms
61,056 KB
testcase_13 AC 76 ms
64,768 KB
testcase_14 AC 114 ms
69,120 KB
testcase_15 AC 93 ms
67,712 KB
testcase_16 AC 65 ms
65,792 KB
testcase_17 AC 83 ms
67,712 KB
testcase_18 AC 134 ms
69,376 KB
testcase_19 AC 102 ms
69,248 KB
testcase_20 AC 77 ms
65,664 KB
testcase_21 AC 68 ms
67,584 KB
testcase_22 AC 91 ms
66,304 KB
testcase_23 AC 41 ms
52,224 KB
testcase_24 AC 40 ms
52,352 KB
testcase_25 AC 43 ms
52,864 KB
testcase_26 AC 90 ms
70,528 KB
testcase_27 AC 62 ms
63,616 KB
testcase_28 AC 71 ms
64,640 KB
testcase_29 AC 76 ms
65,792 KB
testcase_30 AC 152 ms
71,040 KB
testcase_31 AC 181 ms
71,936 KB
権限があれば一括ダウンロードができます

ソースコード

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 i in range(N):
    a,b = E[i]
    for j in range(K,-1,-1):
        dp[min(j+a,K+1)] = max(dp[j] + b,dp[min(j+a,K+1)])

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