結果

問題 No.872 All Tree Path
ユーザー nadare
提出日時 2019-08-30 22:35:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 702 ms / 3,000 ms
コード長 637 bytes
コンパイル時間 337 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 156,832 KB
最終ジャッジ日時 2024-11-22 01:05:08
合計ジャッジ時間 7,742 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

def inpl(): return list(map(int, input().split()))

N = int(input())
size = [1]*N
depth = [-1]*N

G = [[] for _ in range(N)]
E = []
for _ in range(N-1):
    a, b, w = inpl()
    a, b = a-1, b-1
    G[a].append(b)
    G[b].append(a)
    E.append([a, b, w])

depth[0] = 0
Q = [0]
Q2 = []

while Q:
    p = Q.pop()
    for q in G[p]:
        if depth[q] != -1:
            continue
        depth[q] = depth[p] + 1
        Q.append(q)
        Q2.append([p, q])

while Q2:
    a, b = Q2.pop()
    size[a] += size[b]

ans = 0

for a, b, w in E:
    if depth[a] < depth[b]:
        a, b = b, a
    ans += w * size[a] * (N-size[a])

print(ans*2)
0