結果

問題 No.872 All Tree Path
ユーザー nadarenadare
提出日時 2019-08-30 22:35:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 536 ms / 3,000 ms
コード長 637 bytes
コンパイル時間 1,729 ms
コンパイル使用メモリ 81,588 KB
実行使用メモリ 156,704 KB
最終ジャッジ日時 2024-05-01 19:02:29
合計ジャッジ時間 7,531 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 536 ms
156,056 KB
testcase_01 AC 509 ms
156,440 KB
testcase_02 AC 517 ms
156,692 KB
testcase_03 AC 296 ms
145,612 KB
testcase_04 AC 34 ms
52,376 KB
testcase_05 AC 515 ms
156,580 KB
testcase_06 AC 509 ms
155,816 KB
testcase_07 AC 525 ms
156,704 KB
testcase_08 AC 111 ms
81,408 KB
testcase_09 AC 113 ms
81,368 KB
testcase_10 AC 107 ms
81,496 KB
testcase_11 AC 111 ms
81,356 KB
testcase_12 AC 111 ms
81,724 KB
testcase_13 AC 33 ms
52,224 KB
testcase_14 AC 34 ms
52,096 KB
testcase_15 AC 34 ms
52,224 KB
testcase_16 AC 34 ms
52,352 KB
testcase_17 AC 34 ms
52,224 KB
testcase_18 AC 33 ms
52,480 KB
testcase_19 AC 34 ms
52,480 KB
権限があれば一括ダウンロードができます

ソースコード

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