結果

問題 No.872 All Tree Path
ユーザー ningenMeningenMe
提出日時 2019-08-30 01:10:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,403 ms / 3,000 ms
コード長 638 bytes
コンパイル時間 175 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 176,504 KB
最終ジャッジ日時 2024-04-28 22:59:09
合計ジャッジ時間 13,936 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,333 ms
176,128 KB
testcase_01 AC 1,357 ms
176,048 KB
testcase_02 AC 1,403 ms
175,784 KB
testcase_03 AC 692 ms
174,580 KB
testcase_04 AC 66 ms
67,200 KB
testcase_05 AC 1,399 ms
175,796 KB
testcase_06 AC 1,381 ms
175,564 KB
testcase_07 AC 1,394 ms
176,504 KB
testcase_08 AC 257 ms
87,596 KB
testcase_09 AC 258 ms
88,064 KB
testcase_10 AC 257 ms
87,644 KB
testcase_11 AC 255 ms
87,808 KB
testcase_12 AC 250 ms
88,576 KB
testcase_13 AC 72 ms
66,944 KB
testcase_14 AC 74 ms
66,944 KB
testcase_15 AC 76 ms
67,328 KB
testcase_16 AC 75 ms
67,328 KB
testcase_17 AC 74 ms
67,200 KB
testcase_18 AC 74 ms
67,328 KB
testcase_19 AC 74 ms
67,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import queue
N = int(input())
edge = [[] for i in range(N)]
size = [1 for i in range(N)]
parent = [[-1,-1] for i in range(N)]

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

q = queue.Queue()
s = queue.LifoQueue()
parent[0] = [0,0]
q.put(0)
while q.qsize() > 0:
    f = q.get()
    for t,w in edge[f]:
        if parent[t][0] == -1:
            parent[t] = [f,w]
            q.put(t)
    s.put(f)
ans = 0
while s.qsize() > 0:
    f = s.get()
    if s != 0:
        size[parent[f][0]] += size[f]
        ans += size[f]*(N-size[f])*parent[f][1]*2
print(ans)
0