結果

問題 No.872 All Tree Path
ユーザー FromBooskaFromBooska
提出日時 2023-08-30 21:30:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,083 ms / 3,000 ms
コード長 782 bytes
コンパイル時間 313 ms
コンパイル使用メモリ 87,064 KB
実行使用メモリ 368,480 KB
最終ジャッジ日時 2023-08-30 21:30:17
合計ジャッジ時間 10,392 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 862 ms
208,496 KB
testcase_01 AC 888 ms
208,244 KB
testcase_02 AC 1,083 ms
209,104 KB
testcase_03 AC 840 ms
368,480 KB
testcase_04 AC 73 ms
70,952 KB
testcase_05 AC 876 ms
208,892 KB
testcase_06 AC 892 ms
211,080 KB
testcase_07 AC 874 ms
209,168 KB
testcase_08 AC 198 ms
91,620 KB
testcase_09 AC 194 ms
92,284 KB
testcase_10 AC 191 ms
91,916 KB
testcase_11 AC 198 ms
91,736 KB
testcase_12 AC 191 ms
91,792 KB
testcase_13 AC 74 ms
71,320 KB
testcase_14 AC 74 ms
70,956 KB
testcase_15 AC 76 ms
71,160 KB
testcase_16 AC 75 ms
70,956 KB
testcase_17 AC 75 ms
71,228 KB
testcase_18 AC 75 ms
71,276 KB
testcase_19 AC 76 ms
70,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 部分木、子の数、DFS
# dfs2回は必要なかったので効率的な方法でやり直す

N = int(input())
edge_list = []
edge_dic = {}
edges = [[] for i in range(N+1)]
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))
    edge_dic[(u, v)] = w
    edge_dic[(v, u)] = w

child = [0]*(N+1)
visited = [0]*(N+1)

import sys
sys.setrecursionlimit(10**7)

def dfs(current):
    visited[current] = 1
    child[current] += 1
    
    for nxt in edges[current]:
        if visited[nxt] == 0:
            dfs(nxt)
            child[current] += child[nxt]

root = 1
dfs(root)
#print(child)

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