結果

問題 No.872 All Tree Path
ユーザー titia
提出日時 2019-10-05 04:47:06
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 1,326 ms / 3,000 ms
コード長 647 bytes
コンパイル時間 182 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 79,520 KB
最終ジャッジ日時 2024-10-04 14:28:03
合計ジャッジ時間 12,705 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N=int(input())
EDGE=[[] for i in range(N+1)]

for j in range(N-1):
    u,v,w=map(int,input().split())
    EDGE[u].append((v,w))
    EDGE[v].append((u,w))


from collections import deque
Q=deque([1])
USE=[0]*(N+1)
USE[1]=1

SORT=[1]

while Q:
    x=Q.pop()

    for to,_ in EDGE[x]:
        if USE[to]==0:
            SORT.append(to)
            Q.append(to)
            USE[to]=1

SORT.reverse()
ANS=0
SCORE=[1]*(N+1)
USE=[0]*(N+1)

for s in SORT:
    USE[s]=1

    for to,w in EDGE[s]:
        if USE[to]==0:
            ANS+=(SCORE[s])*(N-SCORE[s])*w
            SCORE[to]+=SCORE[s]

print(ANS*2)
        
0