結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,403 ms
176,240 KB
testcase_01 AC 1,394 ms
175,924 KB
testcase_02 AC 1,409 ms
175,908 KB
testcase_03 AC 662 ms
174,704 KB
testcase_04 AC 64 ms
66,560 KB
testcase_05 AC 1,419 ms
175,960 KB
testcase_06 AC 1,354 ms
175,696 KB
testcase_07 AC 1,387 ms
176,372 KB
testcase_08 AC 242 ms
87,604 KB
testcase_09 AC 242 ms
88,064 KB
testcase_10 AC 243 ms
87,780 KB
testcase_11 AC 244 ms
87,960 KB
testcase_12 AC 243 ms
88,576 KB
testcase_13 AC 64 ms
66,688 KB
testcase_14 AC 64 ms
67,328 KB
testcase_15 AC 66 ms
67,328 KB
testcase_16 AC 65 ms
67,460 KB
testcase_17 AC 65 ms
67,328 KB
testcase_18 AC 65 ms
67,328 KB
testcase_19 AC 66 ms
67,200 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