結果

問題 No.872 All Tree Path
ユーザー ああいいああいい
提出日時 2021-12-05 14:32:07
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,639 ms / 3,000 ms
コード長 716 bytes
コンパイル時間 100 ms
コンパイル使用メモリ 10,980 KB
実行使用メモリ 61,516 KB
最終ジャッジ日時 2023-09-21 14:02:06
合計ジャッジ時間 16,021 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,578 ms
47,712 KB
testcase_01 AC 1,574 ms
47,616 KB
testcase_02 AC 1,582 ms
47,792 KB
testcase_03 AC 1,236 ms
61,516 KB
testcase_04 AC 16 ms
7,960 KB
testcase_05 AC 1,599 ms
47,724 KB
testcase_06 AC 1,639 ms
47,724 KB
testcase_07 AC 1,580 ms
47,608 KB
testcase_08 AC 146 ms
12,084 KB
testcase_09 AC 141 ms
12,204 KB
testcase_10 AC 148 ms
12,188 KB
testcase_11 AC 148 ms
12,112 KB
testcase_12 AC 145 ms
12,152 KB
testcase_13 AC 16 ms
7,976 KB
testcase_14 AC 16 ms
8,116 KB
testcase_15 AC 17 ms
8,008 KB
testcase_16 AC 16 ms
7,972 KB
testcase_17 AC 17 ms
7,996 KB
testcase_18 AC 16 ms
8,068 KB
testcase_19 AC 16 ms
8,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
u = [0] * (N-1)
v = [0] * (N-1)
w = [0] * (N-1)
for i in range(N-1):
    u[i],v[i],w[i] = list(map(int,input().split()))
    
tree = [[] for _ in range(N+1)]
for i in range(N-1):
    tree[u[i]].append(v[i])
    tree[v[i]].append(u[i])
ko = [0] * (N+1)
q = []
q.append((1,0))
while len(q):
    now,parent = q.pop()
    if ko[now] == 0:
        ko[now] = 1
        q.append((now,parent))
        for i in tree[now]:
            if i != parent:
                q.append((i,now))
    else:
        for i in tree[now]:
            if i != parent:
                ko[now] += ko[i]
s = 0
for i in range(N-1):
    m = min(ko[u[i]],ko[v[i]])
    M = N - m
    s += m * M * w[i]
print(s * 2)
                
0