結果

問題 No.872 All Tree Path
ユーザー FromBooskaFromBooska
提出日時 2023-03-02 08:51:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 736 ms / 3,000 ms
コード長 986 bytes
コンパイル時間 1,116 ms
コンパイル使用メモリ 81,744 KB
実行使用メモリ 290,416 KB
最終ジャッジ日時 2023-10-17 07:05:58
合計ジャッジ時間 9,226 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 597 ms
129,440 KB
testcase_01 AC 581 ms
129,308 KB
testcase_02 AC 736 ms
129,716 KB
testcase_03 AC 581 ms
290,416 KB
testcase_04 AC 38 ms
53,492 KB
testcase_05 AC 590 ms
129,440 KB
testcase_06 AC 600 ms
129,648 KB
testcase_07 AC 591 ms
129,552 KB
testcase_08 AC 137 ms
81,428 KB
testcase_09 AC 139 ms
81,572 KB
testcase_10 AC 131 ms
81,548 KB
testcase_11 AC 140 ms
81,524 KB
testcase_12 AC 137 ms
81,432 KB
testcase_13 AC 39 ms
53,492 KB
testcase_14 AC 39 ms
53,492 KB
testcase_15 AC 41 ms
53,492 KB
testcase_16 AC 39 ms
53,492 KB
testcase_17 AC 39 ms
53,492 KB
testcase_18 AC 39 ms
53,492 KB
testcase_19 AC 39 ms
53,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 全点対最短距離の和
# グラフならダイクストラ、ワーシャルフロイド、BFS/DFSなどかと思ったが
# 木なので最短距離というか1本道しかない
# ルート・子の数で行けそう
# たとえばある辺の頂点u, v. u側の頂点の数*v側の頂点の数*辺コスト*2の和
# 子の数はDFSで数えるか

N = int(input())
edges = [[] for i in range(N+1)]
edge_list = []
for i in range(N-1):
    u, v, w = map(int, input().split())
    edges[u].append(v)
    edges[v].append(u)
    edge_list.append((u, v, w))

import sys
sys.setrecursionlimit(10**7)

def dfs(current, previous):
    if child[current] > 0:
        return
    calc = 0
    for nxt in edges[current]:
        if nxt != previous:
            dfs(nxt, current)
            calc += child[nxt]
    child[current] = calc + 1

child = [-1]*(N+1)
root = 1
dfs(root, 0)

ans = 0
for u, v, w in edge_list:
    mn = min(child[u], child[v])
    ans += mn*(N-mn)*w*2
print(ans)

0