結果

問題 No.1488 Max Score of the Tree
ユーザー ygd.ygd.
提出日時 2021-04-23 22:26:50
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,842 bytes
コンパイル時間 323 ms
コンパイル使用メモリ 86,952 KB
実行使用メモリ 77,736 KB
最終ジャッジ日時 2023-09-17 12:33:34
合計ジャッジ時間 4,728 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 101 ms
77,052 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 75 ms
71,196 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 101 ms
77,056 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 72 ms
71,496 KB
testcase_24 AC 74 ms
71,296 KB
testcase_25 AC 80 ms
76,016 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())
G = [[] for _ in range(N)]
RN = [[-1]*N for _ in range(N)]
for i in range(N-1):
    a,b,c = map(int,input().split())
    a-=1;b-=1 #0-index
    G[a].append((b,c,i)) #相手、cost, index
    G[b].append((a,c,i))
    RN[a][b] = (i,c)
    RN[b][a] = (i,c)
#print(RN)
leaf = [0]*N
par = [-1]*N
stack = []
stack.append(~0)
stack.append(0)
while stack:
    v = stack.pop()
    if v >= 0:
        for u,cost,idx in G[v]:
            if u == par[v]: continue
            par[u] = v
            stack.append(~u)
            stack.append(u)
    else:
        a = ~v
        if a == 0: continue
        if len(G[a]) == 1:
            leaf[a] = 1
            leaf[par[a]] += 1
        else:
            #print(a,par[a],"L",leaf[a])
            leaf[par[a]] += leaf[a]
#print(par)
#print(leaf)
ans = 0
for i in range(N):
    npar = [-1]*N
    stack = [i]
    while stack:
        v = stack.pop()
        for u,cost,idx in G[v]:
            if u == npar[v]: continue
            npar[u] = v
            stack.append(u)
    #print(i,npar)
    for j in range(i+1,N):
        cum = 0
        nj = j
        used = [0]*(N-1)
        Flag = 0
        while npar[nj] != -1:
            nxt = npar[nj]
            idx, cost = RN[nj][nxt]
            used[idx] = 1
            cum += cost
            nj = nxt
            if cum > K:
                Flag = 1
                break
        if Flag == 1: continue
        ret = 0
        stack = [0]
        while stack:
            v = stack.pop()
            for u,cost,idx in G[v]:
                if u == par[v]: continue
                if used[idx] == 1:
                    ret += cost*leaf[u]*2
                else:
                    ret += cost*leaf[u]
                stack.append(u)
        #print(i,j,ret,used)
        ans = max(ans,ret)
print(ans)
                    
0