結果

問題 No.872 All Tree Path
ユーザー titiatitia
提出日時 2019-10-05 04:47:06
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,333 ms / 3,000 ms
コード長 647 bytes
コンパイル時間 76 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 79,648 KB
最終ジャッジ日時 2024-04-15 03:54:44
合計ジャッジ時間 12,415 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,333 ms
75,136 KB
testcase_01 AC 1,314 ms
75,392 KB
testcase_02 AC 1,327 ms
75,136 KB
testcase_03 AC 765 ms
79,648 KB
testcase_04 AC 28 ms
10,752 KB
testcase_05 AC 1,318 ms
75,264 KB
testcase_06 AC 1,330 ms
75,136 KB
testcase_07 AC 1,321 ms
75,264 KB
testcase_08 AC 120 ms
17,152 KB
testcase_09 AC 121 ms
17,152 KB
testcase_10 AC 126 ms
17,408 KB
testcase_11 AC 120 ms
17,152 KB
testcase_12 AC 130 ms
17,280 KB
testcase_13 AC 28 ms
10,752 KB
testcase_14 AC 28 ms
10,752 KB
testcase_15 AC 28 ms
10,880 KB
testcase_16 AC 28 ms
10,624 KB
testcase_17 AC 28 ms
10,624 KB
testcase_18 AC 29 ms
10,880 KB
testcase_19 AC 28 ms
10,880 KB
権限があれば一括ダウンロードができます

ソースコード

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