結果
| 問題 | No.872 All Tree Path | 
| コンテスト | |
| ユーザー |  AT274_ | 
| 提出日時 | 2019-10-14 14:42:39 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 791 ms / 3,000 ms | 
| コード長 | 799 bytes | 
| コンパイル時間 | 530 ms | 
| コンパイル使用メモリ | 82,176 KB | 
| 実行使用メモリ | 178,300 KB | 
| 最終ジャッジ日時 | 2024-12-23 22:50:10 | 
| 合計ジャッジ時間 | 10,062 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 18 | 
ソースコード
# nadare様のコード参考
# https://yukicoder.me/submissions/374115
N = int(input())
size = [1] * N
depth = [-1] * N
G = [[] for i in range(N)]
E = []
for i in range(N - 1):
    a, b, w = map(int, input().split())
    a, b = a - 1, b - 1
    G[a].append([b, w])
    G[b].append([a, w])
    E.append([a, b, w])
depth[0] = 0
visited = [False] * N
stack = [0]
rev_follow = []
while stack:
    n = stack.pop()
    visited[n] = True
    for to, w in G[n]:
        if visited[to]:
            continue
        stack.append(to)
        depth[to] = depth[n] + 1
        rev_follow.append([n, to])
while rev_follow:
    a, b = rev_follow.pop()
    size[a] += size[b]
ans = 0
for a, b, w in E:
    if depth[a] < depth[b]:
        a, b = b, a
    ans += 2 * w * size[a] * (N - size[a])
print(ans)
            
            
            
        