結果

問題 No.872 All Tree Path
コンテスト
ユーザー titia
提出日時 2019-10-05 04:47:06
言語 Python3
(3.14.7 + numpy 2.5.2 + scipy 1.18.0 + ACL)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 892 ms / 3,000 ms
+ 187µs
コード長 647 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 57 ms
コンパイル使用メモリ 15,232 KB
実行使用メモリ 79,740 KB
最終ジャッジ日時 2026-09-10 04:54:01
合計ジャッジ時間 9,834 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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