結果

問題 No.872 All Tree Path
ユーザー AT274_AT274_
提出日時 2019-10-14 14:42:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 793 ms / 3,000 ms
コード長 799 bytes
コンパイル時間 1,120 ms
コンパイル使用メモリ 87,008 KB
実行使用メモリ 180,368 KB
最終ジャッジ日時 2023-08-25 14:22:55
合計ジャッジ時間 9,846 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 793 ms
180,160 KB
testcase_01 AC 767 ms
180,368 KB
testcase_02 AC 775 ms
179,748 KB
testcase_03 AC 417 ms
177,864 KB
testcase_04 AC 69 ms
71,404 KB
testcase_05 AC 753 ms
180,196 KB
testcase_06 AC 761 ms
180,300 KB
testcase_07 AC 760 ms
179,924 KB
testcase_08 AC 171 ms
86,544 KB
testcase_09 AC 169 ms
86,244 KB
testcase_10 AC 167 ms
86,384 KB
testcase_11 AC 168 ms
86,248 KB
testcase_12 AC 169 ms
86,168 KB
testcase_13 AC 70 ms
71,556 KB
testcase_14 AC 69 ms
71,380 KB
testcase_15 AC 72 ms
71,484 KB
testcase_16 AC 71 ms
71,320 KB
testcase_17 AC 71 ms
71,332 KB
testcase_18 AC 72 ms
71,256 KB
testcase_19 AC 70 ms
71,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 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)
0