結果

問題 No.872 All Tree Path
ユーザー nadarenadare
提出日時 2019-08-30 22:35:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 641 ms / 3,000 ms
コード長 637 bytes
コンパイル時間 1,295 ms
コンパイル使用メモリ 86,668 KB
実行使用メモリ 156,712 KB
最終ジャッジ日時 2023-08-14 06:17:17
合計ジャッジ時間 9,409 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 641 ms
154,520 KB
testcase_01 AC 626 ms
155,216 KB
testcase_02 AC 631 ms
154,888 KB
testcase_03 AC 371 ms
146,180 KB
testcase_04 AC 73 ms
71,508 KB
testcase_05 AC 630 ms
155,064 KB
testcase_06 AC 625 ms
154,760 KB
testcase_07 AC 633 ms
156,712 KB
testcase_08 AC 157 ms
82,360 KB
testcase_09 AC 157 ms
82,504 KB
testcase_10 AC 153 ms
82,420 KB
testcase_11 AC 155 ms
82,556 KB
testcase_12 AC 153 ms
82,524 KB
testcase_13 AC 69 ms
71,072 KB
testcase_14 AC 71 ms
71,212 KB
testcase_15 AC 70 ms
71,396 KB
testcase_16 AC 72 ms
71,364 KB
testcase_17 AC 71 ms
71,132 KB
testcase_18 AC 72 ms
71,364 KB
testcase_19 AC 71 ms
71,096 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