結果

問題 No.872 All Tree Path
ユーザー tamatotamato
提出日時 2020-02-20 22:04:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 714 ms / 3,000 ms
コード長 961 bytes
コンパイル時間 158 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 165,288 KB
最終ジャッジ日時 2024-04-17 06:31:52
合計ジャッジ時間 8,341 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 714 ms
159,152 KB
testcase_01 AC 690 ms
159,412 KB
testcase_02 AC 700 ms
158,920 KB
testcase_03 AC 346 ms
165,288 KB
testcase_04 AC 40 ms
53,632 KB
testcase_05 AC 707 ms
158,988 KB
testcase_06 AC 681 ms
161,108 KB
testcase_07 AC 695 ms
164,344 KB
testcase_08 AC 113 ms
82,224 KB
testcase_09 AC 112 ms
82,048 KB
testcase_10 AC 110 ms
82,232 KB
testcase_11 AC 114 ms
82,048 KB
testcase_12 AC 113 ms
81,792 KB
testcase_13 AC 41 ms
53,376 KB
testcase_14 AC 40 ms
53,632 KB
testcase_15 AC 41 ms
53,504 KB
testcase_16 AC 42 ms
53,632 KB
testcase_17 AC 41 ms
53,504 KB
testcase_18 AC 41 ms
53,504 KB
testcase_19 AC 41 ms
53,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    from collections import deque
    input = sys.stdin.readline

    N = int(input())
    adj = [[] for _ in range(N+1)]
    E = {}
    for _ in range(N-1):
        a, b, w = map(int, input().split())
        adj[a].append(b)
        adj[b].append(a)
        E[a*(N+1)+b] = w
        E[b*(N+1)+a] = w

    que = deque()
    que.append(1)
    seen = [-1] * (N+1)
    seen[1] = 0
    par = [0] * (N+1)
    child = [[] for _ in range(N+1)]
    seq = []
    while que:
        v = que.popleft()
        seq.append(v)
        for u in adj[v]:
            if seen[u] == -1:
                seen[u] = seen[v] + 1
                par[u] = v
                child[v].append(u)
                que.append(u)
    seq.reverse()

    dp = [1] * (N+1)
    ans = 0
    for v in seq:
        for u in child[v]:
            dp[v] += dp[u]
            ans += E[v*(N+1)+u] * dp[u] * (N-dp[u]) * 2
    print(ans)


if __name__ == '__main__':
    main()
0