結果

問題 No.872 All Tree Path
ユーザー 👑 KazunKazun
提出日時 2020-11-13 03:38:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 590 ms / 3,000 ms
コード長 784 bytes
コンパイル時間 333 ms
コンパイル使用メモリ 87,220 KB
実行使用メモリ 143,436 KB
最終ジャッジ日時 2023-09-30 01:54:31
合計ジャッジ時間 7,528 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 584 ms
136,016 KB
testcase_01 AC 573 ms
139,048 KB
testcase_02 AC 590 ms
139,260 KB
testcase_03 AC 309 ms
143,436 KB
testcase_04 AC 93 ms
71,664 KB
testcase_05 AC 563 ms
136,384 KB
testcase_06 AC 541 ms
135,764 KB
testcase_07 AC 587 ms
139,160 KB
testcase_08 AC 164 ms
84,140 KB
testcase_09 AC 166 ms
84,104 KB
testcase_10 AC 166 ms
84,148 KB
testcase_11 AC 161 ms
84,224 KB
testcase_12 AC 160 ms
84,380 KB
testcase_13 AC 94 ms
71,808 KB
testcase_14 AC 94 ms
71,600 KB
testcase_15 AC 95 ms
71,608 KB
testcase_16 AC 91 ms
71,756 KB
testcase_17 AC 92 ms
71,592 KB
testcase_18 AC 92 ms
71,668 KB
testcase_19 AC 90 ms
71,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque
input=sys.stdin.readline

N=int(input())
E=[[] for _ in range(N+1)]

Edge=[0]*(N-1)
for i in range(N-1):
    u,v,w=map(int,input().split())
    E[u].append(v)
    E[v].append(u)

    Edge[i]=(u,v,w)

root=1
Depth=[-1]*(N+1)
Depth[root]=0
Parent=[-1]*(N+1)
Parent[root]=0
Q=deque([root])

F=[[] for _ in range(N)]
F[0]=[root]
while Q:
    v=Q.popleft()

    for u in E[v]:
        if Depth[u]==-1:
            Depth[u]=Depth[v]+1
            Parent[u]=v
            Q.append(u)
            F[Depth[u]].append(u)

Descend=[1]*(N+1)

for G in F[::-1]:
    for x in G:
        if x!=root:
            Descend[Parent[x]]+=Descend[x]

X=0
for (u,v,w) in Edge:
    if Depth[u]>Depth[v]:
        u,v=v,u

    X+=w*Descend[v]*(N-Descend[v])
print(2*X)
0