結果

問題 No.872 All Tree Path
ユーザー hedwig100hedwig100
提出日時 2020-04-14 12:39:46
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 949 ms / 3,000 ms
コード長 650 bytes
コンパイル時間 233 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 112,896 KB
最終ジャッジ日時 2024-04-09 05:18:36
合計ジャッジ時間 11,740 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 945 ms
69,248 KB
testcase_01 AC 945 ms
68,992 KB
testcase_02 AC 949 ms
68,992 KB
testcase_03 AC 661 ms
112,896 KB
testcase_04 AC 109 ms
24,832 KB
testcase_05 AC 931 ms
69,120 KB
testcase_06 AC 932 ms
69,120 KB
testcase_07 AC 935 ms
68,992 KB
testcase_08 AC 181 ms
29,440 KB
testcase_09 AC 180 ms
29,312 KB
testcase_10 AC 183 ms
29,440 KB
testcase_11 AC 180 ms
29,312 KB
testcase_12 AC 175 ms
29,312 KB
testcase_13 AC 105 ms
24,832 KB
testcase_14 AC 107 ms
24,832 KB
testcase_15 AC 111 ms
24,960 KB
testcase_16 AC 108 ms
24,960 KB
testcase_17 AC 115 ms
24,832 KB
testcase_18 AC 105 ms
24,832 KB
testcase_19 AC 105 ms
24,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

INF = 10 ** 7
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)

MAXN = 200005
G = [[] for _ in range(MAXN)]

ans = 0
def dfs(i,n,p = -1):
    global ans
    ret = 1
    for e,c in G[i]:
        if e == p:
            continue
        tmp = dfs(e,n,i)
        ans += 2 * tmp * (n - tmp) * c
        ret += tmp
    return ret

def main():
    n = int(input())
    for _ in range(n - 1):
        a,b,c = map(int,input().split())
        a -= 1
        b -= 1
        G[a].append((b,c))
        G[b].append((a,c))
    
    dfs(0,n)
    global ans
    print(ans)
if __name__ == '__main__':
    main()
0